Sujet : Re: getFirstDayOfMonth()
De : tr.17687 (at) *nospam* z991.linuxsc.com (Tim Rentsch)
Groupes : comp.lang.cDate : 14. Mar 2024, 23:57:54
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <86msr0pgjx.fsf@linuxsc.com>
References : 1 2 3 4 5 6 7 8 9 10 11
User-Agent : Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Michael S <
already5chosen@yahoo.com> writes:
On Mon, 4 Mar 2024 21:21:25 -0000 (UTC)
Lew Pitcher <lew.pitcher@digitalfreehold.ca> wrote:
[...]
If you want to /guarantee/ that the return value of strcmp()
is -1, 0, or +1 (for "less than", "equal to", or "greater than")
you will have to process the return value with something like
>
/*
** Returns -1 if argument < 0, 0 if argument == 0, 1 if argument > 0
*/
int iSignOf(int valu)
{
return ((valu > 0) - (valu < 0));
}
>
Too tricky to my liking.
I'd rather write straight-forward code and let compiler to figure
out the best sequence.
>
int iSignOf(int val)
{
if (val < 0)
return -1;
if (val > 0)
return +1;
return 0;
}
I've seen the idiomatic form before, and my reaction to it is
more of it being overly cute than overly tricky. That said,
it is a bit on the tricky side; even so, I'm not sure the cure
suggested is much better than the disease. I would simply write
a single return statement:
return val < 0 ? -1 : val > 0 ? 1 : 0;
(possibly condensed to take advantage of the 0/1 result of the
"greater than" operator for non-negative operands).
I suppose some people prefer the multiple return form to the ?:
form, although I'm not sure why except perhaps as a carryover
from earlier experience.