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.
  2. Dismiss Notice

Curly Brackets in C#

Discussion in 'Scripting' started by tapir, Jun 18, 2014.

  1. tapir

    tapir

    Joined:
    Oct 24, 2012
    Posts:
    4
    Hello,

    I know this question can bring a lot of debate, but I just want to know if there is a proper answer when placing curly braces on C# on Unity scripts.

    Example 1
    Code (CSharp):
    1. class Something
    2. {
    3.     void Foo ()
    4.     {
    5.     }
    6. }
    Example 2
    Code (CSharp):
    1. class Something {
    2.     void Foo () {
    3.     }
    4. }
    Places that favor example 1.

    Places that favor example 2.

    • Unity documentation.
    • Unity creates new scripts like this.
    • Mono guidelines
    What is the actual Unity convention here?
    Thanks,
     
  2. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    In all honesty, there is no right way, I often mix and match both styles. If I'm dealing with properties then often I'll go with keeping the braces on the same line, whereas for methods and if-statements I'll put them on the next line.

    Code (csharp):
    1. // Automatic property, keep braces and content on same line.
    2. public int Health { get; set; }
    3.  
    4. // Waste of space.
    5. public int Health
    6. {
    7.     get;
    8.     set;
    9. }
    10.  
    11. // I prefer this way
    12. void DoSomething()
    13. {
    14. }
    15.  
    16. // But this is fine too
    17. void DoSomething() {
    18. }
    19.  
    20.  
    So the correct answer is to use both! Even alternate if you'd like :)
     
    Magiichan likes this.
  3. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    This is up to your team to decide.
     
  4. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    I let Ctrl+K Ctrl+F decide.

    ha
     
  5. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    Depends on what's easier to read when working in a team, but there is no right way to do it.
    Alternative: put all your classes on one line & don't worry about it xD
     
    GarthSmith and User340 like this.
  6. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
  7. mweldon

    mweldon

    Joined:
    Apr 19, 2010
    Posts:
    109
    The compiler will accept it either way, therefore technically either way is the right way.

    However, there is a preferred way and a not-preferred way. The preferred way is to follow the convention that your team has collectively agreed to, whatever it might be, and no matter how much you might disagree with it.
     
  8. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    It's a joke, that's probably the shortcut to auto format the code for him :'P
    sorry for going offtopic~
     
  9. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    Its auto format in visual studio ;)
     
  10. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    I see.
     
  11. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    I use ctrl + K ctrl + D, what's the difference? o3o
     
  12. TheSniperFan

    TheSniperFan

    Joined:
    Jul 18, 2013
    Posts:
    712
    The only right answer for this question is to choose one and stick with it.
    Just don't mix them up.
     
  13. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    What's wrong with mixing 'em up?
     
  14. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    Your choice formats document. Mine is selection...

    interestingly enough, the shortcut shown is Ctrl + E, Ctrl + D/F
     
    Magiichan likes this.
  15. TheSniperFan

    TheSniperFan

    Joined:
    Jul 18, 2013
    Posts:
    712
    The same thing as with not sticking to a naming convention or anything really.
    It's more likely that you confuse whoever wants to work with your code (yes, this can include you).

    If you make sure that the structure of your code is consistent (in itself and between files), it makes it much easier to understand, because once you understand the structure, you'll always know where to search.
     
  16. 3agle

    3agle

    Joined:
    Jul 9, 2012
    Posts:
    508
    There is no right or wrong answer.
    But Example 1 is the right answer.
     
  17. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    I agree except for auto-implemented properties or read-only properties that simply return a value.

    Code (csharp):
    1.  
    2. public int Thing { get; set; }
    3.  
    4. private int otherThing;
    5. public int OtherThing { get { return otherThing; } }
    6.  
    Oddly enough I've never read Mono's guidelines until just now. Maybe I'm weird but I don't like most of them and they seem to contradict the MS/.NET guidelines (what little there are).
     
  18. schragnasher

    schragnasher

    Joined:
    Oct 7, 2012
    Posts:
    117
    Code (csharp):
    1.  
    2. class Something {
    3.     void Foo () {  
    4.     }
    5. }
    6.  
    If you are not doing it this way your a bad person and hate puppies.
     
    GregMeach likes this.
  19. _met44

    _met44

    Joined:
    Jun 1, 2013
    Posts:
    633
    Haha this is such a troll-attractive topic :p

    Let me do my part of feeding then, schragnasher you are writting you C# like a web developer. :D
     
  20. TheSniperFan

    TheSniperFan

    Joined:
    Jul 18, 2013
    Posts:
    712
    It's something I never understood.
    Getting so emotional about this. How? Why? Who?
     
    GregMeach likes this.
  21. 3agle

    3agle

    Joined:
    Jul 9, 2012
    Posts:
    508
    I don't think anyone is actually taking this seriously, if that's what you are thinking. :)
     
  22. _met44

    _met44

    Joined:
    Jun 1, 2013
    Posts:
    633
    I would say it can get emotionnal because when you have to read someone's code that isn't written the way you do it, it's a lot longer and a pain in you know where. And it's espacially true when you spend a reasonnable amount of time to make your code perfect so it's eye candy at all times and place and you have to work with code from people who seem to not give a damn.

    But most of all, a moderated amount of trolling is fun and never hurts ;)
     
  23. GregMeach

    GregMeach

    Joined:
    Dec 5, 2012
    Posts:
    249
    I use both, sometimes the readability is better with 1 vs 2 but then sometimes 1 seems like a huge waste of white space.

    With either example, they are better with this line above them
    //This method/function does <this>

    PS What about one line IF statements use {} or not?
     
  24. 3agle

    3agle

    Joined:
    Jul 9, 2012
    Posts:
    508
    Logically it does come down to that tradeoff, and nothing else really makes sense as an argument.
    The decision is then do you save space, or make it readable?
    For me personally, it's the latter, every time. Having a longer file makes no difference, whereas someone finding it difficult to read your code does have an impact.