{} or not

Pages: 12
closed account (EwCjE3v7)
What`s better? As in easer to read:

1
2
3
4
5
6
7
int main ()
{
   for (int i = 0; i <= 10; ++i) {
      cout << i << endl;
   }
   return 0;
}


or

1
2
3
4
5
6
7
8
int main ()
{
   for (int i = 0; i <= 10; ++i) 
   {
      cout << i << endl;
   }
   return 0;
}


or

1
2
3
4
5
6
int main ()
{
   for (int i = 0; i <= 10; ++i)
      cout << i << endl;
   return 0;
}
Last edited on
I prefer the second.
It gives me proper infos about the scopes without having to read up to the end of the line.

Sometimes I use the first one, but I can't tell why.
2.

First one mixes styles, big nono.

Last one has no braces on a one-liner, something else I'm against. If you add anything else, you'll need to add the braces anyways. Might as well have them now.
Linux kernel style is to /not/ use braces on one line statements unless they're nested. A space afterwards is required for clarity. In the case of nested control statements or blocks larger than one statement, style 1 above is used to reduce line count while keeping clarity.

Last edited on
closed account (EwCjE3v7)
Thank you, that helps. I need to choose a style, and I`m going for which gets voted best that`s the one I will go with. Currently in my book they use the first one when the scope has a lot in it, but used the third one if there just a line or two.
I prefer first one. I dislike (almost) empty line between condition and body. Sometime when I write one-time code to test something I use third one, but it is too easy to make a mistake which will not be easy to find.
closed account (EwCjE3v7)
So second one is winning? anyone else agree/disagree?
Second one is called Allman style. http://en.wikipedia.org/wiki/Indent_style#Allman_style
Code::Blocks come with included autoformatter in this style. You can try it out.

First one mixes styles, big nono.
It's a classic K&R style!
Second one. Though I would feel bad if I didn't mention that choosing what style to use based on what others like probably isn't the best way to go about it in my opinion.

If it is your choice on what style you can use (IE the codebase doesn't have a set style guideline in place) then choose whatever looks best to you and most importantly be consistent. Most programmers know how to read and write pretty much every common style in the book quite well so might as well choose one that you like the look of and then just be consistent with it.

Nothing makes the code more confusing then different styles plastered all over the same file (If you are working on a file that you didn't write try and use the same style that is already in use).

So my point is choose one that you like and that suits you, but be prepared to use and read other styles because you will have to specially on larger projects.
Last edited on
closed account (EwCjE3v7)
Thanks guys, I do like the first one as it saves lines and is a little more clear to me but I might have to start with the second one as I see loads using it and I should get myself used to it to.

Thanks guys
I got confused initially by looking at your second style:
1
2
3
4
   for (int i = 0; i <= 10; ++i) 
  {
      cout << i << endl;
   }


If you notice, the opening brace is not aligned with the closing brace. I got confused even more because so many people preferred this style!
You should definitely not choose a style based on what others use. All of these styles are still in rather heavy use anyways. You should either follow what your current project dictates (assuming it has rules laid out specific to the project), or use what you think is most clear.
closed account (EwCjE3v7)
Thanks you, I will use the first one
I prefer braces to always be on their own line, it's easier for my brain to process the way the code looks then. Otherwise there is too much variety and I cannot easily tell which code belongs to which blocks.
I've been using the #2 style. It makes it easier for me to match up opening and closing braces to make sure I'm not missing any.
@YellowPyrmid
The first two methods are fine. The last one you have to be careful with because if you don't have the braces it only executes the very next line after the for loop.
ResidentBiscuit wrote:
First one mixes styles, big nono.

Says who? Bjarne's latest edition of The C++ Programming Language has code like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
int main()
{
      if(/*condition*/){
            // statements
      }
      for(;;){
            // statements
            switch(){
            }
      }

      return 0;
}


As for the last option is only good if you want to print out a single statement:
1
2
3
4
5
6
7
8
9
10
11
12
for(int i = 0; i <= 10; ++i)
      std::cout << i << std::endl;      // only this is printed out if you exclude {}
      
       // this is considered to be outside the for loop 
       // and will only be printed after the loop is done...
       // or in this case will give an error because I'm 
       //accessing i out of scope
      if( i % 2 == 0){
            std::cout << i << " is even!" << std::endl;
      }else{
            std::cout << i << " is not even!" << std::endl;
      }
Last edited on
Worst style ever:
1
2
3
4
5
for(unsigned i = 0; i < 100; ++i)
    callFunction(),
    a = a + b,
    std::cout << "Hello" << std::endl,
    std::cin >> b;
closed account (N36fSL3A)
A bit off topic, but don't you guys hate it when you do something like:
1
2
3
4
if(stuff); // < Notice the semi colon
{
    // Stuff
}


And you spend hours trying to figure out what's wrong?
That's hard to miss:

clang says this:
test.cc:7:10: warning: if statement has empty body [-Wempty-body]
if(stuff); // < Notice the semi colon
         ^
test.cc:7:10: note: put the semicolon on a separate line to silence this warning
      [-Wempty-body]
1 warning generated.


gcc says this:
test.cc: In function 'int main()':
test.cc:7:10: warning: suggest braces around empty body in an 'if' statement [-Wempty-body]
Last edited on
closed account (N36fSL3A)
In Visual Studio it's caused quite a bit of issues.
Pages: 12