i.e, if(x) { /* blah */ } vs. if (x) { /* blah /* }. I never understood why anyone would use the second style, it's not like it makes the code substantially easier to read (in fact, I think it's aesthetically worse and more distracting), and it wastes line capacity.
Oh, yes, the second one has a broken comment. I don't understand why anyone would use the second style either.
Name:
Anonymous2010-03-04 11:21
>line capacity
FrozenVoid corrupted your poor soul.
Name:
FrozenVoid2010-03-04 11:23
Optimize for maximum line capacity.
Name:
Anonymous2010-03-04 11:24
I always assumed the idea was to allow no ambiguity between the if and formal functions, the latter which typically require no space between the call and the parameter parentheses. I'm sure some people also do this as a matter of subjective readability or, to enforce a sort of personal consistency, most often they do it one way or another and they decide they'll do it that way. It's rather like the difference between:
int test() {
return 0;
}
and: int test()
{
return 0;
}
Here's one. How often do you/when do you (x - 1) as opposed to (x-1)? Which do you prefer? x++, x += 1, or x = x + 1?
Name:
Anonymous2010-03-04 11:34
>>5
Haven't you ever had a line that's just one character too long for whatever column limit you prefer? Mine's 80 columns for C code, and I run into it a lot with long conditionals, so all the space available can be important for elegance.
>>7
I've seen some people put spaces between functions and parens, too. And the first function form you give is a bastardization, not even K&R wrote functions that way. I'm fine with either K&R or BSD braces styles. I put spaces between arithmetic operators because bunched-up expressions are harder to understand.
I don't put spaces between the keyword or function name and the opening parenthesis, since the keyword or function name is associated with its parameters/expression; seeing a floating (...) with whitespace around it makes it look like the expression is standalone. Spaces between parameters are used when they look similar or there are decimal points/other things that could be mistaken as a comma.
Similarly, I prefer x*4 + 3*x vs x*4+3*x or x * 4 + 3 * x since it shows the operator precedence more clearly. (x * 4+3 * x would be the worse of the choices).
Name:
Anonymous2010-03-04 12:03
>>10 makes it look like the expression is standalone
In the case of if, I'd say it is!
No spaces of course, ( if 1 2 3) just looks stupid.
Name:
Anonymous2010-03-04 13:22
This thread is full of fucking 13 year old shit people. Why has nobody even named a coding style yet? K&R for life
if(x)
error: line 1: expected SEP
>>7
Perhaps toy examples like that, but for actual expressions I will refer you to: >>10
but ignore: decimal points/other things that could be mistaken as a comma
Try also (with reasoning): if (x) { /* syntax highlighting does not leave 'if' unnoticed, even when set apart from the condition */
if ( long && expression && here ); /* Spaces keep parentheses separate from each part of the condition, try also putting each part on a separate line indented the same amount */
}
function (butt); /* here you are doing 'function' to 'butt' */
many_params( "suck my shit", 0, 6, NULL ); /* here you are taking these many things and putting them into 'many_params', the immediate open parenthesis denotes its role as function; notice also comma, space as in written language */
I see that faggotry a lot in PHP, because PHP developers don't know the difference between built-in syntax and calling functions, and consequently, neither do PHP users.
Name:
Anonymous2010-03-04 18:26
>>10
If I have something like x * 4 + 3 * x I'd go 7*x (or at least 4*x + 3*x). The 7*x would definitely be faster by an insignificant amount if you can get it.
>>13
For reference I use: function(param1, param2);
if(param1 && param2)
{
//Stuff
}
There are three exceptions I can think of. When using Java's System.arraycopy(src,src_index, dest,dest_index, length); I unintentionally cluster the related parameters. (I didn't even have to think twice about writing that example just now!) When passing numeric parameters to functions, and making minor increments or decrements, I tend to cluster function(x-1); or function(x, (x-1)); in what I admit probably IS practice of a useless superstition (and the parentheses around the x-1 is just paranoia).
Finally, as a rule, I always use the example of the control structure above UNLESS I'm invoking a branching statement or a return on its own. if(param1 > param2) { continue; } // Yes, curly brackets