• Create Account

### #ActualL. Spiro

Posted 11 February 2013 - 04:43 PM

Clean your code by always using braces even if they are not required by the syntax.

Consistent code is clean code.
Omitting braces “just because you can” is inconsistent. It is a sign you seek to put forth the minimal effort allowed by the syntax rather than to just keep things the same everywhere.
It unnecessarily puts you or those maintaining your code later at risk of hard-to-spot bugs.

int iSum = 0;
for ( int i = 0; i < 20; ++i )
iSum += i;

“It works. Humm, but I want to see what the value of iSum is on each iteration.”

int iSum = 0;
for ( int i = 0; i < 20; ++i )
cout << iSum << endl;
iSum += i;

“What it always prints 0 and my algorithm broke too, WTF!?”

There is just no need for this kind of risk.
As mentioned already, clearer code is more important than compact code.

Omitting braces for the sake of syntactical exceptions just doesn’t make sense.

L. Spiro

### #1L. Spiro

Posted 10 February 2013 - 08:49 AM

Clean your code by always using braces even if they are not required by the syntax.

Consistent code is clean code.
Omitting braces “just because you can” is inconsistent. It is a sign you seek to put forth the minimal effort allowed by the syntax rather than to just keep things the same everywhere.
It unnecessarily puts you or those maintaining your code later at risk of hard-to-spot bugs.
int iSum = 0;
for ( int i = 0; i < 20; ++i )
iSum += i;
“It works. Humm, but I want to see what the value of iSum is on each iteration.”
int iSum = 0;
for ( int i = 0; i < 20; ++i )
cout << iSum << endl;
iSum += i;
“What it always prints 0 and my algorithm broke too, WTF!?”

There is just not need for this kind of risk.
As mentioned already, clearer code is more important than compact code.

Omitting braces for the sake of syntactical exceptions just doesn’t make sense.

L. Spiro

PARTNERS