Re: [PATCH 03/10] kill: flip all comparions to be in smaller - greater order

Sami Kerola
2014-03-19T19:35:08Z

On 19 March 2014 12:32, Benno Schulenberg <bensberg@justemail.net> wrote:
> On Sun, Mar 9, 2014, at 21:30, Sami Kerola wrote:
>> This makes code more readable.
>
> Hmm, I don't agree with that.
>
>> - if (argc > 2)
>> + if (2 < argc)
>
>> - if (num < SIGRTMIN || num > SIGRTMAX)
>> + if (num < SIGRTMIN || SIGRTMAX < num)
>
> I like to see always the variable on the left and the fixed value on
> the right.

Hi Benno,

Once up on time I also thought

if (value > constant)

is better, but then I read coreutils/HACKING file and found

http://thread.gmane.org/gmane.comp.version-control.git/3903/focus=4126

which made me to change my mind. Now I believe it is best to write
comparisions in order the numerical values, something like this:

1 < 2 && 3 < 4

There are couple reasons. First of all C, as many other programming
languages, are read from left to right. When counting smaller to greater
order somehow feels correct for most of the people. To be more concrete;
lets assume a value is expected to be within range 2 - 3, in this case

if (1 < value && value < 4)
do_something();

is how I would write the comparison. When the value must not be within
that range

if (value < 2 || 3 < value)
error();

With multiple operators sticking to only one way of comparing is, IMHO,
quite much more readable than using both operators. Notice that the
alternative would look, when wrote with constants, the following.

2 > 1 && 3 < 4

Since I like to get my eyes to read code without thinking too hard I like
to see all comparision operations in numeric order, including the cases
when there is exactly one comparison, such as

if (value < 0)

That said I am aware style is matter of taste, and things of that nature
are easy to disagree. Even if that would be the case I hope you don't
recard numeric order completely loonie.

--
Sami Kerola