469,923 Members | 1,785 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,923 developers. It's quick & easy.

A defense for bracket-less code

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[i] = 0;
}

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

Don.

Apr 26 '06 #1
7 1106
wrong group

Apr 26 '06 #2
On Wed, 26 Apr 2006 10:20:57 -0400 in comp.lang.python, Don Taylor
<no************@gmail.com> wrote:
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.
-----------------------------------------------------------------------
On 26 Apr 2006 07:54:38 -0700 in comp.lang.python,
"au**************@free.fr" <au**************@free.fr> wrote:
wrong group


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

--
Change is inevitable, progress is not.
Apr 26 '06 #3
au**************@free.fr wrote:
wrong group


why?

</F>

Apr 26 '06 #4
Dave Hansen wrote:
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. :)

Apr 26 '06 #5
Edward Elliott wrote:
Dave Hansen wrote:
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. :)


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
Apr 26 '06 #6
Stelios Xanthakis wrote:
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.

Apr 26 '06 #7
Edward Elliott wrote:
Stelios Xanthakis wrote:
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.


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

Apr 26 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Stefan Gangefors | last post: by
8 posts views Thread by Ken in Melbourne Australia | last post: by
3 posts views Thread by Robert Mark Bram | last post: by
6 posts views Thread by Jupiter5F | last post: by
1 post views Thread by Richard Thayne | last post: by
1 post views Thread by prodziedzic | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.