Bose_geek
Bose_geek

Reputation: 498

Does putting less number of curly braces really helps?

What if do not use a curly braces after an if statement. Like :-

if(SomeCondition())
    return true;

Instead of

if(SomeCondition())
{
  return true;
}

Does it really help the compiler be it in terms of compilation time, better IL code generation, accquires less space or any other aspect (which at this point I am really not able to think of) ? Would like to know does it really help or is it just about code readability ?

Upvotes: 0

Views: 135

Answers (4)

Guffa
Guffa

Reputation: 700512

There is no difference at all in the generated code, at least not in release mode. In debug mode there might be some more nop instructions added, so that there is at least one instruction per line so that you can single step through the lines when you debug the code.

There is naturally a difference in compilation time, as the codes aren't identical. However, that difference is so small that you would need to have millions of lines of code before you can even notice the difference.

You should use the one that makes the code easier to maintain. If your code style causes you to do a mistake, just one mistake will take longer to fix than the total time that you could ever save in compilation.


Personally I prefer to always have the brackets there, even if there is a single statement in the code block. It's easier to read the code if it follows a single pattern instead of two different patterns depending on the situation.

Upvotes: 1

Amir Popovich
Amir Popovich

Reputation: 29846

They do the same.
The MSIL code will be almost the same in both cases if your'e in debug mode(Thanks Dirk).
In release mode the MSIL will be the same.

Here's a linqpad example(debug mode):

Example1:
C#:

void Main()
{
    bool a = true;
    int i;
    if(a)
     i = 17;  
}

IL:

IL_0001:  ldc.i4.1    
IL_0002:  stloc.0     // a
IL_0003:  ldloc.0     // a
IL_0004:  ldc.i4.0    
IL_0005:  ceq         
IL_0007:  stloc.2     // CS$4$0000
IL_0008:  ldloc.2     // CS$4$0000
IL_0009:  brtrue.s    IL_000E
IL_000B:  ldc.i4.s    11 
IL_000D:  stloc.1     // i

Example2:

C#:

void Main()
{
    bool a = true;
    int i;
    if(a)
    {
     i = 17;  
    }
}

IL:

IL_0001:  ldc.i4.1    
IL_0002:  stloc.0     // a
IL_0003:  ldloc.0     // a
IL_0004:  ldc.i4.0    
IL_0005:  ceq         
IL_0007:  stloc.2     // CS$4$0000
IL_0008:  ldloc.2     // CS$4$0000
IL_0009:  brtrue.s    IL_0010
IL_000B:  nop         
IL_000C:  ldc.i4.s    11 
IL_000E:  stloc.1     // i

Upvotes: 2

ConMan
ConMan

Reputation: 1652

They are both equivalent in terms of compilation time. It really comes down to a question of personal preference. Generally I like to include the braces for everything except return statements i.e. if(condition) { do something } else return x;

Upvotes: 0

Samuel
Samuel

Reputation: 6490

Well technically you are right as the lexer part of the tool chain does not need to parse those additional tokens but the difference is a couple of CPU cycles, so you won't notice this and I suggest to always use braces.

Edit: As others pointed out: The code will be the same. If you read the grammar specification for C# (and for some other languages like C, C++, Java) then you will see that if condition is followed by a statement, not by several statements. In order to be able to write several statements for the if condition you will need the curly braces. You are forced to use the braces when you will additionally log something or do something before the return.

For your situation the MSIL code IMHO may look the same, but this is not guaranteed. As the compiler is free to add NOP IL-commands whenever the compiler feels so. The semantic of your code however won't change.

Long story short: Your samples are semantically equivalent.

Upvotes: 0

Related Questions