Click here to Skip to main content
15,881,092 members
Please Sign up or sign in to vote.
3.40/5 (2 votes)
See more:
This :
C#
if(condition)
{
  statement;
}

OR
C#
if(condition)
  statement;


I prefer 2nd option, in need technical details of any to select 1st option.
Posted
Comments
phil.o 23-Sep-13 9:12am    
Typical question which is not a technical issue but a matter of personal preference.
There is no right answer for this kind of question.
Viral Pandya 23-Sep-13 11:34am    
Yes Phil, I also feel the same. I wanted to know others opinion on this. It is all developer's approach towards coding. So it may be matter of coding pattern or standards created individuals.

Every computer language implements the concept of "scope" which is the execution context in which actions will be executed, and the context (namespace context) in which "objects" (variables, classes, etc.) exist, and are accessible. The semantics of how they do this vary widely.

In C#, an if statement defines the start of a logical "block" which consists of the code to be executed if the evaluation of the conditional expression results in the boolean value 'true.

The braces that enclose code to be executed define execution scope.

Whether you write:
// Kernighan and Ritchie style
if(condition)
{
     // do stuff
     // do more stuff
}

// 1TBS (one true bracing style)
if(condition){
     // do stuff
     // do more stuff
}
Is a matter of style, and convention [^]. It's also a matter of great meaning to some people who engage in almost "religious" wars over the formatting of curly braces, whether or not using tabs or spaces is better [^], etc.

C# allows you to omit the enclosing braces that define a logical block of code only in the case you wish to execute a single code statement.

People disagree about whether allowing a no-braces-enclosed special syntax for the case of a single statement is a "good thing."

I am among those who think it is not a "good thing," and that it's a very good idea to get in the habit of always using braces, and that this practice, in the long-run, will lead to more readable, and maintainable code, as well as helping one avoid trivial errors.

C# also allows you to use the "?:" pair of delimiters as a special if/then/else "short-cut:" [^]. I find that useful in certain cases.
 
Share this answer
 
v2
Comments
Viral Pandya 23-Sep-13 9:08am    
Thanks for detailed reply.
But here I would like to ask on explanation given that "if braces defines scope of that code to execute, then complier already compiles 'single if statement' without any error. So it is already defined in compiler itself to get scope of 'single statement if block'. So if it already recognizing scope by itself, then why to write braces specifically in single if statement?" I hope it makes sense.
Hem Raj Thakur 23-Sep-13 9:16am    
if(condition)
{
statement
}
this way is good for programming. because it is professional way. easy to use and easy to understand.
bbirajdar 24-Sep-13 1:32am    
Agree.. Compiler can understand anything as long as it compiles. The more important point is the other developer who reads your code should also be able to understand your code and maintain it...
BillWoodruff 23-Sep-13 9:16am    
Yes, Viral, you make sense ! The designers of the C# language made the decision to allow that "exemption" from enclosing a single statement to be evaluated as a result of an 'if statement's condition evaluated to 'true.

As one person mentioned above, there is the issue that if you want to add additional statements to the scope of your 'if block in the future, that you will need to add curly braces. In my experience I frequently want to redefine what is executed in an 'if or 'else block.

There is an issue of personal choice, and style here.

However, when you are working as a professional programmer, as part of a team, I think you'll find that developing the habit of regularly using braces, even when you don't absolutely have to, will be an asset.
Viral Pandya 23-Sep-13 9:22am    
Thanks Bill... It helps.
None is better, I'd rather go for the first option because it makes your code easier to read. But there's not reason to say either one is better or worse then the other.

Eduard
 
Share this answer
 
If its a single definitely you can go for 2nd but in case we have set of statement in that case we need to go for 1st.
So its matter of requirement. Need to do accordingly.
- SG
 
Share this answer
 
Quote:
The problem with the second version is that if you go back and add a second statement to the if or else clauses without remembering to add the curly braces, your code will break in unexpected and amusing ways.

Maintainability-wise, it's always smarter to use the fist form.

Source: Is it bad practice to use an if-statement without brackets?[^]

regards...:)
 
Share this answer
 
v5
Comments
Viral Pandya 23-Sep-13 8:29am    
I need technical details if any to select 1st option.
Dholakiya Ankit 23-Sep-13 8:54am    
i have given you technical details viral
Viral Pandya 23-Sep-13 9:54am    
The details you gave is conventional not technical. Thanks for your reply.
Dholakiya Ankit 24-Sep-13 0:35am    
Can you give me difference first bet conventional and technical ?
Viral Pandya 24-Sep-13 2:51am    
I wanted any technical reason to select if any, may be more related to compiling side. Conventional means the ways/rules, we have created for programming. Everyone has their own usual ways to do programming. I hope you understand.
Offcourse , First one defines execution scope .
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900