Braces
I have a question I always wanted to ask. A board with developers seems like a good place. :)
Why do most programmers prefer braces like
while(expression) {
stuff;
}instead of
while(expression)
{
stuff;
}Is it just the way you were taught? For instance, I had a professor who made us code the latter way. He would doc points if it was not EXACT...even tabs. God I hated that guy. :)
But, to me, he was not off-base with the braces. With the brace on the line after the expression, it is exactly in line with the closing one. This makes it really easy to match up and find missing ones. Sure, most of us use Eclipse or something that marks the matching brace, but there are times when I need to use something like Notepad or edit from a FTP editor that does not tell me that info. I even used an editor that drew a line straight down from a brace, so you could follow it and see the closing brace.
I also think the extra whitespace makes very nested code much easier to read.
I bring this up because it has always interested me as to why the first way is dominant. The first way saves some vertical space, but I don't think vertical space is a big deal though it may have been years ago. My boss once told me a story of FORTRAN programmers who would write a program that filled the screen and then would call a function just so the code did not go past one screen. :) I am a big fan of spaced out code for readability. I tend to use a lot of blank lines.
So, is it because you like it or is it just because it is the way that you have always done it?

I prefer the second one ;)
Well, there is another one! Hello!
This forum is too low on the page and I think everyone is missing it. Or they are ashamed as to why they use the first way. :)
I definitely prefer the first one. The matching argument doesn't hold, since you can always match the closing brace with the keyword. However I don't know why you would think vertical space doesn't matter. The more of the body of a function you can fit on your screen the easier it is to follow its logic.
The nesting levels argument doesn't hold either: both ways provide the same amount of indentation, so they are both very readable.
Now compare these two versions:
for (i){
for (j)
{
for (k)
{
if (z)
{
foo;
}
else
{
bar;
}
}
}
}
and
for (i) {for (j) {
for (k) {
if (z) {
foo;
} else {
bar;
}
}
}
}
The first example use 35% more vertical space with no benefit for readability of the code.
If you still feel the the first version is easier to read, try a bigger indentation in your editor with the second version (I typically use 8 spaces).
-Vartan
I prefer the first one, but only because I also have cases where there is no opening brace.
for (i) {for (j)
for (k) {
if (z) {
foo;
} else {
bar;
}
}
}
}
Oops, there's one too many }, which one do I delete?
It's much easier to spot now:
for (i)
{
for (j)
for (k)
{
if (z)
{
foo;
}
else
{
bar;
}
}
}
}
Maybe it's just dumb of me to sometimes leave out the braces, but I don't usually like them wrapping single lines.
Well, there we go, I think we should put it our coding standard to never leave out braces unless the body is a one-liner (and even that should be considered, especially for loops).
-Vartan
I prefer the first one. Second one makes me mad.
I usually brace as per the second case, except when the statement is a one liner in which case I ignore braces
I also tend to do things like:
if(true) foreach($a){
}
Because I feel that needless indentation is a waste of time and space.
Hence, overall my code tends to look like this:
if($a) echo 'Stuff';elseif($b)
sprintf('%s %s %s %s %s', $a, $b, $c, $d);
elseif($c) foreach($turnip as $t) echo 'Stuff';
else
{
echo 'Yes';
echo 'No';
}
I'm locking this thread. Conversations about century-old subjects like this one suck the life out of the community. If you really want to continue this discussion, please make a post - or do a search - on stackoverflow.com.