bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: coding standards


From: Clark Wang
Subject: Re: coding standards
Date: Sun, 4 Mar 2018 20:43:02 +0800

On Sun, Mar 4, 2018 at 5:15 AM, don fong <dfong@dfong.com> wrote:

> admittedly this is a very minor point, but i am curious.  this has to do
> with coding standards for bash source.
>
> consider an if statement in C (or bash, for that matter).  which is form is
> better?
>
> Form (A):
>
>     if (flag)
>         X();
>     else
>         Y();
>
> Form (B):
>
>     if (flag == 0)
>         Y();
>     else
>         X();
>
> they are functionally equivalent.  but IMHO (A) is slightly more readable.
> first because flag (in this case) is intended to be a boolean value not
> arithmetic, and second because it's simpler to think about an if when the
> condition is positive.
>
> this is what i'd say if (B) were under code review.
>
> i submitted a patch with code in form (A).  it was added to the code base
> in form (B).  was there a good reason for this mutation?
>

I believe the main reason is to keep consistent with existing code.

I used to use ``if (flag)'' but these days I'd prefer ``if (flag != /* or
== */ 0)'' which is  explicit that ``flag'' is not a boolean var. I use
``if (flag)'' only when `flag' is a boolean. It's similar I used to write
``if (ptr)'' but now I prefer ``if (ptr != NULL)'' which is explicit that
`ptr' is a pointer.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]