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"

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:
- if(Condition)
- Statement;
- if(Condition)
- {
- Statement;
- }
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:
- if(!IsPostBack)
- Response.Write("This page is not posted back yet.");
- //Statement to be executed based on if condition
- Response.Write("Some other code");
- if (!IsPostBack)
- {
- Response.Write("This page is not posted back yet.");
- //Statement to be executed based on if condition
- }
- Response.Write("Some other code");
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:
- if(!IsPostBack)
- Response.Write("This page is not posted back yet.");
- //Only this statement can execute for if condition
- Response.Write("Some other code");
- Response.Write("Some other code");
- if(!IsPostBack)
- {
- Response.Write("This page is not posted back yet.");
- Response.Write("Some other code");
- Response.Write("Some other code");
- }
- //All the three statements can execute for if condition
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:
- if (true)
- {
- }
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!

SubashPosted Sep 22, 2016, 12:56 AM
Thank you bro useful tip
Abhishek AroraPosted Jan 28, 2015, 11:00 PM
Sahil Sharma sir nicely explained. Looking forward for more interesting topics like this..
Ibrahim RashidPosted Dec 19, 2014, 9:18 AM
Partly acceptable or partly non-acceptable. Sahil , You have nice way of creating an article interesting,Keep writing. But at end Experience matters. Sam Sir,you input is acceptable.
Sahil SharmaPosted Nov 3, 2014, 1:00 AM
Sam Hobbs Thanks for the review and suggestions about the title. I think "useless" will do more damage than "stupid", so the choice would be between philosophical & theoretical. You put an interesting point about number of people working on the code . In my opinion, if a developer is placed to an existing project or maintenance project and the project follows parameter less standards, he cannot simply inject parameter standards to the code or change the standards. He has to stick with the existing standards. But if the team is starting a new project, then it should be discussed which standard to use for coding.
Guest UserPosted Nov 2, 2014, 7:45 PM
:) good one
Sam HobbsPosted Nov 1, 2014, 4:50 PM
Also note that your sample code does not always use the indentation that is commonly accepted and used. Use of indentation can help to avoid errors described here. In my opinion, the main disadvantage of using blocks (curly braces) for single lines of code is that it takes up space. I prefer to have as much of the code visible as possible, but that is less important now that monitors are so much larger. I think one consideration is whether the code is to be written and maintained by one person or many people. If many people then it helps to have a standard. The standard depends on the experience of the developers. If the shop employs mostly beginners then it is more appropriate to require use of blocks (curly braces) always.
Sam HobbsPosted Nov 1, 2014, 4:45 PM
I also suggest that (at least some of the time) "block" should be said instead of "curly braces" since that is what you are actually talking about, see: http://msdn.microsoft.com/en-us/library/aa664737(v=vs.71).aspx
Sam HobbsPosted Nov 1, 2014, 4:40 PM
At least this article is interesting. My suggestion would be to say "useless" instead of "stupid". Or perhaps you could call them theoretical or philosophical or something like that. People that answer questions in forums often (usually) do not like theoretical questions.