The title says it all. I try to avoid curly braces whre I can but I have recently found a few issues which I wasn’t able to explain but using curly braces solved these issues. I also read once that recently Microsoft reccommends to use curly braces for each statement.
Does somebody have a more detailed explanation here and shall I better change my habbit, at least for Unity?
Here is one example roughtly out of my mind:
if(val1)
DoStuff();
if(prop == null) //Here I had a NullRefference exception
DoOtherStuff(prop);
I know no plausible situation where (prop == null) could cause a NullRefference exception.
Simply using curly braces solved the issue above.
I’m not aware of any issues in Unity with needing curly braces like in your example (with just a single line of code). I do it all the time without problems. Usually if you use them it’s just for clarity. It doesn’t hurt anything by adding them.
If curly braces fixed your problem, sounds like either a bug or something else in your code was doing something.
there are two basic reasons to always use curly braces.
it allows multi-line inclusion within an if statement
it shows the user the logical separation of code for understand-ability sake.
This being said, I am not a proponent of the double line if. (like you have it.)
My methodology works a little like this:
// single line if is always reflective to the reader
if(someVal == null) return;
// multi-line should always have curly braces.
if(someVal == null)
{
Debug.Log("someVal was not set");
return;
}
Now this is all personal preference. Using the single line with no return should be just as readable as multi-line with curly braces.
The way you are presenting it, the user does not know when the if ends.
// So did the original coder say... this:
if(val1){
DoStuff();
}
if(prop == null){
DoOtherStuff(prop);
}
// or create a bug when trying to write this:
if(val){
DoStuff();
if(prop == null){
DoOtherStuff(prop);
}
}
I never use braces for single lines because I absolutely hate it But it certainly is down to preference, there is no problem using them.
To me though, if you don’t use the braces then you should be in a habit of using the auto formatter since they kinda go hand in hand. That way if you do the single line if, and meant it to be a multi line if, the autoformatter would show you that the indentation is wrong and you can spot the problem
Like others have mentioned, it works as you’d expect.
In regards to changing your habit:
It’s most important to stay consistent.
I wouldn’t recommend to change your personal preference just for Unity, do what’s best for you or follow the conventions you’ve been given.
For example, I used to write one-line statements many years ago, nowadays I always use braces even for a simple return statement.
That makes me feel better and more comfortable (sounds stupid, I know), looks more consistent and aesthetic to me and and the overall code looks much more formatted (also my personal impression).
I was quite happy that I had changed early enough, since several projects at university and the company I’m working for demand it anyway, so currently I don’t have to think about any conventions at all.
I prefer omitting the curly braces but I typically use an empty line after the indented command to make it clear the scope is changing.
One argument I’ve heard for always using curly braces is to prevent silly mistakes where a programmer adds a second command to the if statement but forgets to add curly braces.
Regarding your Null Reference Exception, could it be related to Unity Blog ?
This is generally really good advice, but it’s also important to actively work to improve your coding practices, especially when it comes to programming defensively.
Many professionals recommend using braces whenever an if statement spans more than one line. In isolation, this is easy to read:
It sure looks like DoMoreStuff() will only be called if (val1) is true. And it’s easy to make this mistake because if your code looked like this:
if(val1)
{
DoStuff();
}
DoSomethingElse();
you’d normally just put your cursor at the end of DoStuff();, press Enter, and type DoMoreStuff();, which would correctly be inside the braces. But doing the same without the braces causes it to really be:
even though the indentation is misleading. In other words, always try to make your code easy to read and hard to break. It might be someone else modifying your code, or it might be yourself two years from now.
In one instance, I’ve caught code that looked like this:
if (foo)
DoSomething(); return;
I suspect it probably happened when some coder was doing some “clean up” and removed braces from what seemed like a one-liner, and didn’t pay attention to what was actually on that one line.
Moral of the story: Adding braces don’t hurt anything. Removing braces, you’d better pay attention.
Because no one’s made this explicitly clear: the “one line” itself doesn’t matter, C# does not care about line breaks most of the time. It’s “one statement” that matters. A statement is generally ended with a semicolon. If you have multiple statements on one line, it will break. Like BlackPete showed above.
One statement, which works properly even though it’s over multiple lines:
if (foo)
return
5
+
Something(
22);
One line with two statements, which will not work as expected:
Thanks for your inputs. I tried to find the code that caused the issue but couldn’t find it anymore.
Maybe it was just one of those compiler issues which I have a lot recently, where Unity will compile scripts but not really include all changes.
Well, BlackPete gave an explicit example of that, but only about 20 minutes before you, so I’ll assume you just missed it.
What I sometimes see is a perfectly good all-on-1-line, which then gets messed-up that way. Since it’s still all one line, it’s a little harder to spot the error (but once you see it, very obvious what was intended, so not all bad):
// semi-common acceptable style, works fine:
if(dogs<cats) dogs=cats;
// but oops!:
if(dogs<cats) Debug.Log("adding dogs"); dogs=cats;