Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Standardized Formatting of Curly Braces?

Discussion in 'Getting Started' started by MeatballB, Aug 31, 2015.

  1. MeatballB

    MeatballB

    Joined:
    Apr 27, 2015
    Posts:
    10
    This may seems stupid / inconsequential, but I've got a bit of a question on the formatting of functions in Unity, specific the curly braces ( { } ).

    From what I've read, C# usually formats curly braces on separate lines:

    Code (C#):
    1. void Start ()
    2. {
    3.     // Code
    4. }
    Yet it seems by default Unity is throwing the first curly brace up on the first line of the function:

    Code (Unity/C#):
    1. void Start () {
    2.     // Code
    3. }
    Just wondering what's the standardized format?

    One other minor question, is it possible to force the MonoDevelop editor to automatically throw in the closing curly brace ( } ) when you type the opening curly brace ( { )?

    Thanks!
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,352
    There's no standard. Just be consistent with whatever you use.

    --Eric
     
  3. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,815
    Hoo boy, you're kicking open the potential for religious debate on this one.

    But I've been coding in C-derived languages since, well, pretty much since they were invented. ;) So I'll give you my $0.02 worth.

    The point of formatting source code is to reduce errors. Thus, coding standards should be adopted which make the code easier to read and harder to screw up. To a first approximation, nothing else matters.

    Therefore, the opening curly brace belongs at the end of a line. Why? Because it makes the code:
    1. Easier to read: few "noise" lines make the code quicker to scan, and easier to take in the "big picture". (This one is certainly debatable, but I find it to be true.)
    2. Harder to screw up: this is the strong argument here. Things on separate lines easily get separated. MonoDevelop even has an option-up/down arrow feature which moves entire lines up or down in the document. But even without that, it's easy to be deep in cogitating on something, decide you need to insert, and just make yourself some blank lines to do so, without thinking too much about what you're separating from what. You're thinking about the logical flow, sure, but maybe not thinking about syntax.
    So if your curly brace is on a separate line, you can far too easily insert something that silently changes the flow of your program. This is most easily seen with if statements. You had:

    Code (CSharp):
    1. if (freeble.brazzle == barnacle)
    2. {
    3.     Freplicate(gamma);
    4. }
    and then, after hastily inserting something while trying to keep your concentration fixed on a larger goal:


    Code (CSharp):
    1. if (freeble.brazzle == barnacle)
    2. Derasterize(fragnor);
    3. {
    4.     Freplicate(gamma);
    5. }
    Which still compiles fine, but no longer does what you intended (it freplicates gamma every time, regardless of the condition).

    Had the curly brace been positioned (ahem) properly, this would be much harder to screw up:

    Code (CSharp):
    1. if (freeble.brazzle == barnacle) {
    2.     Freplicate(gamma);
    3. }
    Now to screw it up in the same way, you have to carefully position the cursor between the ) and the {, which (unlike inserting blank lines) is not something you're likely to do accidentally.

    For the same reason, even worse than putting the curly braces on the next line, is not using them (on single-line blocks) at all:

    Code (CSharp):
    1. if (freeble.brazzle == barnacle)
    2.     Freplicate(gamma);
    Hiss! Bad programmer, no biscuit for you! Get off the island! I hope it's clear by now how easy it is to screw this one up, and silently break your code. Never, ever do this.

    However, by the same reasoning, a single-line if without curlies is fine if it's all on one line:

    Code (CSharp):
    1. if (freeble.brazzle == barnacle) Freplicate(gamma);
    This is easy to read and hard to screw up, for the same reason as a well-placed curly; to break it, you have to insert something in the middle of a line, which just doesn't happen accidentally.

    Now, all the same arguments apply to things like loops.

    What about functions and classes, then? Well in this case it really doesn't matter, because you can't insert code before the opening brace of a function or class and have it compile. The compiler will show you the error of your ways right quick. So, that one's purely a matter of taste. I tend to like the curly at the end of the line, just to be consistent, and so I don't have to think about whether to hit space or return before typing {. But by all the considerations that really matter, it doesn't matter. :)
     
  4. MeatballB

    MeatballB

    Joined:
    Apr 27, 2015
    Posts:
    10
    Ah thanks for the replies. I know anything about 'standards' for code formatting could quickly devolve into a flame war, but just wanted to get an understanding of what most folks do.

    Any idea if I can force MonoDevelop to autotype that closing brace when I type the opening brace? Or will I need to use Visual Studio or something like that for more advanced features?
     
  5. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,815
    I dunno... editors that insert closing punctuation when I type opening punctuation drive me up a tree, so I quickly find a way to turn it off and then forget about it. :)

    I do know that MonoDevelop has a ton of editor features (many of which I've turned off), though, so it's worth poking around in the preferences to see what you can find.
     
    jhocking likes this.
  6. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    8,692
    This.

    People have different preferences depending on style, but either is fine.
     
  7. MeatballB

    MeatballB

    Joined:
    Apr 27, 2015
    Posts:
    10
    Thanks again all.

    In case someone is searching in the future, there is a way to automatically add (or turn off) matching braces in MonoDevelop. Go to Tools -> Options -> Behavior (Under Text Editor) and there's a 'Insert matching brace' checkbox.
     
  8. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,269
    Do you have 150 icons on your desktop and 1,238 unread emails in your inbox? Well then, this formatting style is for you!
    Code (csharp):
    1. if (myPants.Contains(cat)) {
    2. file.report();
    3. }
     
  9. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    this
    {
    }

    Is always best.
     
    zombiegorilla likes this.
  10. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    VS is just flat out better. Wait for Unity 5.2 then just use VS CE and you will enter a golden age of actual competence with VS happily assisting you with
    {

    }
     
    Ryiah likes this.
  11. jhocking

    jhocking

    Joined:
    Nov 21, 2009
    Posts:
    812
    As a matter of fact I do, but I fail to see the connection :confused:
    :oops:
    :eek:
    :D
     
    LaneFox likes this.
  12. Martin_H

    Martin_H

    Joined:
    Jul 11, 2015
    Posts:
    4,421
    Was that irony? I honestly can't tell.
     
    VardenEE likes this.
  13. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,352
    (Off the record, JoeStrout is correct and anyone using a different style is obviously a drooling heretic. But we are accepting of drooling heretics here, so I would never say anything officially.)

    --Eric
     
    Ryiah, Kiwasi, jhocking and 1 other person like this.
  14. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Nah eric and joe are totally wrong - because I said so.
    {
    }
     
    Ryiah, zombiegorilla and JoeStrout like this.
  15. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,269
    It's all about the Choas format.

    Code (csharp):
    1.  
    2.         private void BlowYourMind(){
    3.             if (_MindNotBlown){
    4.                 BlowYourMind();
    5.                 else if (!_MindNotBlown){BlowYourMind();}}}
     
    JoeStrout likes this.
  16. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Far more important, be consistent with whatever style your boss uses. ;)

    I personally go for the same line approach. Mainly because I started using that approach and I don't see the point changing. Objectively speaking there is not much to pick between the two styles, nor is there any consistent approach from the community.

    And occasionally its appropriate to throw the whole darn thing on a single line. I tend to throw simple lambda expressions entirely on a single line.
     
  17. jgnmoose

    jgnmoose

    Joined:
    Apr 1, 2014
    Posts:
    44
    Code (csharp):
    1.  
    2. if (awesome) {
    3.     // write your braces like this
    4. }
    5.  
     
  18. Jaimi

    Jaimi

    Joined:
    Jan 10, 2009
    Posts:
    6,077
    Yes, there's a standard. There are several of them in fact. The most common is braces on the next line. this is because it's easier to read and see what they enclose - this is the microsoft standard. The second most common (and oldest) is opening brace on the same line. This is because it takes up the least space, so you were able to see more lines of actual code on an 80x24 terminal. This is the old ansi c standard.

    Choose whichever one you like. I prefer readability over compactness, so I choose the first.
     
    Ryiah likes this.
  19. jhocking

    jhocking

    Joined:
    Nov 21, 2009
    Posts:
    812
    It's hardly a choice of readability vs. compactness. "Braces on the next line" is only more readable to some people because they are used to that look; it's literally less readable to me because a brace with nothing in front of it means I need to look for whether it's a function or if statement or what. Yeah it's only a tiny pause, which is why it's not so much a choice based on any rational difference, so much as which appearance you're more used to.

    Ultimately I don't really care about bracketing; our code at work is routinely written both ways. The formatting thing that really bugs me out is inconsistent indenting; years ago I once worked with someone who didn't correct the indentation after pasting in code from elsewhere. !@#$!#%!@#$
     
    JoeStrout and Kiwasi like this.
  20. Jaimi

    Jaimi

    Joined:
    Jan 10, 2009
    Posts:
    6,077
    Those are the official reasons, take em or leave them. Of course whatever you're used to is more readable for you. I don't think anyone is advocating that you personally change your style.