David Mathog said:
Why do you favor that form over the "spaceless" variant?
To be clear we're talking about
if(condition){
...
}
vs.
if (condition) {
...
}
(I also added a space between "if" and "("). I just find the latter
form significantly easier to read. Too many consecutive puncuation
characters tend to blend together visually. The space preceding the
"{" makes it easier to see. It doesn't (IMHO) need to be on a line
by itself, but it does need to stand out.
And I like a space after the "if" partly because the spaceless
version looks too much like a function call. It may also be partly
because, before learning C, I used a language (ok, it was Pascal)
that didn't use parentheses for if statements, and not having some
white space after "if" is a bit jarring.
And it's how K&R do it.
I don't suggest that my opinion is right and yours is wrong; it's
largely a matter of personal taste, K&R precedent, and my own
failing visual acuity.
The (long story) background for the first post is that once upon a
time I used to use:
if(condition)do_something();
but when "do_something" was too long wrote it as
if(condition)
do_something();
which, in nested code when the condition and do_something lines were
long enough that the right side wasn't normally visible, sometimes led
me to think this was:
if(condition){
do_something();}
so that another line could be added within the conditional
if(condition){
do_something_first();
do_something();}
when it was actually
if(condition)
do_something_first();
do_something();
which is not at all the same thing.
In my own style, and in every common style I've seen, a closing brace is
always on a line by itself. The only exception to this is the use, in
some styles (not mine), of "} else {".
Other times the confusion was:
if(long_condition)actual_do_something_off_right_side();
looks_like_conditional_but_not_do_something();
I'm not saying this happened even once a week, but it happened often
enough that I changed my style to avoid both of those, instead always
putting braces on the expression following an if:
if(condition){ do_something(); }
Personally, I almost always use braces for if, for, while, et al (a
habit I picked up from Perl, which requires them, but I think it's a
good idea regardless). I'll only omit the braces if the whole thing
fits on a single line *and* is clearer that way, so I'd write:
if (condition) do_something();
I'll violate my usual rules and write things on one line if there
are several consecutive similar statements, and aligning the code
vertically makes it easier to read:
if (condition1) do_something(1);
if (condition2) do_something(2);
if (condition3) do_something(3);
But it turns out that while this eliminates the preceding issue, it is
relatively easy for an errant key stroke to nip off the closing brace
since it is the last character on the line, leading to the issue that
started this thread. Particularly hard to see in long lines where the
closing brace is often off the right of the screen. As others have
noted, one can reduce the frequency of this issue by using the form:
if(condition)
{
do_something();
}
which is fine in low density code, but is on the losing end of the
readability tradeoff (in my opinion) in code that more closely
resembles a table, like this:
if(condition1){ a=1; b=2; c=3; /*more terms*/ }
else if(condition2){ a=1; b=0; c=3; /*more terms*/ }
else if(condition3){ a=1; b=2; c=0; /*more terms*/ }
/*etc.*/
There I would have aligned the "{"s, and possibly the conditions:
if (condition1) { a=1; b=2; c=3; /*more terms*/ }
else if (condition2) { a=1; b=0; c=3; /*more terms*/ }
else if (condition3) { a=1; b=2; c=0; /*more terms*/ }