473,396 Members | 1,972 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 1195
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Stefan Gangefors | last post by:
I'm trying to figure out what I'm doing wrong when using ereg(). This is my regexp: ereg("^]$", "]"); and that does'n work, but this does: ereg("^$", "[");
8
by: Ken in Melbourne Australia | last post by:
If I use the curly bracket syntax (referred to as the complex syntax) within a string, how do I get to call a function within it? The php manual says that the first (or previous) character for...
3
by: Robert Mark Bram | last post by:
Howdy All! Is there any difference between referencing objects and attributes with dot notation or bracket notation? Example, document.formname.controlname document Can I access...
6
by: Jupiter5F | last post by:
You're probably going to tell me this is off topic but, I suspect my system is being invaded by other users using spying software etc.. I received a few emails where something clearly downloaded...
102
by: Skybuck Flying | last post by:
Sometime ago on the comp.lang.c, I saw a teacher's post asking why C compilers produce so many error messages as soon as a closing bracket is missing. The response was simply because the compiler...
2
by: Robert Hanson | last post by:
Hi All, I noticed that when I place the ending bracket on a function, the enclosed statements will bold for a short period of time. Is there a way to get that to occur again after you have...
3
by: dink | last post by:
helo, I'm new to VS 7.1, searched thru help but no luck. Anyone can tell me a keyboard shortcut to go to the ending bracket when cursor is under opening bracket and vice-versa. I think there...
1
by: Richard Thayne | last post by:
In VS.NET 2003 IDE is there a keyboard shortcut to go to the next bracket or find close bracket? Is there a way to do that and if so is there a website that shows all those keyboard shortcuts? ...
0
by: vaj | last post by:
Desktop Tower defense the most addictive flash game ever http://www.yelp.com/redir?url=http://www.teenwag.com/show/games
1
by: prodziedzic | last post by:
I want use scanf to read from input everything till right square bracket ']'. It seems to be something like that: scanf("%]) but that's not working. Any ideas?
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.