I
ImpalerCore
Not really. I just see a stated opinion, which is what I'm curious to
hear the reasoning behind.
I've been writing C for a living since 1998, so I know about the
syntax. I just fail to see how it is in any way safer, clearer or in
any other better than this:
if (ch == '\n') {
at_beginning = 1;
} else {
at_beginning = 0;
}
Showing off doesn't score high with me. And that is the only reason
for coding in that cramped style.
at_beginning = ch == '\n';
at_beginning = ch = '\n';
The one comment I'll offer is that the advantage of using a boolean
variable explicitly is to assign semantic meaning to a (possibly
complicated) assembly of boolean logic. Stuffing it directly in an
'if' expression provides the maintenance programmer less context. If
the whole purpose of the 'if/else' expression is to assign a boolean
value to 1 or 0 anyways, in my opinion, it's much more verbose than it
needs to be.
Getting back to the whole 'a = b == c' style debate, I think that the
expression is fine, but I will typically go to the point of making
'==' expression in parentheses a la 'a = (b == c)'. I typically don't
add parentheses with other conditions like &&, ||, <, >, etc., unless
it's part of a larger boolean expression, i.e. I have no problems with
'a = b < c'. Out of curiosity, would you write an 'if' expression for
'a = b < c'?
On a different note, I range between dislike and abhor most 'if
( variable = function() )' even if some of them have idiomatic
histories.
Both of the lines are equally plausible, when seen by themselves. So
to figure out the intent, you need to look elsewhere in the source. In
this case it isn't that hard, but when you are looking at 400k lines
of code written over a decade by a multitude of developers, each with
their own idea about how "clever" they are, constructs like this
become a major risk.
I don't think many will debate that having a company coding style
guidelines for projects of that size is in general a good idea. Of
course there's a big difference between saying it and actually doing
it.
Best regards,
John D.