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


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

    The braces are noise, so for readability they should probably be
    used only when necessary. Suggesting that they should be used for
    single-statement blocks may be akin to suggesting that people use
    more brackets than necessary in simple expressions, such as...

        a = (b + c);

    The guideline, "when in doubt use brute force", which sometimes
    applies to brackets in expressions (when one can't remember
    operator precedence), doesn't really apply to statements where the
    rules are much simpler.

--

***             Count Templar, ELITE, Cobra Mk III (FM-287)             ***



Sun, 01 Jun 1997 21:11:33 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");

DP>And now, let's consider the proposed alternative:

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

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

Actually I don't use Whitesmith.

I prefer K&R

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

Quote:
}

and for _me_ it is easier to maintain and understand.
--
           "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 21:26:20 GMT  
 why no braces for single statement blocks?

[snip]

(A).

Quote:

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

>And now, let's consider the proposed alternative:

(B).

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

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

(C).

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

Quote:
}

(D).

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

Quote:
}

My choice is D by a large margin. C comes in second and A & B were not
in the running.

Dennis



Sun, 01 Jun 1997 22:23:17 GMT  
 why no braces for single statement blocks?

Quote:

>How about this: Superfluous use of parentheses may indicate
>excessive operator density.

           ^^^^^^^^ change to "programmer" :-)

---
Jetson



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

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.

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

>: Anybody else feel the same way?

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

>    You are not alone

We agree, our corporate coding style specificies that braces are always
to be used.

Jeff Sumner
LinkAge Software, Inc



Mon, 02 Jun 1997 00:24:52 GMT  
 why no braces for single statement blocks?
My rule is:

If the single statement is very short, put it on the same line as the if,
while, etc., and do not use braces:

        if (catastropheOccurred()) return I_GIVE_UP;

If the single statement will not fit on the same line, use braces:

        if (complexFunc(foo, bar) != simpleFunc(blech, bax)) {
           foo = bar + grungyComputation(blech, bax);
        }

--
David F. Skoll
<a HREF="http://www.doe.carleton.ca/students/dfs/">
Click here for my home page</a> "Query two pi" on typewriter.



Mon, 02 Jun 1997 02:20:10 GMT  
 why no braces for single statement blocks?

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

    My vote would be for...

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

    thus avoiding an infinite loop when j is less than k. It's also
    interesting that the style above, which throws away vertical space
    on extra braces and uses very big indentation, skimps on what some
    people might consider the important bits, within the ()s.

Quote:
> I prefer K&R

    I just opened my copy of K&R2 to a page somewhere in the middle
    that has some code on it, and noticed...

    o   Indents are 4 spaces (remember hard tabs aren't totally
        portable).

    o   No extra braces are used, despite single-statement while/if/if
        and for/if constructs.

    o   Expressions within ()s are almost always spaced out nicely.

    Code layout is obviously a thing that varies from programmer to
    programmer, and I think it tends to evolve too as one gets
    increasingly comfortable with the language. One gets to trust
    things like operator precedence (leading to a = b + c * d instead
    of a = b + (c * d)), or, in this case, statement blocking.

--

***             Count Templar, ELITE, Cobra Mk III (FM-287)             ***



Mon, 02 Jun 1997 03:33:33 GMT  
 why no braces for single statement blocks?

|> 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;

If you are that certain about no code ever being added, why not use this:

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

In all other cases, braces get my vote even if not needed syntactically.

                                                 MCE
--
================================================================================
#include <std/disclaimer>
 GCS -d+ H s++:-- !g p1 !au a29 w+ v C+++ UHLUS+++$ P+ L++ 3- E--- N+++ K W---
 M-- V-- -po+ Y- t--- !5 !j R- G''' tv- b+ !D B? e+++ u+++ h+(*) f !r n---- y?

================================================================================



Mon, 02 Jun 1997 04:01:50 GMT  
 why no braces for single statement blocks?

Since we're all talking about our style...

I always put braces unless the single statement is on the same line,
which is usually only for very simple (read: internal) functionality.

        if (a==b) {
                DoSomething();
        }

        if (c==d) break;

Style is an amazingly personal thing and an attempt to force a style
on someone who knows what they are doing will not only fail, but make
you something akin to an enemy for life.  However, this is not to say
that forcing a style on someone just learning is also a bad idea.  I
find it extremely beneficial to teach style while teaching
programming.  The most important lessons, though, are on how to
improve your style.

Myself, I picked up the "always use braces" idea a couple years ago
from someone who said that was what they did and why they liked it.
Had they tried to _make_ me do it, I would not use it to this day :-)
I added it to my personal style because _I_ felt it was a good idea.
I also added the "unless it's all on one line" (which they didn't
have) because I felt there were times when it was better (more
understandable code) not to have the braces.  Live and learn!

When I work on projects with several other people, I usually try to
standardize the style somewhat so it is easier to understand the project
as a whole.  Talk about having to walk on razors not to offend anybody...

                                        Brian

-------------------------------------------------------------------------------
    In theory, theory and practice are the same.  In practice, they're not.



Mon, 02 Jun 1997 05:03:20 GMT  
 why no braces for single statement blocks?
Perhaps we would be better off if C had been structured like some
other languages - something like;

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



Mon, 02 Jun 1997 06:25:38 GMT  
 
 [ 27 post ]  Go to page: [1] [2]

 Relevant Pages 
 

 
Powered by phpBB® Forum Software