A defense for bracket-less code

D

Don Taylor

Found in a style guide (http://www.artlogic.com/careers/styleguide.html)
-----------------------------------------------------------------------
Another case where "unnecessary" braces should be used is when writing
an empty while loop:

while (*p++ = *q++)
{
// this loop intentionally left empty...
}

instead of the form that is more commonly found:

while (*p++ = *q++);

By prohibiting this common loop format, we can easily check for cases
where legal (but wrong) code like:

int i = 0;
while (i++ < kLoopLimit);
{
myBuffer = 0;
}

performs a loop with no body, then executes the intended body of the
loop exactly once. Python programmers can stop chuckling now.
 
D

Dave Hansen

Found in a style guide (http://www.artlogic.com/careers/styleguide.html)
-----------------------------------------------------------------------
Another case where "unnecessary" braces should be used is when writing
an empty while loop:

while (*p++ = *q++)
{
// this loop intentionally left empty...
}

FWIW, I usually code this like

while (*p++ = *q++)
continue;
instead of the form that is more commonly found:

while (*p++ = *q++);

PC-lint picks this up (as well as the erroneous code in the elided
example), but will allow the "continue" form shown above.

[...]
loop exactly once. Python programmers can stop chuckling now.

Not really. It was mostly a lead-in to that last sentence. Problems
like this couldn't happen in Python. So it's an opportunity to get a
giggle at the expense of programmers using a language that gives you
enough rope to shoot yourself in the foot...

Regards,
-=Dave
 
E

Edward Elliott

Dave said:
Not really. It was mostly a lead-in to that last sentence. Problems
like this couldn't happen in Python. So it's an opportunity to get a
giggle at the expense of programmers using a language that gives you
enough rope to shoot yourself in the foot...

Which can be entirely avoided by making the braces mandatory rather than
optional. This is one thing perl got right:

while (foo); # parse error
while (foo) next; # parse error
while (foo) { next; } # ok
while (foo) { } # ok

And if you compile C++ without a lint checker, well, you're playing with
fire. :)
 
S

Stelios Xanthakis

Edward said:
Which can be entirely avoided by making the braces mandatory rather than
optional. This is one thing perl got right:

while (foo); # parse error
while (foo) next; # parse error
while (foo) { next; } # ok
while (foo) { } # ok

And if you compile C++ without a lint checker, well, you're playing with
fire. :)

Since the whitespaceless frontend seems relevant here, may I add that:
bugs happen. Such "statement bugs" are very easy to detect once you
see that something's going wrong. If only all the bugs were as simple
as statement bugs! But unfortunatelly, less than 0.1% of the bugs I've
encountered so far are statement bugs :(

Also, I think that perl does that because otherwise code like

if ($x) $y++ if $z; else $z--;

would be even more confusing :)


Stelios
 
E

Edward Elliott

Stelios said:
Also, I think that perl does that because otherwise code like

if ($x) $y++ if $z; else $z--;

would be even more confusing :)

With or without braces, that's not legal code. A one-line if can't be
followed by an else. The closest you can do is this:

$y++ if $z;
$z-- if !$z;

but that's two statements. Your example would need braces even if they were
optional.
 
C

Carl Banks

Edward said:
With or without braces, that's not legal code. A one-line if can't be
followed by an else.

Thus proving the original claim about it being confusing. :) I think
he intended it to parse this way:

if ($x) { $y++ if $z; } else { $z--; }


Carl Banks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top