trinity-devel@lists.pearsoncomputing.net

Message: previous - next
Month: January 2015

Re: [trinity-devel] Codebase formatting discussion

From: Slávek Banko <slavek.banko@...>
Date: Sat, 17 Jan 2015 15:52:58 +0100
On Sunday 11 of January 2015 22:20:50 Timothy Pearson wrote:
> > This is of course subjective and not a big deal for me as well (I have
> > already worked with both style actually).
> > Mostly two reasons:
> > 1) the operator at the end of the line tells me immediately that the
> > conditional expression is not over and I need to
> > continue reading the next line
>
> Wouldn't the lack of a curly brace state the same thing, given our rules
> on curly braces?
>
> > 2) logically easier to read complex statement. For example
> >
> > if (a == 3 &&
> >     (b == 5 || c == 4 ||
> >     (d == 10 && e == 20)))
> >
> > rather than:
> >
> > if (a == 3
> >     && (b == 5 || c == 4
> >
> >
> > I find the second one more prone to misinterpretation, since the || in
> > the 3rd row might look at the same level as the
> > && in the second row at first sight. And in TDE I have seen some rather
> > complex and long ifs :-)
> > Just my 2 cents, let's see what Slavek thinks as well.
>
> Point taken.  However, I think we need to add a rule as well that states
> all conditionals must be enclosed in parenthesis; I have cleaned up so
> many compiler warnings and so much buggy code becase of lazy programmers
> in this respect that I don't want to see another unenclosed conditional.
> ;-)
> If this rule is enforced, your second example becomes:
> if ((a == 3)
>     && ((b == 5) || (c == 4)
>
>
> which is a bit easier to read, but overall this style is still harder to
> read than your first example when more than one condition is present on
> one line.  Perhaps we should allow both styles and simply state that the
> style providing "best legibility" should be used?
>
> The number of long/complex ifs in the codebase are why I am insisting this
> be hammered out properly. :-)  We haven't head from Slavek in a while so I
> guess we'll keep drawing up the specification and he can review it and
> comment when he has time.

For me also are certainly more readable operators at the end of the line, 
rather than at beginning. The only case where I'm used to using the operators 
at the beginning of the line is sql where I use words operators (and, 
or, ...) instead of symbols.

By the way, in my usual style I'm used to indent brackets also in conditions, 
which from my perspective clarifies their nesting. For example:

if (a == 3 &&
    (b == 5 || c == 4 ||
     (d == 10 && e == 20)) &&
    (f == 15 || g == 25)) {
}

if (a == 3 && (b == 5 || c == 4 ||
               (d == 10 && e == 20)) &&
    (f == 15 || g == 25)) {
}


> Forcing parenthesis everywhere can make code more difficult to read
> sometimes, as in:
>
> if ((a == 3) || (b == 5) || (c == 4))
> vs
> if (a == 3 || b == 5 || c == 4)
>
> So I also favor the "best legibility" rule. Let's restate it as something
> like "the minimum number of parenthesis that clearly isolate a conditional
> logic block. Example:
>
> Right: if (a == 3 || (b == 5 && d != 20) || c == 4)
> Wrong: if (a == 3 || b == 5 && d != 20 || c == 4)

In this I agree with Michele.

-- 
Slávek