Iris Classon
Iris Classon - In Love with Code

Stupid Question 219: In C# should you use braces on one line if-statements? And are one-liners on the same line considered a bad practice?

Now here are two questions I’ve had for a while, as I’ve seen both ways by good developers and I’m a bit unsure what the most popular rule is. If an if-statement only executes one line then you can omit the braces, I tend to do that as the braces add too much noise. I know some might argue that it might cause problems if you change the code later, I would argue that you would read the code before changing it and therefore you shouldn’t miss adding the braces. But it is absolutely a valid point, I might be the one being wrong here thinking ‘should we add things just in case?’. Look at these two examples, which one would you consider to be the best practice?

if_one

if_two And what about the one-liners? Such as this one:

if_three I’ve been avoiding them, although I might have used one here and there (I know consistency is key but I’m pretty sure that I’ve unknowingly been inconsistent a few times although I do my best not to do so). I read somewhere that we read better from top to bottom than left to right (in regards to line length). I’m very curious what you do and what you think :)

Comments

Leave a comment below, or by email.
Mike Reynolds
7/15/2013 6:31:00 PM
Hi Iris,

I used to not use curly braces for one-liners, but one day changed my style -- I find that having curly braces makes things clearer for developers that read your code later.

Mike 
Rod
7/15/2013 6:48:05 PM
Assuming common sense you could ignore the curly braces and inline the statement as long as is readable.

But is very well known that common sense is the less common of all the senses so to maximise the readability use the curly braces even though it increments the number of lines of code.

For instance, would you prefer to read a code like this?

var instance = booleanCondition ? (IObjectInterface) new ConcreteObjectA() : new ConcreateObjectB();

or would you prefer...

IObjectInterface instance = null;
if (booleanCondition) instance = new ConcreteObjectA();
else instance = new ConcreteObjectB();

or would you prefer

IObjectInterface instance = null;
if (booleanCondition)
{
   instance = new ConcreteObjectA();
}
else 
{
   instance = new ConcreteObjectB();
}

I would choose the last option because it would be less troublesome to explain to someone new to the code and it create standards for the rest of the developers working on the code. 
Ivan Zivkovic
7/15/2013 6:55:25 PM
My rule: no curly braces if it is only single statement in if and else, but if there is a curly brace on any side of if or else I add it for both.

I see with adding curly braces as equivalent of new sentence (not always needed). 
Sondre Naustdal
7/15/2013 10:29:02 PM
I find curly braces to be a natural definer for blocks of code, but I rarely need to have them on a line of their own. So my suggestion is like this:
If(condition){
    instance = ObjA
}else{
    instance = ObjB
} 
Pawel
7/15/2013 11:30:16 PM
Maybe should try Java way:
if (something) {
tralalala
}
else {
trolololo
} 
James Curran
7/15/2013 11:43:45 PM
I've been programming in curly-brace languages for 25 years now, and I STILL haven't been able to come up with a consistent rule on how to handle there, other than "Do whatever makes in easiest to read". 
Steve Fenton
7/16/2013 12:21:27 AM
Curly braces are never needed for your later self. They are really useful for those other people who need to change your code later on who maybe aren't concentrating.

I wrote about this after a colleague (who was a clever chap and competent programmer) caused a big error due to missing curly braces.

http://www.stevefenton.co.uk/Content/Blog/Date/201005/Blog/Always-Use-Those-Curly-Braces/ 
Yeurch
7/16/2013 12:35:43 AM
I tend to omit the braces personally ... I think the code indentation will always be good enough to prevent mistakes when going back to code later.
One exception is that I will always use braces on a single-line `if` if there is a multi-line `else` (or vice versa).  For example:
    if (foo == 1)
    {
        DoSomething();
    }
    else
    {
        DoSomethingElse();
        ThenSomethingElse();
    }

The if and the statement on one line ... yes, providing it's short and readable! 
Simon Colmer
7/16/2013 1:17:16 AM
I used to skip the braces for one-liners until I came to read some of my code and noticed how odd it looked jumping between braces and no braces if I have 2 statements within an IF statement (etc).

I now use curly braces for everything. I do prefer the standard JavaScript way of doing things (keeping the first brace on the same line of the IF statement) but VS always refactors it to the next line ;) 
Kunal Chowdhury
7/16/2013 1:36:47 AM
Generally I always prefer to put the one liner code inside a curly braces inside if statement. This is to make sure that, some other developers don't unnecessarily create problem. Also, it is very easy when you read it later (without confusion).

As you mentioned, this varies from developer to developer but, I just use it whenever I am using a single line inside if statement.

Regards,
Kunal 
Marc A. Brown
7/16/2013 5:52:55 AM
I will do this:
if(condition) DoSomething();

But this:
if(condition)
  DoSomething();
is bad because later on you'll add a statement:
if(condition)
  DoSomething();
  DoSomethingElse();
and wonder why it doesn't work right. 
Andy Dent
7/17/2013 4:40:27 PM
We require braces on one-liners to avoid error when people edit code. It does make it a lot easier to scan lots of code (dense C++ with a lot more lines per function). 

My personal bracing style is close to our current team style - we use opening braces on the same line. This only costs one vertical line compared to no braces but also has a very important safety feature - if someone deletes a conditional line it causes a compiler error.

ie: the following will still execute the block, if you comment our or accidentally remove the if statement1
// if (condition)
{
  do something
}

Safely - you can't remove the if statement.
if (condition) {
  do something
}

if (condition) {
  do something
} else {
  do something else
} 
Josh
7/18/2013 9:39:54 AM
You said it... "consistency is key"

When I am updating somebody's code, I stick to their convention. It is easier to read and refer to in the future.

For my own code, I use a new line with no curly brace. 
Iris Classon
7/19/2013 11:02:31 AM
Reply to: Josh
Consistency is hard, and as you said- I almost always adapt to the main dev style. 
DevJonny
7/25/2013 4:56:04 AM
Reply to: Sondre Naustdal
Welcome the the Java world Sondre! :) 
Justin
8/5/2013 7:12:12 AM
Late to the game, but I use two styles.

If I have the statement only consists of changing an indiviual object with NO else I will use an inline style:

if (!IsAdmin) return;  //skip the execution of the rest of the function

Otherwise it will always have curly braces around it

if(IsAdmin) {
    //Do something only an admin can
}
else {
    log("Unauthorized access", user.ToString());
    return;
} 


Last modified on 2013-07-15

comments powered by Disqus