locked
Omit curly braces or not? RRS feed

  • Question

  • Hello.

    Is any microsoft best practice recommendations about using curly braces in code?

    Should curly braces be used in single-statement condition?

    For example:

    if (condition)

    {

        only one statement;

    }

    Can curly braces be omitted in this code or it is not recommended?

    Please provide microsoft proof links if possible.

    Thank you.


    Ivan Gorbadey. Sharepoint 2010 MCPD & MCITP.

    Tuesday, April 24, 2012 5:59 PM

Answers

  • Hello,

    I'd use the curly brackets for every case because I simply find it more readable and consistent.

    Each developer/team with his/her/their own conventions but Stylecop default rule is enabled to warn you for such violation.

    Bottom line, you should use the convention that you like most and that is working for you, practices are nothing but guidance not set in stone.


    Eyal (http://shilony.net), Regards.



    • Edited by Eyal Solnik Wednesday, April 25, 2012 3:01 AM
    • Marked as answer by Ivan Gorbadei Wednesday, April 25, 2012 4:43 AM
    Wednesday, April 25, 2012 2:56 AM
  • From what I found, it is always wise to include curly braces even if its a single line condition.  Personally, I do it for readability.

    Link: http://blogs.msdn.com/b/brada/archive/2005/01/26/361363.aspx 

    • Marked as answer by Ivan Gorbadei Wednesday, April 25, 2012 4:44 AM
    Tuesday, April 24, 2012 6:10 PM

All replies

  • If you only have 1 line of code bellow if statement, you dont need brackets. This is event valid for sub if statement, like:

    if(condition 1)
        if(condition 2)
            if(conditon 3)
                 //some thing3
            else
                 //some thing else 3
        else
             //some else 2


    Mitja

    Tuesday, April 24, 2012 6:06 PM
  • From what I found, it is always wise to include curly braces even if its a single line condition.  Personally, I do it for readability.

    Link: http://blogs.msdn.com/b/brada/archive/2005/01/26/361363.aspx 

    • Marked as answer by Ivan Gorbadei Wednesday, April 25, 2012 4:44 AM
    Tuesday, April 24, 2012 6:10 PM
  • It is suggested that all control structures (if, while, for, etc.) use braces, but it is not required.

    Tuesday, April 24, 2012 6:11 PM
  • This is another example of not using curly paretnheses (not brackets actually):

    //VALID:
                int a = 1;
                int b = 2;
                int c = 3;
                if (a == 1)
                    if (b == 2)
                        if (c == 4)
                            Console.WriteLine("a is 1, b is 2 and c is 4");
                        else
                            Console.WriteLine("a is 1, b is 2 c is NOT 4");
                    else
                        Console.WriteLine("a is 1 and b is NOT 2");
                else
                    Console.WriteLine("a is NOt 1");

    There is something else important to say: you can create (instanitate or deifne) a new varible only inside parentheses, if you dont have them, you cannot do that. Example:

    //INVALID:
                int a = 1;
                if(a == 1)
                    int b =2;
    //VALID:
                int a = 1;
                if (a == 1)
                {
                    int b = 2;
                }


    Mitja

    Tuesday, April 24, 2012 6:12 PM
  • It is suggested that all control structures (if, while, for, etc.) use braces, but it is not required.
    NO need to, in some cases (explained above).

    Mitja

    Tuesday, April 24, 2012 6:13 PM
  • If you only have 1 line of code bellow if statement, you dont need brackets. This is event valid for sub if statement, like:

    if(condition 1)
        if(condition 2)
            if(conditon 3)
                 //some thing3
            else
                 //some thing else 3
        else
             //some else 2


    Mitja

    Mitja thank you for your answer.

    But my question was not about validity of code. The question is about maintainability and recommended practices.

    Do microsoft recommend to omit braces in such cases or not?

    There is a book where microsoft employee wrote:"AVOID omitting braces, even if the language allows it.". So my question: Is it really best practice to use braces always?


    Ivan Gorbadey. Sharepoint 2010 MCPD & MCITP.

    Tuesday, April 24, 2012 6:13 PM
  • Well, this is a matter of taste, everyone has an opinion.

    I tend to omit braces but only if the "single statement" fits on a line, in particular I never omit them if that statement is another control statement (if, for etc.).

    Tuesday, April 24, 2012 6:13 PM
  • As said, if you have only 1 row of code as your statement, there is no problem with omitting parentheses. If you have 2 or more lines of code you MUST have parentheses. This is intentially done (included in C#) becuase many times happenes that there is only 1 row of a statement - so writing parenthese is just not needed. And Microsoft (actually C# is very) glad with it. No harm done what so ever.

    Mitja

    Tuesday, April 24, 2012 6:21 PM
  • If there are 2 or more statements you have to use parentheses, otherwise there is will an runtime error, or 2nd line of code will jump under 1st if:

    //WITH PARENTHESES:
                int a = 1;
                int b = 2;
                if (a == 1)
                    if (a == 2)
                    {
                        Console.WriteLine("a is 1");
                        Console.WriteLine("a is 2");
                    }
    
    //NO PARENTHESES:
                int a = 1;
                int b = 2;
                if (a == 1)
                    if (a == 2)
                        Console.WriteLine("a is 1");
                Console.WriteLine("a is 2");


    Mitja

    Tuesday, April 24, 2012 6:25 PM
  • @Matja:

    These are parenthesis  ) (

    These are square brackets ] [

    These are curly brackets } {

    @the thread:

    An if statement, or a for/while/foreach/else/else if/try/catch/finally/anything else I forgot statement is always followed by exactly one statement.  The use of curly braces creates a single statements which means, "create a new scope and execute all of the statements contained within the braces."  Therefore they effectively turn 0, 1, 2, etc. statements into a single statement, allowing you to execute more than one statement inside of an if/for/whatever block.

    @OP:

    Some people find it easier to read if statements when curly braces are used, even when they aren't needed.  Some people don't.  This is generally a matter of preference, and there are not any strongly enforce conventions either way.  Some individuals feel strongly, but there is no general consensus among the C# community at large.


    • Edited by servy42 Tuesday, April 24, 2012 6:51 PM
    Tuesday, April 24, 2012 6:43 PM
  • There really isn't much of a benefit of doing it that way anyway.  If the premise is that you have a one line statement, it would likely not be an assignment. 

    Even if it was done like this:

    int a = 1;
    if (a == 1)
    {
       int b = 2; 
    }

    b goes out of scope after the last } and is no longer defined.

    So, there isn't much benefit here.

    Wednesday, April 25, 2012 12:17 AM
  • The main problem with out using the { }'s is where an else condition would take place.

    Consider:

    if (a == 1)
       if (b == 2)
         if (c == 3)
            Console.WriteLine("hello");
         else
            Console.WriteLine("world");

    Now where does the else condition apply? Is it for the if (a == 1), if (b == 2), or if (c == 3). It's a common problem language writter's need to deal with.

    For example, what if I wrote it like this?

    if (a == 1)
       if (b == 2)
         if (c == 3)
            Console.WriteLine("hello");
    else
       Console.WriteLine("world");

    It's easy to know it applies to if (c == 3) if you're very familar with the language., but it's easier to read it with curly braces to know where it goes.

    My rule of thumb is if I have nested ifs, I will always use curly braces.  If its a single if with one line, then I'll ignore the braces because I feel it's easier to read.

    It's all preference.  It's not that it's impossible.  It just can be harder to read in some cases.


    Wednesday, April 25, 2012 12:24 AM
  • Hello,

    I'd use the curly brackets for every case because I simply find it more readable and consistent.

    Each developer/team with his/her/their own conventions but Stylecop default rule is enabled to warn you for such violation.

    Bottom line, you should use the convention that you like most and that is working for you, practices are nothing but guidance not set in stone.


    Eyal (http://shilony.net), Regards.



    • Edited by Eyal Solnik Wednesday, April 25, 2012 3:01 AM
    • Marked as answer by Ivan Gorbadei Wednesday, April 25, 2012 4:43 AM
    Wednesday, April 25, 2012 2:56 AM
  • If I have several single lined IF statements in a row I omit the curly braces.  It looks nicer to me.

    If I have an IF/ELSE I always use braces.

    If it is only 1 or 2 IF statements I use the brackets.

    Easier to read code is the way to go.  Probably something you will figure out over time.

    Wednesday, April 25, 2012 3:16 AM
  • I tend to use the braces like this:

    if (condition) { i = 0; }

    Then, if I need to add more statements in the condition block, it is easier than having to add the {} later.


    Dan Randolph

    Wednesday, April 25, 2012 3:42 AM