DEV Community

Arman @programmerByDay
Arman @programmerByDay

Posted on • Originally published at programmerbyday.wordpress.com

Why You Should Always Use Curly Braces For Single Statement Blocks In C#

Some say it is a personal preference and it's not a big deal. I'd say it is not a personal preference anymore to omit curly braces for single statement blocks ... here is why:

A single statement is one line of code that usually sits below IfWhile or For commands.

if(string.IsNullOrEmpty(someStr))
   Console.WriteLine("someStr is empty. surprise!!!");
Enter fullscreen mode Exit fullscreen mode

I used to think if it is a one liner, it's cleaner to have it without curly braces. Even maybe put it in one nice and short line. After all, C# compiler allows it, who am I to disagree with that .. lol

I posted a while ago about how a newly joined developer affects how you should write code, we want to write the code in a way that it is easily readable by every team member in future and it also creates a structure that prevents future mistakes. Over my past years, I learnt that always using curly braces is one of those structures.

Curly braces makes the block less error-prone to future modifications by others.

What if someone decides to comment out that one-line:

if(string.IsNullOrEmpty(someStr))
   Console.WriteLine("someStr is empty. surprise!!!");
Enter fullscreen mode Exit fullscreen mode

What if someone adds a new line and forgets to surround the block with curly braces:

if(string.IsNullOrEmpty(someStr))
   Console.WriteLine("someStr is empty. surprise!!!");
someStr = "Default string in case of being empty";
Enter fullscreen mode Exit fullscreen mode

Curly braces make the code more organised in case of multiple if..else statements

if(varA > varB)
   Console.WriteLine("varA is bigger");
if(varA == varB)
{
   logger.Log("we got a match");
   CalculateFee(varA, varB);
}
if(varA < varB)
{
   var result = fetchItemsDatabase(varB);
   if(result is null)
      break;
   if(result.Length > 0)
      foreach(var item in result)
         Console.WriteLine(item);
   if(result.Length == 100)
   {
      logger.Log("Celebrating 100");
      Celebrate100();
   }
}
Enter fullscreen mode Exit fullscreen mode

You see? It puts cognitive mental load in order to separate each block of code. God forbids, if indentation was not done right, it will get even worse and becomes a nightmare to maintain this code!

There is not much value for a single line block anymore

When this feature was introduced (perhaps in C/C++ compiler), I believe it was to save some space and shrink the code to make it more readable. Back then, functions and code blocks were quite lengthy and convoluted.

It is not the case anymore though. We now break down logics into small manageable pieces, the storage is cheap, compilers can handle millions of files in a project and most developers use multiple big screens to read the code. So there is no point in shrinking the code and saving some space by not using curly braces.

Takeaway

Use curly braces always 🙂

Top comments (4)

Collapse
 
mileswatson profile image
Miles Watson

I would argue that not using curly braces is fine, as long as you keep the condition and action on the same (short) line. For example, doing a quick check then returning does not need 4 lines when it should only take 1.

If the condition and action are too long to fit on one line, then curly braces should be used for clarity.

Collapse
 
programmerbyday profile image
Arman @programmerByDay

Thanks,
But I think in 2020 we don’t need to be sensitive about number of lines of the code anymore.
Being future-proof and having low maintenance cost that helps code quality is a little higher in priority.

Collapse
 
mileswatson profile image
Miles Watson

One line statements have less visual clutter, so they can arguably be more maintainable than unnecessarily adding curly brackets everywhere.

Do you really need brackets around “if (x < 0) break;” ?

Thread Thread
 
programmerbyday profile image
Arman @programmerByDay

Yeah, That's right it is less visual cluttering.
But I think the other benefits of having brackets (as I mentioned in the post) outperforms that.
I'm all about building a structure in the code to decrease the chance of future mistakes or bugs.