473,506 Members | 11,491 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.
Jul 22 '05 #1
16 3311
On Sat, 31 Jan 2004 02:02:04 +0000 (UTC) in comp.lang.c++, "Jason"
<ja***********@btinternet.com> 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.

Jul 22 '05 #2

"David Harmon" <so****@netcom.com> wrote in message
news:40***************@news.west.earthlink.net...
On Sat, 31 Jan 2004 02:02:04 +0000 (UTC) in comp.lang.c++, "Jason"
<ja***********@btinternet.com> 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 allI do is remove braces from single statement if, for and while loops, is itguaranteed 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.

Jul 22 '05 #3
On Sat, 31 Jan 2004 02:02:04 +0000 (UTC), "Jason"
<ja***********@btinternet.com> wrote in comp.lang.c++:
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/~a...FAQ-acllc.html
Jul 22 '05 #4

"Jack Klein" <ja*******@spamcop.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
Jul 22 '05 #5
Jason wrote:
"David Harmon" <so****@netcom.com> wrote in message
news:40***************@news.west.earthlink.net...
On Sat, 31 Jan 2004 02:02:04 +0000 (UTC) in comp.lang.c++, "Jason"
<ja***********@btinternet.com> 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.
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

Jul 22 '05 #6
On Fri, 30 Jan 2004 22:34:01 -0700, "Jonathan Turkanis"
<te******@kangaroologic.com> wrote in comp.lang.c++:

"Jack Klein" <ja*******@spamcop.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/~a...FAQ-acllc.html
Jul 22 '05 #7

"Jack Klein" <ja*******@spamcop.net> wrote in message:
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
Jul 22 '05 #8

">
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.
Jul 22 '05 #9
On 31 Jan 2004 00:23:55 EST in comp.lang.c++, Gianni Mariani
<gi*******@mariani.ws> was alleged to have written:
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.

Jul 22 '05 #10

"Jason" <ja***********@btinternet.com> wrote in message
news:bv**********@hercules.btinternet.com...
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).


Jul 22 '05 #11
Jonathan Turkanis wrote:
"Jack Klein" <ja*******@spamcop.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.

Jul 22 '05 #12

"lilburne" <li******@godzilla.net> wrote in message
news:bv************@ID-203936.news.uni-berlin.de...
Jonathan Turkanis wrote:
"Jack Klein" <ja*******@spamcop.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
Jul 22 '05 #13
Jonathan Turkanis wrote:

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.

Jul 22 '05 #14

"lilburne" <li******@godzilla.net> wrote in message:

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

Jul 22 '05 #15
lilburne <li******@godzilla.net> wrote in message news:<bv************@ID-203936.news.uni-berlin.de>...
Jonathan Turkanis wrote:

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.


Well, you can't argue: Bugs cost. Yes? We want to eliminate
the possibility of bugs. Yes? It is not impossible that a drunken
idiot will introduce a bug because of braces. No? So we make
braces required then. Yes? Can't argue unless insane.

But K&R didn't make braces obligatory. And this is a symbolic move!
Because:

[1] Orthogonal logic

statement:
expression
compound-statement
if-statement
....

compound-statement: {
statement
}

and a compound-statement IS a statement

[2]
They say that good indention is very important (and should
be enough to avoid the braces problem therefore)

[3]
One should generally be focused when programming.
So: avoid braces like hell unless your employer forces you to
use them.

everybody happy with that?
Jul 22 '05 #16
> 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


This bug should have been caught in two other places:
- you should not write a macro that can be misused like that
- the person who wrote "if (blah) MACRO()" should have been more awake

I would not want to impose braces on all my code in order to avoid
that situation.

In my years of braceless coding I have only had a couple of
unexpected if..else behaviours, and one situation that took an hour
to find. IMHO this is not enough reason to start crippling
my code with unnecessary braces. The gist of it was:

if (blah)
some_large_expression();
else
return some_other_large_expression(),

// debug();
printf("blah was true\n");

and the function would print "blah was true" when blah was false,
and return the wrong thing, and I was even more annoyed by the fact
that if I put in the debug line then the behaviour would all change
again. :)
Jul 22 '05 #17

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
1563
by: Michael | last post by:
>>Right. Then we can have "does the brace go on the same line or the >>next line" wars. >> >> > > int main (void) > { > if (true) > { > }
97
4321
by: Kjetil Torgrim Homme | last post by:
often when re-factoring code, I need to change the indent level of some chunk of code. due to the lack of an end marker, my Emacs has to use heuristics when re-indenting, and this will...
14
1598
by: Andrew Bullock | last post by:
I thought the two bits of code should be the same? They aren't evaluating the same for me .... Can someone tell me why please? Thanks Andrew
12
13423
by: Pawel | last post by:
Hallo group members Could You tell hw to write this code in order not to get warning from subject: #include <iostream> using namespace std; using namespace __gnu_cxx; struct entry {
2
1583
by: mauricioarango | last post by:
Hi everybody, I'm a PHP beginner, so please forgive me for this question. I am working on code someone else wrote a long time ago. The code was written without curly braces in many places, which...
22
2368
cat
by: Jag | last post by:
I've read parts of K&R's ANSI C v2 and this is what their cat looked like but when I compared the speed of this code to gnu cat, it seems very slow. How do I optimize this for greater speeds? is...
0
7220
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
7308
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,...
1
7023
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
7479
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
5617
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,...
1
5037
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3178
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1534
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.