Which is the better syntax?

May 7, 2006

A colleague and I had a discussion the other day about the use of curly brackets in C# and when to use them. The use of curly brackets is of course a part of the C# syntax, but you can use them in various different ways. We discussed the two line if-statement, because that’s the only scenario we found us to be in doubt.

Here are two ways of writing the same if-statement with and without the use of curly brackets.

With curly brackets:

if (foo == "enabled")
{
   return true;
}

Without curly brackets:

if (foo == "enabled")
   return true;

We both come from the Visual Basic world where you don’t have curly brackets, and that’s probably why we had this discussion in the first place. The question is which approach is the best? I know it’s a stupid question and a very subjective one, but I’m interested in your opinion on this. What do you think?

* $4.95/month BlogEngine.net Hosting – Click Here!

Comments (5) -

 Lars
Lars
5/7/2006 9:15:25 PM #

I prefer the first one, because I think it gives a better overview.

In VB I always use and prefer "If-Then-End If" on 3 lines instead of just one. Again for better overview/reading code.

 Mads Kristensen
Mads Kristensen
5/8/2006 6:11:30 PM #

That's also what we have decided now, but there is something very simple and clean about the latter.

Jan Schreuder
Jan Schreuder
5/9/2006 6:04:48 PM #

For better reading, and it makes your code safer. WIth the curly brackets, you are sure what part of the code belongs to the if-statement. If you need to add another statement before the return true, you're always sure that the if statement is still correct.

I know this can lead to heated discussions Wink

 Torsten
Torsten
5/10/2006 11:54:00 AM #

Always use curly brackets Smile

I know the latter example give the same result, but for understanding the code, it gives a better over view.

Ex.
if (something)
  a=b;
  b=c;

it could look like b=c was also executed if something was true.

 Russ Painter
Russ Painter
6/13/2006 1:51:00 PM #

Alway use the brackets.

The reason is simple - That one line of code that needs to be conditionally executed works fine without them now.  BUT - adding a new feature or bug fix in the future you may need to add another line before or after it that is also conditional.  If you forget to add the brackets at this point, then only one of the lines will be executed and you're going to end up with a bug that could be very hard to track down.

The other thing is if you use versioning software - and you in fact remembered to put in the brackets - this one line change is now a 3 line change.

Save yourself the hassle in the future by just putting them in straight away.

if (SomethingChanged == true)
   SaveToLog()

becomes

if (SomethingChanged == true)
   SendEmailToAdmin();
   SaveToLog();

Comments are closed

About the author

Mads Kristensen

Mads Kristensen
Program Manager at the Microsoft Web Platform team and founder of BlogEngine.NET.

More...

Month List

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer’s view in any way.