accepted layout of code/curly brackets {

Just wondering what the general consensus is with things like this (as an example)...which is preferred in terms of layout/readability? I usually go with the first one.

1
2
3
4
5
6
7
8
9
10
11
for()
{
     for()
     {
          if()
          {
            //code
            //code
          }
     }
}


or this:

1
2
3
4
5
6
7
8
for(){
   for(){
      if(){
      //code
      //code
    }
  }
}


or any other option(s) completely different?

closed account (zb0S216C)
Definitely the first option. The second option just looks too messy.

Wazzak
I see a lot of examples that use option two.

Definitely option one for me, though.
Some people use both; one for functions, and the other for loops / decision trees, like below:

1
2
3
4
5
6
7
int main()
{
    for(...;...;...) {
        ...
        ...
    }
}
I prefer the first because the braces match up and you can easily see which scope is ending, which code belongs to what scope, etc. and it also gives some spacing in there so you can more easily separate code from condition.

The second makes it very very hard to tell with complex program scope structures.
Last edited on
The problem is that there are some well-known programmers that use the second style of coding which is very bad. And newcommers repeat this bad style very othen after these well-known programmers.

I advice to use the first style of programming. it makes much easy reading of code.
> Just wondering what the general consensus is with things like this

There is no general consensus; there are many different indent styles in wide use.
See: http://en.wikipedia.org/wiki/Indent_style

What is important is consistency - a code base should choose one indent style, and use it everywhere. Typically, the choice of indent style is not a decision made by each programmer independently. And the same programmer could be using different styles for different programs - for instance, BSD KNF for kernel code, and Allman (aka ANSI/ISO/BSD) for userland code.
Last edited on
The problem is that there are some well-known programmers that use the second style of coding which is very bad.


Why is it 'very bad'? Surely it is down to personal preference?

The key is to be consistent. Pick a style and stick with it. Or if you are joining an established team of developers working on the same product, adopt their style to ensure consistency and readability.

*edit - Ninja'd! :)
Last edited on
Why is it 'very bad'? Surely it is down to personal preference?

Because everything that is not as vlad wants it is very bad.
Last edited on
@L B
I prefer the first because the braces match up and you can easily see which scope is ending, which code belongs to what scope, etc. and it also gives some spacing in there so you can more easily separate code from condition.

The second makes it very very hard to tell with complex program scope structures.


Odd, I use the second style for the exact same reason. If I see a '}', I scroll up and see what word is at the same indentation. I know the word I find will be followed by a '{', so I don't see the point of being able to have it there visibly.

If I see a '{' at the same indentation, I'll expect it to be a purely data-wise encapsulation (in a long function when I want stack variables to destruct when no longer needed).
Topic archived. No new replies allowed.