why no braces for single statement blocks? 
Author Message
 why no braces for single statement blocks?

Where did this idea originate?

I think it's a fundamental style error that leads to maintenance
difficulties.

I believe that you should always mark blocks with braces regardless of
the number of statements.

Consider what has to happen when another statement has to be added in
the same path.

If you are learning C then please think about following this
guideline.

Anybody else feel the same way?

for (i=0;i<10;i++)
        while (j < k)
                if (j < m)
                        printf("bla");

--
           "Oh no, actually darling I don't have time for games."
            (PGP key: send email with Subject: request pgp key)
                    (ASCII for text only messages)



Sat, 31 May 1997 20:33:43 GMT  
 why no braces for single statement blocks?

Quote:

>>If you are learning C then please think about following this
>>guideline.

PJL> Pick a style that's right for *you* and stick with it; do not
PJL> force your style on anybody else.  It's tantamount to a
PJL> holy war.

"force you style"

I don't consider the words "think about following" an act of force.

Point taken Grumpy :-)
--
           "Oh no, actually darling I don't have time for games."
            (PGP key: send email with Subject: request pgp key)
                    (ASCII for text only messages)



Sun, 01 Jun 1997 02:19:19 GMT  
 why no braces for single statement blocks?

Quote:


VJP>(Robert>Nicholson) writes:
>>I think it's a fundamental style error that leads to maintenance
>>difficulties.

VJP>My favorite quote regarding style: "Style is what we agree it is"
VJP>Personally, whenever I write an if (cond) statement, I immediately
VJP>type the opening and closing braces before writing the conditional
VJP>statement. **BUT** that is my style.  I don't propose to force it upon
VJP>anyone!

OK let's drop this right here and now. I was not attempting to "force"
my style on anybody else. I was merely describing the merits of the
style I choose to adopt.
--
           "Oh no, actually darling I don't have time for games."
            (PGP key: send email with Subject: request pgp key)
                    (ASCII for text only messages)



Sun, 01 Jun 1997 02:20:27 GMT  
 why no braces for single statement blocks?

Quote:
>I think it's a fundamental style error that leads to maintenance
>difficulties.

It would depend on your proficiency at reading C.  Some people would prefer I
not use complex assignments like:
        a = b =  c == d++;
simply because they haven't learned the order of operations and/or don't know
what the operator == returns.

Quote:
>I believe that you should always mark blocks with braces regardless of
>the number of statements.
>Consider what has to happen when another statement has to be added in
>the same path.

You add a couple of braces.

Quote:
>Anybody else feel the same way?

Not really.  Adding braces when you don't need them doesn't hurt the compiler
any, but it does increase the risk of unbalanced braces, and takes up extra
space.

The argument over when to include braces is probably as old and personal as
whether or not braces should always be on a line all by themselves.

Quote:
>for (i=0;i<10;i++)
>        while (j < k)
>                if (j < m)
>                        printf("bla");

Would you prefer I did *this*:

for (i=0;i<10;i++) {
        while (j < k) {
                if (j < m) {
                        printf("bla");

Quote:
}}}

or this:

for (i=0;i<10;i++)
{
        while (j < k)
        {
                if (j < m)
                {
                        printf("bla");
                }
        }

Quote:
}

I find this last method causes more problems than it solves, because it allows
me to see less of the surrounding code as I work.

I would probably write it as:

for (i=0;i<10;i++)
/* WARNING: endless loop, j not changed */
        while ((j < k) && (j < m))
                printf("bla");

---
Jetson



Sat, 31 May 1997 15:57:40 GMT  
 why no braces for single statement blocks?

Quote:
>Where did this idea originate?

        Probably with Algol.  Braces are short-hand begin/end pairs.  I
        do know that in Pascal, begin/end are not needed for single
        statements either.

Quote:
>I think it's a fundamental style error that leads to maintenance
>difficulties.

        1. You're entitled to your opinion.

        2. You have supplied no data to support your claim.

        3. You're way, way too late...about 30 years too late.

Quote:
>I believe that you should always mark blocks with braces regardless of
>the number of statements.
>Consider what has to happen when another statement has to be added in
>the same path.

        So, you just add the braces--what's the big deal?  For those of
        us who do not have a problem, why should we add them.  Just deal
        with it.

Quote:
>If you are learning C then please think about following this
>guideline.

        Pick a style that's right for *you* and stick with it; do not
        force your style on anybody else.  It's tantamount to a
        holy war.

Quote:
>Anybody else feel the same way?

        Yes, there are; I am not one of them.
--
        - Paul J. Lucas
          AT&T Bell Laboratories
          Naperville, IL


Sun, 01 Jun 1997 00:24:47 GMT  
 why no braces for single statement blocks?

Quote:

(Robert>Nicholson) writes:
>I think it's a fundamental style error that leads to maintenance
>difficulties.

My favorite quote regarding style: "Style is what we agree it is"
Personally, whenever I write an if (cond) statement, I immediately
type the opening and closing braces before writing the conditional
statement. **BUT** that is my style.  I don't propose to force it upon
anyone!

Regards, Vince
Opinions expressed may not be correct,
  but at least they're my own.



Sat, 31 May 1997 17:26:38 GMT  
 why no braces for single statement blocks?

Quote:
> My favorite quote regarding style: "Style is what we agree it is"

My favorite is the old Latin saw about Gus being on the bus.  :-)

After that I feel almost guilty, but I just can't resist the opening you've
provided:

Quote:
> Personally, whenever I write an if (cond) statement, I immediately
> type the opening and closing braces before writing the conditional
> statement.

Personally, I prefer not to write an if until I have some idea of what's
going to go into the conditional statement; hence, I often feel no need
to write unnecessary braces.

Live long and code well!



Sun, 01 Jun 1997 07:55:08 GMT  
 why no braces for single statement blocks?
 > I tend to omit the braces for single line statements only so that would
 > become:
...
 >
Sometimes I omit them, sometimes not, but when I do omit them I put the
single statement on the same line as the condition, loop or whatever.  So:

for (i=0;i<10;i++) {
    while (j < k) {
        if (j < m) printf("bla");
    }

Quote:
}

or:

for (i=0;i<10;i++) {
    while (j < k) if (j < m) printf("bla");

Quote:
}

or even:

for (i=0;i<10;i++) while (j < k) if (j < m) printf("bla");

depending on the kind of conditions and loops used.
--
dik t. winter, cwi, kruislaan 413, 1098 sj  amsterdam, nederland, +31205924098



Sun, 01 Jun 1997 10:52:54 GMT  
 why no braces for single statement blocks?

Quote:


>VJP>(Robert>Nicholson) writes:
>>>I think it's a fundamental style error that leads to maintenance
>>>difficulties.
>VJP>My favorite quote regarding style: "Style is what we agree it is"
>VJP>Personally, whenever I write an if (cond) statement, I immediately
>VJP>type the opening and closing braces before writing the conditional
>VJP>statement. **BUT** that is my style.  I don't propose to force it upon
>VJP>anyone!
>OK let's drop this right here and now. I was not attempting to "force"
>my style on anybody else. I was merely describing the merits of the
>style I choose to adopt.

        You did no such thing -- you supplied no hard data, rendering
        your suggestion nothing more than idle opinion.  Anybody can
        have an opinion.  The whole thing is best dropped.  Like I said,
        it's religious war.

        However, if someone is programmer and stumbles over and has a
        hard time with something as trivial as braces, I suggest s/he
        get another career -- how on Earth is that person going to be
        able to deal with a real problem?
--
        - Paul J. Lucas
          AT&T Bell Laboratories
          Naperville, IL



Sun, 01 Jun 1997 12:00:18 GMT  
 why no braces for single statement blocks?


Quote:
>Anybody else feel the same way?

>for (i=0;i<10;i++)
>        while (j < k)
>                if (j < m)
>                        printf("bla");

I tend to omit the braces for single line statements only so that would
become:

for (i=0;i<10;i++) {
    while (j < k) {
        if (j < m)
            printf("bla");
    }

Quote:
}

--
-----------------------------------------


-----------------------------------------


Sun, 01 Jun 1997 04:29:54 GMT  
 why no braces for single statement blocks?

Quote:

>Where did this idea originate?

*shrug*  Dennis Ritchie?  Certainly, if it wasn't his idea, he
cribbed it from someone else.

Quote:
>I think it's a fundamental style error that leads to maintenance
>difficulties.
>I believe that you should always mark blocks with braces regardless of
>the number of statements.

I use gnuemacs, which has a feature to automatically indent all source
code, and I am in the habit of using it all the time.  When you are
using it, the fact that a brace is missing becomes immediately
obvious because of the way the code is indented.

This feature is incredibly valuable to me.  Not only does it make
missing braces obvious, it also makes missing parens, backets,
close quotes, etc, etc immediately obvious.

-Dave



Sun, 01 Jun 1997 10:40:46 GMT  
 why no braces for single statement blocks?
: Where did this idea originate?

Algol?  It has a certain elegance since the compound statement { ... }
is just another statement.

: I think it's a fundamental style error that leads to maintenance
: difficulties.  I believe that you should always mark blocks with
: braces regardless of the number of statements.  Consider what has to
: happen when another statement has to be added in the same path.

Well there are often statements where you can tell that it is very
unlikely other statements will ever be added.  There are many
examples such as:

        for (i = 0; i < 10; ++i)
                a[i] = 0;

Plus adding redundant braces IMHO can make the code more difficult to
read.  (Actually, don't tell anyone but I really like Ada's syntax in
this area where you have if ... endif etc.)

But I agree that if it seems at all likely that other statements may
be added to the loop/if/whatever then putting the braces in can avoid
maintenance problems.

Andrew.
--

--------------------------------
Just a SPOKE, not a SPOKESPERSON



Sun, 01 Jun 1997 15:23:52 GMT  
 why no braces for single statement blocks?
: Where did this idea originate?

: I think it's a fundamental style error that leads to maintenance
: difficulties.

: I believe that you should always mark blocks with braces regardless of
: the number of statements.

: Consider what has to happen when another statement has to be added in
: the same path.

: If you are learning C then please think about following this
: guideline.

: Anybody else feel the same way?

: for (i=0;i<10;i++)
:       while (j < k)
:               if (j < m)
:                       printf("bla");

: --
:            "Oh no, actually darling I don't have time for games."
:             (PGP key: send email with Subject: request pgp key)
:                     (ASCII for text only messages)

    You are not alone

*---------------------------------------------------*
*                   Pablo Durban Garcia             *
*                   Div. Plataformas de Gestion     *
*                   Telefonica I+D (R&D)            *
*                   C/ Emilio Vargas, 6             *
*                   28043 Madrid (SPAIN)            *

*---------------------------------------------------*



Sun, 01 Jun 1997 15:36:03 GMT  
 why no braces for single statement blocks?

Quote:
>Where did this idea originate?

>I think it's a fundamental style error that leads to maintenance
>difficulties.

>I believe that you should always mark blocks with braces regardless of
>the number of statements.

>Consider what has to happen when another statement has to be added in
>the same path.

>If you are learning C then please think about following this
>guideline.

>Anybody else feel the same way?

>for (i=0;i<10;i++)
>    while (j < k)
>            if (j < m)
>                    printf("bla");

And now, let's consider the proposed alternative:

for (i=0;i<10;i++)
{
        while (j < k)
        {
                if (j < m)
                {
                        printf("bla");
                }
        }

Quote:
}

Does it look better?  Is it easier to understand?  Answer these questions
and make your choice accordingly.

Dan
--
Dan Pop                       | The only reason God was able to make the
CERN, CN Division             | world in 7 days was he didn't have to remain

Mail:  CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland



Sun, 01 Jun 1997 19:56:00 GMT  
 why no braces for single statement blocks?

[...]

Quote:
> Anybody else feel the same way?

I agree.  Concluding such a statement with a single semicolon might
lead to misunderstandings since the semicolon is easily missed when
reading code.  In my opinion, it's better to include a block after all
flow control primitives, even if the block is empty.

Best regards,

Christian
--

\ Fjellbirkeland 21A 114, N-0864 Oslo, Norway - Tel: +47 22 187103 - *<8O) /



Sun, 01 Jun 1997 21:05:42 GMT  
 
 [ 27 post ]  Go to page: [1] [2]

 Relevant Pages 

1. re : why no braces for single statement blocks?

2. WHY NO BRACES FOR SINGLE

3. using statement, why just a single type?

4. Why am I seeing such a delay between my catch block and my finally block

5. SUMMARY: Brace style -- why should I care?

6. 'using' statement in block

7. handling single quotes in database statements

8. Q: Why isapi extention blocks while running for a long time

9. why does this if statement fail ?

10. C# Strange if{} statement behavior, why?

11. Why method not showing up from #import statement

12. Newbie: Why VBScript statements cannot work in project Settings

 

 
Powered by phpBB® Forum Software