Coding Style: Curly Braces

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

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?

Advertisement

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.

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

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid


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.
[size=2][ 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 ]

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

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.

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.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

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.

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 !

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 !

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

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

Albert Einstein

"It is a miracle that curiosity survives formal education"

Albert Einstein

This topic is closed to new replies.

Advertisement