LAME Question of the Day: Role of Curly Braces {} in Our Code?

Introduction

Last night, I received a text from one of my geek friends about the people's behavior and I tried to explain the text using the following image:

               "There are two types of people in this world"

                         if condition

I have experienced both types in several articles, blog posts, forums and I'm sure all of you too. I laughed for a while and imagined which one is better. Is Type One better than Type Two? There are times when such questions come to your mind, so I started Googling and was surprised by the answers posted on such questions. Some of the common answers are “This is a stupid question”, "This is a very basic question" and so on. Even some of the authors used it as "Stupid question of the day" on their blogs.

In my opinion, there are no stupid questions. There is only a difference between the level of knowledge about the subject among people. So, I though it would be fun to blog a series of such questions. I started writing about it and when naming the title of the series, I thought the word “Stupid” is a little brutal; so I come up with "LAME (Let's Ask Most Escaped) Questions". Such type of questions always come to mind but somehow you hesitate to ask them or ditch them, so LAME is the description I chose for such questions. Although, I'm looking for other alternatives as well, feel free to post your suggestions in the comments section.

Question of the Day

Our lame question of the day is: Should we use curly braces {} for if/if-else statements or not? Out of the given two approaches below, which one to prefer when coding?

Coding Style 1:

  1. if(Condition)      
  2.   Statement;   
Coding Style 2:
  1. if(Condition)      
  2. {      
  3.      Statement;      
  4. }  
Analysis:

The question is pretty simple and experienced by all of us but I never paid attention to it. Both of the preceding two approaches are syntactically correct and are widely used. So the question arises, which one is the most preferred approach and should be used when writing code? To better understand the question, let's break the question into several cases and then decide which one to choose that saves you from unexpected blunders.

Case 1: If you frequently test your code

Consider a case where you are reviewing your code and temporarily comment out several lines to debug something. If you use the first coding style, it is generally more prone to errors than the second one. Take an example where I'm displaying a message about the status of Page Load and also have some other lines of code in my program. According to the preceding two approaches, my code should be:

Approach I:
  1. if(!IsPostBack)      
  2. Response.Write("This page is not posted back yet.");    
  3. //Statement to be executed based on if condition   
  4.     
  5. Response.Write("Some other code");   
Approach II:
  1. if (!IsPostBack)      
  2. {      
  3.       Response.Write("This page is not posted back yet.");    
  4.       //Statement to be executed based on if condition     
  5. }     
  6. Response.Write("Some other code");  
When debugging the code, if I accidentally comment out the second line (statement to be executed after the if condition), the first approach considers the adjacent line as a statement for the if condition and executes it whereas in the second approach I thought that I didn't provided any statement to execute and proceeds to the next line without taking it as the if statement. So it is clear that although both the approaches are syntactically correct, the second approach is less prone to errors and should be used.

Case 2: If you want to execute multiple statements

If your code must execute multiple statements based on the if condition, then it is only possible using the second approach. Consider the following code:

Approach I:
  1. if(!IsPostBack)        
  2. Response.Write("This page is not posted back yet.");  
  3. //Only this statement can execute for if condition   
  4.       
  5. Response.Write("Some other code");    
  6. Response.Write("Some other code");     
Approach II:
  1. if(!IsPostBack)    
  2. {            
  3.       Response.Write("This page is not posted back yet.");       
  4.       Response.Write("Some other code");        
  5.       Response.Write("Some other code");       
  6. }    
  7.      
  8. //All the three statements can execute for if condition    
Case 3: If you believe in coding standards and clean code.

If you write your code using recommended coding standards and are a big fan of Robert C Martin (Uncle Bob), then you should always use curly braces when writing your if statements, even if they are not necessary.

An interesting fact is that even the default setting of Visual Studio follows the same coding convention. If you want to auto-generate the if statement in Visual Studio, just type if and press the Tab Key twice and Visual Studio creates the statement for you like this:
  1. if (true)    
  2. {    
  3.                          
  4. }  
Case 4: Real-world consequences of parameterless coding.

How many of you are Apple fans and are using iPhone? I hope many of you are, since I'm a big fan of Apple products and are using an iPhone. If you are currently using iOS 7 or have used it in the past then I'm sure you've heard about the Apple's SSL/TLS bug that leads the Apple devices to fail in verifying the authentication of the connection between SSL layers.

The point of discussing this topic here is that the cause of this bug is the accidental addition of just a single extra line of code in the security program that leaps over the actual security check code and authenticates the user without verifying the details. There was a lot of discussion about the cause of the bug and some thoughts exist about the coding style used in the code. There is a very useful article that explains the importance of using curly braces that might prevent the SSL bug from getting into action. Read here [^].

Conclusion

The conclusion from all the preceding cases points out that the use of curly braces, even for single lines, prevents you from accidental crashes and errors in your code. It is recommended and considered a good practice to use curly braces. I hope this blog post helps you to understand the use of curly braces {} in your coding life. Your feedback and constructive criticism is always appreciated, keep it coming. Until then, try to put a ding in the universe!


Similar Articles