# How do you prefer to nest code?

This topic is 2592 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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?

##### Share on other sites
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.

##### Share on other sites
[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.

##### Share on other sites

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.

##### Share on other sites
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.

##### Share on other sites
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.

##### Share on other sites
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.

##### Share on other sites

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.

##### Share on other sites
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).

##### Share on other sites
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 } 

• 22
• 10
• 19
• 15
• 14