squirrely braces

May 20, 2014 at 4:00am
This is not really a programming question but a more a programming practice question. I was wondering which is the best practice:
1
2
3
4
 int main(){
  cout << "Hello World";
  return 0;
}

or
1
2
3
4
5
int main()
{
  cout << "Hello World";
  return 0;
}

Which is the best way to use the braces?
Is there any experienced programmers out there that can answer that?
What do you suggest?
May 20, 2014 at 4:48am
I've always seen the second version used. I would guess the reason is for clarity. If you see one brace, it's easy to look up or down and find its complement.
May 20, 2014 at 7:28am
It is not a programming practice question but a syle one.
Answer: There is several styles which is used by different programmers/projects. It is not important which you prefer, it is your ability to adhere to project style you are working on which is important.
May 20, 2014 at 8:42am
+1 for what miinipaa said. you can have your own personal style, but projects will require a certain way. my default example for this is webkit, which requires its code along that style
May 20, 2014 at 10:13am
Just another thing that I feel I should add on - just because you see people doing one version does not necessarily mean its better. In this case, it is just the style preferred by most C++ programmers and style guides. However, if you come from a different background than C++, you'll often see people using the braces in a different way, which was more natural and common to their original language.

For example, most experienced people I see on these forums use the second version, but I use the second version (if slightly neater):
1
2
3
4
int main() {
    std::cout << "Hello World!\n";
    return 0;
}
May 20, 2014 at 12:54pm
When you work on group project you may have to use a particular style. This is to make it easier for others in the same project to read your code and combine it together. The key thing here is to maintain consistency. Reading code that is not consistent and jumps to one style and another becomes more confusing than it needs to be. Easily read code is well written code.
Topic archived. No new replies allowed.