How do you prefer to nest code?

Started by
18 comments, last by Splinter of Chaos 13 years ago
In Java, the standard convention seems to be to place a bracket in the same line as the control statement like this:

for(int i=0;i<5;i++) {

//do action

}

I think that this style of nesting is hard to read. For me, giving the starting bracket its own line makes it much easier to see which control structures a piece of code belongs to. Am I a minority in this view? If not, then why did the cluttered version nesting seem to gain so much traction?
-----------------------------Download my real time 3D RPG.
Advertisement
My personal preference is for:

xxx {
yyy {
zzz;
}
}


That's not however the amount of indentation I prefer, but the forum code tag insisted on 4.

As for why, it saves vertical space.
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
[quote name='ManaStone']
For me, giving the starting bracket its own line makes it much easierto see which control structures a piece of code belongs to.
[/quote]
I disagree - the control structure itself lets you know that there will be a block of code that follows it. Why waste a whole line to indicate the start of a block when the control structure (for/while/switch statement) already does so. The indentation level itself indicates which block you are in.

I disagree - the control structure itself lets you know that there will be a block of code that follows it.

A lot of people say that, but i find that it's much easier for my brain to groc two parallel brackets than match keywords to brackets. Personally, i think the argument of saving space is lacking when compared to arguments based on clarity. I rarely hear people say that it's more clear when the bracket doesn't have its own line.

Though, for very short blocks that aren't nested, i often don't give it its own line. I guess it's inconsistent of me, but a two-line block does seem more clear to me with that style.
Depends on the language, of course.

I like the following (Clojure):

(xxx (xxx xxx xxx)
(xxx
(xxx (xxx xxx xxx)
xxx
(xxx xxx)))
(xxx))

and (python)

xxx xxx xxx xxx:
xxx xxx xxx xxx:
xxx
xxx:
xxx


In C++, I generally follow the Java convention nowadays, except for the function scope:

xxx xxx ()
{
xxx (xxx) {
xxx();
}
}


I used to prefer putting the { on its own line, but nowadays I value my vertical screen space a lot lot more than the horizontal space, so I place it on the line with the control statement.
Just an FYI: Another way to save vertical space is to lower your font size as much as possible. In fact, i believe i read small fonts faster than large ones, so it's a win-win.
I think that most who work on large projects with thousands of lines of code start to realize that vertical space is valuable. For me, I find my vertical space is very valuable, so I don't see a need to waste vertical space. I use correct tabbing, so I don't need a curly brace to indicate a nested scope. Removing that extra space makes reading large amounts of easier.
Wisdom is knowing when to shut up, so try it.
--Game Development http://nolimitsdesigns.com: Reliable UDP library, Threading library, Math Library, UI Library. Take a look, its all free.

Just an FYI: Another way to save vertical space is to lower your font size as much as possible. In fact, i believe i read small fonts faster than large ones, so it's a win-win.


This does not work for me. I don't have terribly good eyesight. Hell, right now I am viewing gamedev.net with (Ctrl +)x4

Like smasherprog said, at least in my own code, I use very struct indention, which makes nesting a breeze to figure out at a glance.
I rarely ever scroll vertically in my projects even in huge files because i use the "Go to declaration", "Find references" for everything and i absolutely try to avoid to write functions that are to big. As a professor told once: "I cannot tell you if your routine has no errors, but i can tell you if it certainly has an error somewhere: Does the code have more than 50 lines?". Thats why i never use (and will use) the

xxx (yyy) {
}


style but always give the brace its own line and maybe also because i orientate myself more using the braces and not the keywords (my eyes find matching braces extremely fast).
It's all down to opinion, every side'll have positive things to say.

I prefer brackets on their own line. Makes it way easier to see where scopes end/start and match up. You don't have to look two or three times if there's a scope start at the end somewhere.

I even group if statements in logical groups per line like this:


if ( ( ( expression )
|| ( anotherExpression ) )
&& ( thirdExpression ) )
{
// do something
}

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

This topic is closed to new replies.

Advertisement