Jump to content

  • Log In with Google      Sign In   
  • Create Account


Coding Style: Curly Braces


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
73 replies to this topic

#61 Chad Smith   Members   -  Reputation: 1072

Like
1Likes
Like

Posted 19 April 2013 - 05:34 PM

Even though I said I have a hard time reading when the braces are on the same line in my original post when asking the question, I will say as long as the style STAYS consistent (and the style is actually possible for a human brain to read and understand in the first place lol) throughout the code base then I really don't have much problem reading it.  Much of a code style is how consistent it is.  Then again could it even be called a "style" if it is not consistent?

 

Cornstalks: don't know any languages in the Scheme family, though what the heck is defining a scope or what is a scope in that family of languages?



Sponsor:

#62 phantom   Moderators   -  Reputation: 6891

Like
0Likes
Like

Posted 19 April 2013 - 05:43 PM

How is that bad? People tend to scan 90% by indentation levels, and since bad indentation will screw up anyone in either style it is always necessary for indentation to be consistent.

Really?
90%
Citation needed.

I know that my scanning method does braces matching to establish extent of code blocks rather than relying on indentation to delimit which is why when people include the opening brace on the same line as the function/if/whatever my parsing speed drops noticeably while I am forced to scan whole lines to find the expected token.

Also...
void function(type param)
{
    // here is some code
    // here is some code
    {
       // here is some code
       // here is some code
    }
    // here is some code
    // here is some code
    {
       // here is some code
       // here is some code
    }
}
... separate line braces is consistent, spacing and visually, with scope introduction which is an idiom I use often and in that compact form.

So... yeah... 90%? Really? I call Bullstat.

#63 L. Spiro   Crossbones+   -  Reputation: 12878

Like
0Likes
Like

Posted 19 April 2013 - 06:08 PM

Really?
90%
Citation needed.
[…]
I know that my scanning method does braces matching to establish extent of code blocks rather than relying on indentation to delimit which is why when people include the opening brace on the same line as the function/if/whatever my parsing speed drops noticeably

How much does your scanning speed drop when indentation levels are off?


L. Spiro

Edited by L. Spiro, 19 April 2013 - 06:09 PM.

It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums

#64 Cornstalks   Crossbones+   -  Reputation: 6974

Like
1Likes
Like

Posted 19 April 2013 - 06:08 PM


I wonder what this thread would be like if people were talking about Racket/Scheme instead:

#lang racket
 
{define {foo bar}
  {if {= 5 bar}
      {display "It was 5\n"}
      {if {not {= 42 bar}}
          {display "It was something else\n"}
          {display "It was The Answer\n"}}}}
 
{foo 15}


o man, that's just insane.  is that actual regular syntax, or are you intentionally making it weird?


A little bit of both. Racket has the "feature" that it doesn't care about what kind of braces you use so long as the opening and closing match (so [] and {} and () are all the same). That code would normally be written using parentheses instead of curly braces, but I used curly braces to make it relevant to the thread.

Cornstalks: don't know any languages in the Scheme family, though what the heck is defining a scope or what is a scope in that family of languages?

Uhhh... the braces (which in normal code would be parentheses). In Racket/Scheme, the parentheses go before the function name, not after (so in C it's foo(), but in Racket it's (foo)). I won't go into the syntax too much, but there's sane scoping. It's just a weird syntax.

Just to show you what kind of horrible stuff you can do, you could write a simple program to print out "hi" a bunch of times, and instead of using the idiomatic parans, you can use non-idiomatic stuff:
(define (<>) (display "hi\n") <>)
[[[{{[{[{[{{(((({({({(({[[{{{((((<>))))}}}]]}))})})}))))}}]}]}]}}]]]
; prints ~32 lines of "hi"
So yeah... I don't care what kind of indentation you use in your C-style code, so long as you're consistent.

Edited by Cornstalks, 19 April 2013 - 06:35 PM.

[ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

#65 TheChubu   Crossbones+   -  Reputation: 3969

Like
0Likes
Like

Posted 19 April 2013 - 06:10 PM

There aren't many studies (or none at all) on code style nor even project management styles (agile, xtreme programming, etc) as far as I know...


"I AM ZE EMPRAH OPENGL 3.3 THE CORE, I DEMAND FROM THEE ZE SHADERZ AND MATRIXEZ"

 

My journals: dustArtemis ECS framework and Making a Terrain Generator


#66 0r0d   Members   -  Reputation: 802

Like
0Likes
Like

Posted 19 April 2013 - 06:13 PM

Consistency also has to do with how you format your code.  If you put your braces in one place part of the time, and in another the rest of the time, then you're not being consistent.

Luckily, I always put opening braces at the end of the line and closing braces on their own line, making me quite consistent.

You missed the point I was trying to make about consistency, but I'm not going to repeat myself.

 

before people starting getting offended by me criticizing their "preferred" style.

Basically I can just stop here since you completely missed the point.

No one really cares enough to be offended, and many people are on your side.
So why were you singled out?
Because those people took an “it is my opinion” approach while you simply stated “X style is best” and “Y style is bad”.

Except I never claimed that what I was saying was a "fact".  I don't have to begin every sentence with "It is my opinion that" for a rational person to understand that what I say is by default an opinion, unless that sentence goes something like "It is a fact that...".  See the difference?

 

This style is the best one.

>>The reason this style is bad

See?
All styles are a matter of opinion, so stop stating things as a matter of fact, or I will single you out harder next time. Trust me, I have been holding back in lieu of happy days and things going my way—getting married, getting a raise, getting back my motivation for my job (overcoming the phil67rpg disease) and technical work and my book, etc.


L. Spiro

 

What I "see" is that you conveniently quoted me out of context, forgetting to include the parts of what I said where I give reasons and examples to back up my claims.

 

Yes, the preference for different styles is a matter of opinion, but I wasn't talking about that. 

 

BTW I also find it really funny that you go off and start lecturing me about people's opinions, and then threaten me if I dare to give my opinion again.  Wow, the hypocrisy.   I'm also unclear what exactly you're threatening me with?  Please feel free to elaborate.


Edited by 0r0d, 19 April 2013 - 06:15 PM.


#67 mhagain   Crossbones+   -  Reputation: 7595

Like
0Likes
Like

Posted 19 April 2013 - 06:19 PM

The more I see this the more I come to realise that what I actually scan by is neither the braces nor the indentation, but the (mostly) blank line before and after.  Am I a whitespace nazi?  Probably.  Do I care overly much?  No.  Why am I still talking about this?  Because it's quite interesting to read arguments from a perspective that's not my own.

 

What makes all of this really silly is that everyone can just run code through their favourite formatter and get it the way they want, then do the same again to put it back to another way.  The very existence of formatters makes any enforcement of styles/standards fairly redundant I'd think.


It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.


#68 SiCrane   Moderators   -  Reputation: 9496

Like
0Likes
Like

Posted 19 April 2013 - 06:42 PM

Well the problem with automatic formatters is that they often don't deal well with indentation that isn't specifically related to scopes and braces. For example code like:
if ( (a && b) ||
     (c && d) ) {
  e = really_long_line +
      something_else +
      something_else_entirely;
}
Sometimes they'll preserve the spacing in second, fourth and fifth lines, sometime they won't. For example, the MSVC IDE will assume that you wanted the additional spacing on a multiple of the tab stop (or at least I think that's the logic behind how it mangles my code). And there's very few things more annoying than seeing a commit where most of the changes are just whitespace differences like when something is run through a bad formatter and then back. While I wish we were at a point where tools made this a moot point, I still just suck it up and work with whatever style the file was originally in.

#69 superman3275   Crossbones+   -  Reputation: 2011

Like
0Likes
Like

Posted 19 April 2013 - 10:13 PM

For the first 1-2 years of programming, I leaned toward the first style. Now, after learning a fair amount of Java and many other languages, I prefer the second style.

 

Granted, this is a personal preference and neither is inherently better, so this post will probably turn into a flame war.

 

Cheers :)!


I'm a game programmer and computer science ninja ph34r.png!

Here's my 2D RPG-Ish Platformer Programmed in Python + Pygame, with a Custom Level Editor and Rendering System!

 

Here's my Custom IDE / Debugger Programmed in Pure Python and Designed from the Ground Up for Programming Education!

Want to ask about Python, Flask, wxPython, Pygame, C++, HTML5, CSS3, Javascript, jQuery, C++, Vimscript, SFML 1.6 / 2.0, or anything else? Recruiting for a game development team and need a passionate programmer? Just want to talk about programming? Email me here:

hobohm.business@gmail.com

or Personal-Message me on here smile.png!


#70 Dwarf King   Crossbones+   -  Reputation: 1741

Like
0Likes
Like

Posted 21 April 2013 - 12:52 PM

Consistency also has to do with how you format your code.  If you put your braces in one place part of the time, and in another the rest of the time, then you're not being consistent.

Luckily, I always put opening braces at the end of the line and closing braces on their own line, making me quite consistent.

>>before people starting getting offended by me criticizing their "preferred" style.

Basically I can just stop here since you completely missed the point.

No one really cares enough to be offended, and many people are on your side.
So why were you singled out?
Because those people took an “it is my opinion” approach while you simply stated “X style is best” and “Y style is bad”.

This style is the best one.

The reason this style is bad

See?
All styles are a matter of opinion, so stop stating things as a matter of fact, or I will single you out harder next time. Trust me, I have been holding back in lieu of happy days and things going my way—getting married, getting a raise, getting back my motivation for my job (overcoming the phil67rpg disease) and technical work and my book, etc.


L. Spiro

 

Are you getting married? A great congratulation from me smile.png Always great to read some good news around the net. Oh back on topic...

 

Personally I prefer this style:

if(something)
{
    Something
}
else
{
  something
}

 

 

Like many other have showed. Good or bad style is not an issue for me. This style is just what makes my eyes happy smile.png


Edited by Dwarf King, 21 April 2013 - 12:52 PM.

"The only thing that interferes with my learning is my education"

Albert Einstein

"It is a miracle that curiosity survives formal education"

Albert Einstein

 


#71 froop   Members   -  Reputation: 636

Like
1Likes
Like

Posted 22 April 2013 - 09:34 AM

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.



#72 Phil123   Members   -  Reputation: 616

Like
0Likes
Like

Posted 24 April 2013 - 08:52 PM

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).


Edited by Phil123, 24 April 2013 - 08:53 PM.


#73 Tom KQT   Members   -  Reputation: 1539

Like
0Likes
Like

Posted 25 April 2013 - 01:53 AM

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++;

Edited by Tom KQT, 25 April 2013 - 01:54 AM.


#74 kunos   Crossbones+   -  Reputation: 2203

Like
0Likes
Like

Posted 25 April 2013 - 02:12 AM

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
Lead Programmer
TWITTER: @KunosStefano
AssettoCorsa - netKar PRO - Kunos Simulazioni




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS