The reasons you should always use curly braces

Hernan Garchtrom
2 min readSep 4, 2016

--

This has been a discussion since the dawn of time between developers. Should we use curly braces even when they aren’t needed? This question applies mostly to conditionals and loops that have only one instruction to process, even in that situation I think they’re a must, maybe even more necessary than when they are compulsory. ECMAScript 6 creators seem to agree with me, taking it even a step further creating a scope for variables that only exist between them.

I have some arguments in favor of always using them, even when they seem not to be needed, I think they are.

if(booleanExpression)
doSomething();

Now this expression is mostly right in almost any programming language. What would happen if I want to comment out the actions to be executed when the conditional is true?

// if(booleanExpression)
// doSomething();

That’s right, we need to comment out both lines, otherwise it won’t work. This can lead to issues when debugging when the developer only comments the execution and not the evaluation of the conditional. The same applies for uncommenting.

However, if we do the following:

if(booleanExpression) {
// doSomething();
}

We only need to comment whatever we have between the curly braces without worrying about the evaluation of the conditional.

if(booleanExpression)
doSomething();

To refactor that in order to add more functionality if the expression is true, we need to add the curly braces, it will not work otherwise.

if(booleanExpression) {
doSomething();
doSomethingElse();
}

That in a versioning environment can lead to conflicts where we don’t need them. Imagine if one developer needs to update the boolean expression evaluated and another needs to add functionality if that’s true. The second developer might not even be aware of the first’s change, and really does not need to be. If we have two pull request at the same time there, the second one to be merged will have conflicts and will need some extra work to be ready.

if(booleanExpression)
doSomething();

Something you as developer might find obvious, but I found in a lot of occasions, is that in the need to add functionality we might miss the need for them. It also applies for situations when merging branches have different indentation configuration and we might end up having something like this:

if(booleanExpression)
doSomething();
doSomethingElse();

With a quick read here, we can confuse that both functions will be executed only if the conditional is true. On a second read, we see that’s not the case as the correct indentation for that code. The following is the correct way to read that code.

if(booleanExpression)
doSomething();
doSomethingElse();

The last function is always executed no matter what. This can lead to serious issues when coding.

As you can see over the past examples, the missing curly braces can cause a lot of issues, that can easily be prevented by the use of them. They also improve the readability of the code, making it clear where something starts and something ends.

--

--