 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Jason Guest
|
Posted: Sat Jan 31, 2004 2:02 am Post subject: braces needed? |
|
|
Programming recently I was the habit of leaving out the braces on if
statements if the branch only applied to a single line, assuming it would
all work and be equivalent to "if(condition) { statement; }" I implemented
an algorithm and it did not work for a while and when i tracked the problem
down it was as if a certain action was not being done from one of these if
statements that did not have a brace, so I placed all if statements in
braces and it worked as expected. This was not a result of misunderstanding
on my part such as including 2 statements under the braceless if and I know
indentation is not important, the statements were usually all on one line
anyhow.
It appeared to work ok when I first started doing it, but not when there was
a few in a row or near each other inside while loops and such like. In some
parts of my code I have neglected to change them to braces without any
problems, confusing me.
I also sometimes feel the desire to omit braces from while and for loops
because i find them irritating and like to shorten my code. Am I able to
avoid braces or not? Is it guaranteed to work or do different compilers
produce different results? If I have code that works perfectly now and all
I do is remove braces from single statement if, for and while loops, is it
guaranteed to make no difference or am I naive to some other potential
problem?
thanks for clarifying.
|
|
| Back to top |
|
 |
David Harmon Guest
|
Posted: Sat Jan 31, 2004 3:28 am Post subject: Re: braces needed? |
|
|
On Sat, 31 Jan 2004 02:02:04 +0000 (UTC) in comp.lang.c++, "Jason"
<jason.carney1 (AT) btinternet (DOT) com> was alleged to have written:
| Quote: | Is it guaranteed to work or do different compilers
produce different results? If I have code that works perfectly now and all
I do is remove braces from single statement if, for and while loops, is it
guaranteed to make no difference or am I naive to some other potential
problem?
|
It is guaranteed to work the same on all compilers. This part of the
language syntax has been well defined from the very beginning of C, and
it would be a huge shock to find a compiler that got it wrong.
The thing controlled by an "if" or "while" is conceptually a single
statement. Multiple statements with braces around them are considered a
single "compound statement". If you want, you can put braces around a
group of statements that have nothing to do with any "if" or "while".
I prefer to omit redundant braces for an "if" or "while" that controls a
single statement, but there is nothing terribly wrong about using them
every time. Some people say it reduces the likelihood of error, and
based on your experience maybe it does. I find it nicer to read the
code without them, but of course you have to understand the language
rules.
|
|
| Back to top |
|
 |
Jason Guest
|
Posted: Sat Jan 31, 2004 3:54 am Post subject: Re: braces needed? |
|
|
"David Harmon" <source (AT) netcom (DOT) com> wrote
| Quote: | On Sat, 31 Jan 2004 02:02:04 +0000 (UTC) in comp.lang.c++, "Jason"
[email]jason.carney1 (AT) btinternet (DOT) com[/email]> was alleged to have written:
Is it guaranteed to work or do different compilers
produce different results? If I have code that works perfectly now and
all
I do is remove braces from single statement if, for and while loops, is
it
guaranteed to make no difference or am I naive to some other potential
problem?
It is guaranteed to work the same on all compilers. This part of the
language syntax has been well defined from the very beginning of C, and
it would be a huge shock to find a compiler that got it wrong.
The thing controlled by an "if" or "while" is conceptually a single
statement. Multiple statements with braces around them are considered a
single "compound statement". If you want, you can put braces around a
group of statements that have nothing to do with any "if" or "while".
I prefer to omit redundant braces for an "if" or "while" that controls a
single statement, but there is nothing terribly wrong about using them
every time. Some people say it reduces the likelihood of error, and
based on your experience maybe it does. I find it nicer to read the
code without them, but of course you have to understand the language
rules.
|
Thanks for the reply(grr @ dig:)). I've never looked at the syntax rules
for a programming language I've used, I've just used it, but now I'm
interested, any online references to the complete syntax of c++ so I can
understand all the language rules? all i get on google is fricking adverts
for books.
|
|
| Back to top |
|
 |
Jack Klein Guest
|
Posted: Sat Jan 31, 2004 4:35 am Post subject: Re: braces needed? |
|
|
On Sat, 31 Jan 2004 02:02:04 +0000 (UTC), "Jason"
<jason.carney1 (AT) btinternet (DOT) com> wrote in comp.lang.c++:
| Quote: | Programming recently I was the habit of leaving out the braces on if
statements if the branch only applied to a single line, assuming it would
all work and be equivalent to "if(condition) { statement; }" I implemented
an algorithm and it did not work for a while and when i tracked the problem
down it was as if a certain action was not being done from one of these if
statements that did not have a brace, so I placed all if statements in
braces and it worked as expected. This was not a result of misunderstanding
on my part such as including 2 statements under the braceless if and I know
indentation is not important, the statements were usually all on one line
anyhow.
It appeared to work ok when I first started doing it, but not when there was
a few in a row or near each other inside while loops and such like. In some
parts of my code I have neglected to change them to braces without any
problems, confusing me.
I also sometimes feel the desire to omit braces from while and for loops
because i find them irritating and like to shorten my code. Am I able to
avoid braces or not? Is it guaranteed to work or do different compilers
produce different results? If I have code that works perfectly now and all
I do is remove braces from single statement if, for and while loops, is it
guaranteed to make no difference or am I naive to some other potential
problem?
thanks for clarifying.
|
Without showing us some of the code that behaved incorrectly, and what
you changed it to to make it behave the way you wanted it to, it is
difficult to say for sure what went wrong. Most likely you had an
"else" or "elseif" matched up to the wrong "if".
I have never seen a professional coding standard that did not require
the braces around all controlled clauses, even if they were only a
single statement. Nor will there ever be one anyplace I work where I
have any measure of control over the coding standard.
There is just too much chance for error, not only when writing the
original code, but even more so later when the code is modified for
defect fixing or upgrade.
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
|
|
| Back to top |
|
 |
Gianni Mariani Guest
|
Posted: Sat Jan 31, 2004 5:23 am Post subject: Re: braces needed? |
|
|
Jason wrote:
| Quote: | "David Harmon" <source (AT) netcom (DOT) com> wrote in message
news:401d1d83.17745936 (AT) news (DOT) west.earthlink.net...
On Sat, 31 Jan 2004 02:02:04 +0000 (UTC) in comp.lang.c++, "Jason"
[email]jason.carney1 (AT) btinternet (DOT) com[/email]> was alleged to have written:
Is it guaranteed to work or do different compilers
produce different results? If I have code that works perfectly now and
all
I do is remove braces from single statement if, for and while loops, is
it
guaranteed to make no difference or am I naive to some other potential
problem?
It is guaranteed to work the same on all compilers. This part of the
language syntax has been well defined from the very beginning of C, and
it would be a huge shock to find a compiler that got it wrong.
The thing controlled by an "if" or "while" is conceptually a single
statement. Multiple statements with braces around them are considered a
single "compound statement". If you want, you can put braces around a
group of statements that have nothing to do with any "if" or "while".
I prefer to omit redundant braces for an "if" or "while" that controls a
|
I have a policy of allways having a brace. In fact, if I had a compiler
option that would refuse anything but compound statements on
if/else/while etc I would hardwire it on. - simply because that one
behaviour if it eliminates one instance of an error I could make is
worth the price - I've seen that error happen with multiple people more
than I can count.
| Quote: | single statement, but there is nothing terribly wrong about using them
every time. Some people say it reduces the likelihood of error, and
based on your experience maybe it does. I find it nicer to read the
code without them, but of course you have to understand the language
rules.
Thanks for the reply(grr @ dig:)). I've never looked at the syntax rules
for a programming language I've used, I've just used it, but now I'm
interested, any online references to the complete syntax of c++ so I can
understand all the language rules? all i get on google is fricking adverts
for books.
|
Occasionally it's good to buy books. Stroustrup's "The C++ Programming
Language" has a grammmer section which is an interesting reference.
Here's a google hit:
http://www.csci.csusb.edu/dick/c++std/cd2/gram.html
|
|
| Back to top |
|
 |
Jack Klein Guest
|
Posted: Sat Jan 31, 2004 5:32 am Post subject: Re: braces needed? |
|
|
On Fri, 30 Jan 2004 22:34:01 -0700, "Jonathan Turkanis"
<technews (AT) kangaroologic (DOT) com> wrote in comp.lang.c++:
| Quote: |
"Jack Klein" <jackklein (AT) spamcop (DOT) net> wrote in message:
I have never seen a professional coding standard that did not
require
the braces around all controlled clauses, even if they were only a
single statement. Nor will there ever be one anyplace I work where
I
have any measure of control over the coding standard.
There is just too much chance for error, not only when writing the
original code, but even more so later when the code is modified for
defect fixing or upgrade.
Certainly requiring braces seems to have wide acceptance, but your
statement is an exaggeration. Glancing quickly through the standard,
the Dinkumware standard library which ships with VC7.1, and the Boost
source, I find numerous uses of 'if' without braces. Generally the
meaning of such code is immediately clear.
Just serach for 'if (' in the standard.
Jonathan
|
The examples in the C and C++ standards are not, nor are they intended
to be, examples of a particularly robust coding style. Investigate
the literature and find the number of defects associated with the
practice of omitting braces.
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
|
|
| Back to top |
|
 |
Jonathan Turkanis Guest
|
Posted: Sat Jan 31, 2004 5:34 am Post subject: Re: braces needed? |
|
|
"Jack Klein" <jackklein (AT) spamcop (DOT) net> wrote in message:
| Quote: |
I have never seen a professional coding standard that did not
require
the braces around all controlled clauses, even if they were only a
single statement. Nor will there ever be one anyplace I work where
I
have any measure of control over the coding standard.
There is just too much chance for error, not only when writing the
original code, but even more so later when the code is modified for
defect fixing or upgrade.
|
Certainly requiring braces seems to have wide acceptance, but your
statement is an exaggeration. Glancing quickly through the standard,
the Dinkumware standard library which ships with VC7.1, and the Boost
source, I find numerous uses of 'if' without braces. Generally the
meaning of such code is immediately clear.
Just serach for 'if (' in the standard.
Jonathan
|
|
| Back to top |
|
 |
Jonathan Turkanis Guest
|
Posted: Sat Jan 31, 2004 5:52 am Post subject: Re: braces needed? |
|
|
"Jack Klein" <jackklein (AT) spamcop (DOT) net> wrote in message:
| Quote: | On Fri, 30 Jan 2004 22:34:01 -0700, "Jonathan Turkanis":
Certainly requiring braces seems to have wide acceptance, but your
statement is an exaggeration. Glancing quickly through the
standard,
the Dinkumware standard library which ships with VC7.1, and the
Boost
source, I find numerous uses of 'if' without braces. Generally the
meaning of such code is immediately clear.
Just serach for 'if (' in the standard.
Jonathan
The examples in the C and C++ standards are not, nor are they
intended
to be, examples of a particularly robust coding style. Investigate
the literature and find the number of defects associated with the
practice of omitting braces.
|
That's an interesting response. You seemed to claiming general
acceptance of the pratice of including braces everywhere. (Maybe I
misinterpretted you.) Notice that I did not disagree that this is a
wise practice. I simply offered some evidence that the practive is not
nearly as widespread as you implied, even in code from very reputable
sources.
Now you tell me to 'investigate the literature'. You're the one making
the argument -- it's your responsibility to provide evidence for your
position.
Of course, the examples from the standard are not intended to be
examples of robust coding style. But certainly they are not examples
of bad style (excpet for those flagged as errors).
Jonathan
|
|
| Back to top |
|
 |
Jason Guest
|
Posted: Sat Jan 31, 2004 5:57 am Post subject: Re: braces needed? |
|
|
">
| Quote: | Occasionally it's good to buy books. Stroustrup's "The C++ Programming
Language" has a grammmer section which is an interesting reference.
thanks for the tip, I wish I had bought that book recently in preference to |
the C++ Standard Library by Josuttis, which I have hardly needed to use. I
guess i can read about iterators and templates when i feel like it, but I
dont find it very helpful as I only ever used vector, string and few stream
objects from the STL. The only reason I did not buy Stroustrup was because
I have experience of reading K & R for the c language and assumed it would
be similar in style ie a little bit on the formal side and rather dull. I
suppose I will buy it anyway at some point.
thanks again.
|
|
| Back to top |
|
 |
David Harmon Guest
|
Posted: Sat Jan 31, 2004 7:06 am Post subject: Re: braces needed? |
|
|
On 31 Jan 2004 00:23:55 EST in comp.lang.c++, Gianni Mariani
<gi2nospam (AT) mariani (DOT) ws> was alleged to have written:
| Quote: | I have a policy of allways having a brace. In fact, if I had a compiler
option that would refuse anything but compound statements on
if/else/while etc I would hardwire it on.
|
I would expect that almost any "lint" type product would allow you to
check for that.
|
|
| Back to top |
|
 |
Nick Hounsome Guest
|
Posted: Sat Jan 31, 2004 3:25 pm Post subject: Re: braces needed? |
|
|
"Jason" <jason.carney1 (AT) btinternet (DOT) com> wrote
| Quote: | Programming recently I was the habit of leaving out the braces on if
statements if the branch only applied to a single line, assuming it would
all work and be equivalent to "if(condition) { statement; }" I
implemented
an algorithm and it did not work for a while and when i tracked the
problem
down it was as if a certain action was not being done from one of these if
statements that did not have a brace, so I placed all if statements in
braces and it worked as expected. This was not a result of
misunderstanding
on my part such as including 2 statements under the braceless if and I
know
indentation is not important, the statements were usually all on one line
anyhow.
It appeared to work ok when I first started doing it, but not when there
was
a few in a row or near each other inside while loops and such like. In
some
parts of my code I have neglected to change them to braces without any
problems, confusing me.
I also sometimes feel the desire to omit braces from while and for loops
because i find them irritating and like to shorten my code. Am I able to
avoid braces or not? Is it guaranteed to work or do different compilers
produce different results? If I have code that works perfectly now and
all
I do is remove braces from single statement if, for and while loops, is it
guaranteed to make no difference or am I naive to some other potential
problem?
thanks for clarifying.
|
The main problem with not including braces that I come across is poorly
written
macros particularly debug ones or ones requiring a local variable
For example:
#define DEBUG(s) if( debug ) puts(s)
This works ok for
if( x > 3 ) DEBUG("too big");
But fails miserably the moment you add an else/else if clause:
if( x > 3 ) DEBUG("too big"); else { // this will only happen if x >3 &&
!debug }
I use the braces but if you don't be sure to use the do..while trick -
#define DEBUG(s) do { if(debug) puts(s); /* local vars ... whatever */}
while(false)
This allows you to use the macro almost exactly as a function returning void
and
a good compiler will probably impose no overhead (although it may warn you
which is
annoying).
|
|
| Back to top |
|
 |
lilburne Guest
|
Posted: Sat Jan 31, 2004 9:14 pm Post subject: Re: braces needed? |
|
|
Jonathan Turkanis wrote:
| Quote: | "Jack Klein" <jackklein (AT) spamcop (DOT) net> wrote in message:
There is just too much chance for error, not only when writing the
original code, but even more so later when the code is modified for
defect fixing or upgrade.
Certainly requiring braces seems to have wide acceptance, but your
statement is an exaggeration. Glancing quickly through the standard,
the Dinkumware standard library which ships with VC7.1, and the Boost
source, I find numerous uses of 'if' without braces. Generally the
meaning of such code is immediately clear.
|
Perhaps they don't have coding standards, or neglect them
from time to time. Our coding standard requires braces
around all control statements and even 'outlaws' single line
variants:
if (a < b) function();
for (int x = 0; x < y; ++x) function();
yet from time to time you'll find such examples in the
codebase even written by the originators of the standard.
They get to fix it.
|
|
| Back to top |
|
 |
Jonathan Turkanis Guest
|
Posted: Sat Jan 31, 2004 9:38 pm Post subject: Re: braces needed? |
|
|
"lilburne" <lilburne (AT) godzilla (DOT) net> wrote
| Quote: | Jonathan Turkanis wrote:
"Jack Klein" <jackklein (AT) spamcop (DOT) net> wrote in message:
Certainly requiring braces seems to have wide acceptance, but your
statement is an exaggeration. Glancing quickly through the
standard,
the Dinkumware standard library which ships with VC7.1, and the
Boost
source, I find numerous uses of 'if' without braces. Generally the
meaning of such code is immediately clear.
Perhaps they don't have coding standards, or neglect them
from time to time. Our coding standard requires braces
around all control statements and even 'outlaws' single line
variants:
if (a < b) function();
for (int x = 0; x < y; ++x) function();
yet from time to time you'll find such examples in the
codebase even written by the originators of the standard.
They get to fix it.
|
Maybe this policy should be adopted universely. My only point was that
it hasn't been even by people familiar with the problems which have
been mentioned in this thread.
Boost doesn't have coding standards -- just some general guidelines.
Obviously the standard doesn't have coding standards. But I'd be
surprised to learn Dinkumware doesn't.
Jonathan
|
|
| Back to top |
|
 |
lilburne Guest
|
Posted: Sat Jan 31, 2004 9:51 pm Post subject: Re: braces needed? |
|
|
Jonathan Turkanis wrote:
| Quote: |
Boost doesn't have coding standards -- just some general guidelines.
Obviously the standard doesn't have coding standards. But I'd be
surprised to learn Dinkumware doesn't.
|
Maybe they do, or maybe it depends on seniority of the
person writing the code, or maybe occasionally things get
missed, or maybe they didn't want people whining about
'bloat' in header files. You can't tell just by examining
the code.
There are consideration other than just readability.
Personally, like others that have responded to this thread,
I consider that using braces removes a class of bugs and is
helpful for maintainance. Also as with Jack Klein if I have
any control of a coding standard I'd be insisting on braces
around all control statements.
|
|
| Back to top |
|
 |
Jonathan Turkanis Guest
|
Posted: Sat Jan 31, 2004 10:15 pm Post subject: Re: braces needed? |
|
|
"lilburne" <lilburne (AT) godzilla (DOT) net> wrote in message:
| Quote: |
There are consideration other than just readability.
Personally, like others that have responded to this thread,
I consider that using braces removes a class of bugs and is
helpful for maintainance. Also as with Jack Klein if I have
any control of a coding standard I'd be insisting on braces
around all control statements.
|
I thought Jack Klein was saying, 'you'd better get used to using
braces, since their in (just about) every professional coding
standard', not 'I wish I could add the requirement to my company's
coding standards.'
I guess I'll repeat one last time: I'm not against requiring braces.
Jonathan
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|