While following a number of Pluralsight courses by Jesse Liberty (which I highly recommend), I was reminded of the long-running debate about whether one should always use braces when control flow statements (conditionals and loops) are involved. Jesse Liberty is one of many who suggest always using braces. So let's say we have the following conditional:
if (x == 0)
Console.WriteLine("x is zero");
else if (x < 0)
Console.WriteLine("x is negative");
else if (x > 0)
Console.WriteLine("x is positive");
The proponents of the always-use-braces camp would have us write them like this:
if (x == 0)
{
Console.WriteLine("x is zero");
}
else if (x < 0)
{
Console.WriteLine("x is negative");
}
else if (x > 0)
{
Console.WriteLine("x is positive");
}
In this article, I'm going to discuss the advantages and disadvantages of this approach, as well as my personal opinion based on my experience. I want to say from the beginning that this article is subjective and not completely factual or objective - so take it for what it is. There is no general consensus on this matter precisely because it is a matter of personal taste.
Brace styles
The Java folks seem to like this kind of style:
if (x == 0) {
Console.WriteLine("x is zero");
}
The .NET camp, on the other hand, seems to prefer aligning braces vertically:
if (x == 0)
{
Console.WriteLine("x is zero");
}
If you have just one statement, you can technically leave out the braces, so you can write your code like this:
if (x == 0)
Console.WriteLine("x is zero");
...or like this.
if (x == 0) Console.WriteLine("x is zero");
Personally, I think the first and last options aren't the best in terms of readability (especially when code becomes complex) so I'll focus on the second and third. I normally use the second option (with braces) when I have multiple statemenents, and the third option (no braces) when I have just one. Many people recommend always using braces, and dismiss the third option as bad practice. Let's take a look at the reasons why.
If you need to add statements, you'll find the braces ready
if (x == 0)
Console.WriteLine("x is zero");
If you need to add additional statements to be executed as part of the conditional, the braces will have to be added anyway. Some people recommend always using braces so that you'll find them ready when you need to add additional statements.
In that case, I suppose, we shouldn't use empty element syntax in XML:
<RowDefinition Height="30" />
...and instead always write our tags in full:
<RowDefinition Height="30"></RowDefinition>
...simply because we might need to add something inside the element at some point. I think this is a very weak argument, because it disregards a language feature that may be very convenient, and at the same time bloats code with something that adds no meaning to the code. Take a look at the first two code snippets in this article - the one that uses braces is twice as long (in terms of lines) as the other one. And for what? Because people are too lazy to type in the braces when they are eventually needed? Wat.
Adding statements can be error-prone
if (x == 0)
Console.WriteLine("x is zero");
Then, you add an additional statement intended to be within the conditional, and you do it like this:
if (x == 0)
Console.WriteLine("x is zero");
Console.WriteLine(" which means it's neither positive nor negative");
Oops! The second Console.WriteLine() isn't actually part of the conditional, so it always gets executed, no matter what. This is a valid argument. But let's dissect it a little further.
First, let's start again from our simple single-line conditional:
if (x == 0)
Console.WriteLine("x is zero");
Now, if you want to add code at this point, you have two courses of action. If you want to add statements as part of the conditional, you know that there's just one statement and no braces, so adding them should be a pretty automatic response:
if (x == 0)
{
Console.WriteLine("x is zero");
Console.WriteLine(" which means it's neither positive nor negative");
}
On the other hand, if you want to add a statement that is not a part of the conditional, you add it at the same level as the if statement:
if (x == 0)
Console.WriteLine("x is zero");
Console.WriteLine(" which means it's neither positive nor negative");
Even in absence of braces, the indentation shows clearly that one statement belongs to the conditional and the other does not. So actually, when seeing this kind of code:
if (x == 0)
Console.WriteLine("x is zero");
Console.WriteLine(" which means it's neither positive nor negative");
...I can't help but think that the readability problem (which results in incorrect control flow) is one of indentation, not of whether to use braces or not.
I can certainly imagine beginning programmers making this kind of mistake, but I find it hard to believe that more seasoned programmers find it hard to read basic conditionals. As one of the answers to this question states:
"I even find it implausible that this should be a common mistake: blocks are a fundamental part of programming. Block level resolution and scoping is an automatic, ingrained mental process for programmers. The brain just does it (otherwise, reasoning about programming would be much harder). There is no additional mental effort required to remember putting the braces: the programmer also remembers to indent the newly added statement correctly, after all; so the programmer has already mentally processed that a block is involved." -- Konrad Rudolph
Also, one of the sections in this article states that:
"Programmers with enough discipline to always notice the braces (and put them in when needed) don't need this idiom [always using braces].
Summary
So find out what works best for you, and don't let anyone tell you how you should write your code based on subjective arguments. While you should definitely learn from more experienced programmers and best practices based on rational and logical arguments, be practical and don't get too religious about your code.
I find your argument persuasive, and suspect I was being overly prissy. -jesse
ReplyDelete