View Single Post
Kickaha
Veteran Member
 
Join Date: May 2004
 
2009-10-04, 20:49

Quote:
Originally Posted by Kraetos View Post
For one-liners? Why?

How is this:

Code:
if (foo) bar;
any less clear than this, which is ugly/awkward:

Code:
if (foo) { bar; }
or this, which is just a huge waste of space?

Code:
if (foo) { bar; }
Java lets bracketless one-liners slide for this reason.
So does C, and it's a huge source of bugs. Consider the above, in your first example. Six months later another dev comes along and adds a second statement to the if block:

Code:
if (foo) bar; andThis;
BZZT. Bug. "But he made a mistake, and it's obvious!" Yup. It is. But it's not when you have several nested ifs, and a smattering if #ifdefs coursing through it. In such cases, the developer that leaves off those brackets deserves every bit of office supplies chucked at their head.

Try this one on, and this one is trivial:

Code:
if (foo) if (bar) if (g) baz; else notBar;
There's a bug there. See it?

Make all blocks explicit, all the time. "Waste of space" is a complete red herring... what, the two bytes on disk for the brackets? You're worried about *two bytes*? Really?
  quote