it opens up a hole for a careless mistake if you are code refactoring or simply adding a second statement that you want to occur inside the "if".
Yeah, so don't do that. Add the extra braces when you need them.
I've been bitten by this issue more than once
I can honestly say I've never been bitten by this, and I wonder what's wrong with people who have (no offense!).
it's fewer lines of code and it's slightly cleaner to the eye.
That
is the positive benefit. Don't dismiss it. Fewer lines of code means less cognitive load, and that's a good thing.
That said, your second code sample would
not have agreed with my personal coding style, for the reasons that
canuckInOR gives: that last else-clause is no longer simple.
I also have the rule that if the 'else' needs the braces, then so does the 'if'. This looks unbalanced, for example:
if (0 == installResult)
Console.WriteLine("MSI Uninstalled.");
else
{
if (reportErrors)
Console.WriteLine("Error: Uninstallation failed. MSI Error code: {0}", installResult);
else
Console.WriteLine("MSI Exit Code: {0}", installResult);
}
On the other hand, I have no problem with this:
if (installResult != 0)
{
if (reportErrors)
Console.WriteLine("Error: Uninstallation failed. MSI Error code: {0}", installResult);
else
Console.WriteLine("MSI Exit Code: {0}", installResult);
}
else
Console.WriteLine("MSI Uninstalled.");
I
do have a problem with this:
Just ick. Get a better compiler and write it naturally: