patterncppCritical
What's the purpose of using braces (i.e. {}) for a single-line if or loop?
Viewed 0 times
linelooptheusingforsinglebracespurposewhat
Problem
I'm reading some lecture notes of my C++ lecturer and he wrote the following:
The 3rd technique is not clear to me: what would I gain by placing one line in
a
For example, take this weird code:
and replace it with:
What's the benefit of using the 1st version?
- Use Indentation // OK
- Never rely on operator precedence - Always use parentheses // OK
- Always use a { } block - even for a single line // not OK, why ???
- Const object on left side of comparison // OK
- Use unsigned for variables that are >= 0 // nice trick
- Set Pointer to NULL after deletion - Double delete protection // not bad
The 3rd technique is not clear to me: what would I gain by placing one line in
a
{ ... }? For example, take this weird code:
int j = 0;
for (int i = 0 ; i < 100 ; ++i)
{
if (i % 2 == 0)
{
j++;
}
}and replace it with:
int j = 0;
for (int i = 0 ; i < 100 ; ++i)
if (i % 2 == 0)
j++;What's the benefit of using the 1st version?
Solution
Let's attempt to also modify
Oh no! Coming from Python, this looks ok, but in fact it isn't, as it's equivalent to:
Of course, this is a silly mistake, but one that even an experienced programmer could make.
Another very good reason is pointed out in ta.speot.is's answer.
A third one I can think of is nested
Now, assume you now want to
which is obviously wrong, since the
Edit: Since this is getting some attention, I'll clarify my view. The question I was answering is:
What's the benefit of using the 1st version?
Which I have described. There are some benefits. But, IMO, "always" rules don't always apply. So I don't wholly support
Always use a { } block - even for a single line // not OK, why ???
I'm not saying always use a
i when we increment j:int j = 0;
for (int i = 0 ; i < 100 ; ++i)
if (i % 2 == 0)
j++;
i++;Oh no! Coming from Python, this looks ok, but in fact it isn't, as it's equivalent to:
int j = 0;
for (int i = 0 ; i < 100 ; ++i)
if (i % 2 == 0)
j++;
i++;Of course, this is a silly mistake, but one that even an experienced programmer could make.
Another very good reason is pointed out in ta.speot.is's answer.
A third one I can think of is nested
if's:if (cond1)
if (cond2)
doSomething();Now, assume you now want to
doSomethingElse() when cond1 is not met (new feature). So:if (cond1)
if (cond2)
doSomething();
else
doSomethingElse();which is obviously wrong, since the
else associates with the inner if.Edit: Since this is getting some attention, I'll clarify my view. The question I was answering is:
What's the benefit of using the 1st version?
Which I have described. There are some benefits. But, IMO, "always" rules don't always apply. So I don't wholly support
Always use a { } block - even for a single line // not OK, why ???
I'm not saying always use a
{} block. If it's a simple enough condition & behavior, don't. If you suspect someone might come in later & change your code to add functionality, do.Code Snippets
int j = 0;
for (int i = 0 ; i < 100 ; ++i)
if (i % 2 == 0)
j++;
i++;int j = 0;
for (int i = 0 ; i < 100 ; ++i)
if (i % 2 == 0)
j++;
i++;if (cond1)
if (cond2)
doSomething();if (cond1)
if (cond2)
doSomething();
else
doSomethingElse();Context
Stack Overflow Q#12193170, score: 526
Revisions (0)
No revisions yet.