Coding Style: Curly Braces

Started by
72 comments, last by kunos 10 years, 11 months ago

I always used to use the first style and hated the second style. But at work we're using the second style and guess what, I got used to it within minutes and it makes absolutely no difference to me.

Advertisement

I generally prefer things like this:


if (something)
    ;
else
    ;
 
if (something)
{
    if (somethingElse)
      ;
   else
      ;
}
 
for (int i = 0; i < someNumber; i++)
{
   if (something)
   {
      if (somethingElse)
         ;
      else
         ;
   }
}

But I would never:


if (something) {
    ;
}

As going back and checking to ensure all the braces are in their correct spot would be tiresome. It's far easier to double check when they're all vertically lined up. (Though this is my own preference. If I got paid to do it in some weird way, then so be it).

I use both styles, but I am still consistent in it, although it may sound weird smile.png

I use the following only for class definitions, functions definitions, namespaces and these sorts of "top level" blocks:


void myFunction() 
{
   int a = 1;
}

And the following for if, for, while and other similar blocks:


if (a == b) {
   a++;
   b--;
}

And btw, for one-line if blocks i usually use this:


if (a == b)
   a++;

I used to be pretty religious about this.. and I was following the style in the first example, I thought the code in the second style was impossible to follow quickly.

The I started to code in Go, that forces the second style on you, and after few days I was just as comfortable as I could.

So now my C++ code uses style 1 and my Go code uses style 2... style 2 generates a more "compact and intense" code imo... and I still stand that style 1 outlines the block logic better, but at the end of the day, it doesn't really matter.

I also enforce using curly brackets for 1 line blocks all the time.

Stefano Casillo
TWITTER: [twitter]KunosStefano[/twitter]
AssettoCorsa - netKar PRO - Kunos Simulazioni

This topic is closed to new replies.

Advertisement