Blocks

P

polas

Hi - I have a quick question. In C code it seems that some authors
will prefer to use indenting to signify a block, and some will use
braces - for instance one author may use

if (test)
do some action;

whereas another author may use
if (test)
{
do some action;
}

Personally I would use the second way (although can see from a style
POV its disadvantages), but is there an established convention of what
to use, and whats the standard say about it?

Nick
 
S

Stephen Sprunk

polas said:
Hi - I have a quick question. In C code it seems that some authors
will prefer to use indenting to signify a block, and some will use
braces - for instance one author may use

if (test)
do some action;

whereas another author may use
if (test)
{
do some action;
}

Personally I would use the second way (although can see from a style
POV its disadvantages), but is there an established convention of what
to use, and whats the standard say about it?

The standard says both are fine; it's solely a matter of style.

Some coding conventions specify the former is preferred because it's
denser. Others specify the latter is preferred because it prevents
novice or careless maintenance programmers from later modifying the code to:

if (test)
do some action;
do some other action;

Both POVs have their advantages.

S
 
L

Lew Pitcher

Hi - I have a quick question. In C code it seems that some authors
will prefer to use indenting to signify a block,

Sort of, but not really. There is a difference between "indentation" and "a
block". A block may use indentation, but indentation is not a language
delimiter for a block.

The term "block" usually denotes a compound statement, which consists of
one left brace bracket, denoting the start of the compound statement,
zero or more complete statements, and
one right brace bracket, denoting the end of the compound statement
and some will use
braces - for instance one author may use

if (test)
do some action;

whereas another author may use
if (test)
{
do some action;
}

For single statements, both versions are usable.

However, if the "true" part of the if() statement requires more than one
statement to complete it's task, then the most straight-forward way to code
it would be as a compound statement. As in
if (test)
{
do_some_action();
if (some_other_test) do_something_else();
do_some_other_action();
}

Personally I would use the second way (although can see from a style
POV its disadvantages), but is there an established convention of what
to use, and whats the standard say about it?

"Indentation" is a matter of style; the standards say nothing about it.
"Blocks of code" are defined (as compound statements) by the standard.

"Indentation" and "Blocks of code" are orthogonal concepts.

--
Lew Pitcher

Master Codewright & JOAT-in-training | Registered Linux User #112576
http://pitcher.digitalfreehold.ca/ | GPG public key available by request
---------- Slackware - Because I know what I'm doing. ------
 
J

James Kuyper

polas said:
Hi - I have a quick question. In C code it seems that some authors
will prefer to use indenting to signify a block, and some will use
braces - for instance one author may use

if (test)
do some action;

whereas another author may use
if (test)
{
do some action;
}

The first form is more susceptible to allowing certain kinds of errors
than the second one. I'm very error-prone, but despite that fact, for
some reason I tend not to make the kinds of errors that the second form
avoids. As a result, I tend to favor the first form, as being less
cluttered.
Personally I would use the second way (although can see from a style
POV its disadvantages), but is there an established convention of what
to use, and whats the standard say about it?

Yes. There's a convention; in fact, there's many conventions, each
conflicting with the others. The standard says only that both forms (and
several others) are legal.

There's a program called 'indent', available on some platforms, which
will reformat C source code to match a specified convention. If you have
access to it, I recommend reading the documentation for it. Almost every
option comes in both a positive form and a negative one. Both forms
exist because for each form, there are some people who prefer it over
the opposite form. When you look at how many options it has, you'll get
a completely inadequate concept of just how many different perfectly
legal ways there are to write C code.

I recently investigated indent in detail, and determined that my
personal preferred coding style is mostly covered by the following
'indent' options:

-bad -bap -nbbb -bbo -nbc -bfda -bfde -bl -bli0 -bls -nbs -ncdb -cdw
-nce -ci4 -ncs -di16 -fca -fc1 -hnl -i4 -ip8 -l78 -nlp -nlps -npcs
-nprs -npsl -nsaf -nsai -nsaw -sc -sob -nss -ut

That option list doesn't correspond at all closely to any of the three
common coding styles indent supports; it's possible that my code can be
uniquely identified by this combination of options, just like a fingerprint.
 
R

RANNA

Hi - I have a quick question. In C code it seems that some authors
will prefer to use indenting to signify a block, and some will use
braces - for instance one author may use

if (test)
    do some action;

whereas another author may use
if (test)
{
    do some action;

}

Personally I would use the second way (although can see from a style
POV its disadvantages), but is there an established convention of what
to use, and whats the standard say about it?

Nick

Hi,

It depends on the situation you encounter. Say if you want to execute
only one instruction when the if condition satisfies, then you can use
first case.
If more than one instructions are executed when the If condition
satisfies, then you can must second case. Hope you already aware of
it.

ranna.
 
K

Keith Thompson

RANNA said:
It depends on the situation you encounter. Say if you want to execute
only one instruction when the if condition satisfies, then you can use
first case.
If more than one instructions are executed when the If condition
satisfies, then you can must second case. Hope you already aware of
it.

It's one statement, not one instruction.

Both forms are really special cases of the same thing. The syntax for
an if statement (we'll ignore the possibility of an else clause) is:

if ( expression ) statement

A compound-statement consists of an opening '{', zero or more
declarations and/or statements, and a closing '}'. For example, it
can be an opening '{', a single statement, and closing '}'; this is
equivalent to the single statement by itself.

So this:

if (x == 42)
x ++; /* an expression-statement */

and this:

if (x == 42)
{ /* a compound-statement */
x ++; /* an expression-statement */
} /* end of the compound-statement */

are exactly equivalent. The choice of which to use is a matter of
style; the standard says nothing about which is better. (The latter
can be easier to maintain.)

There are also style issues regarding the placement of the braces,
something else the standard doesn't address. Common choices are:

if (x == 42) {
x ++;
}

if (x == 42)
{
x ++;
}

if (x == 42)
{
x ++;
}

if (x == 42)
{
x ++;
}

All are equivalent, all are equally valid.
 
S

Stephen Sprunk

RANNA said:
If more than one instructions are executed when the If condition
satisfies, then you can must second case. Hope you already aware of
it.

That's not strictly true; I've abused the comma operator to do multiple
things in a single statement specifically to avoid creating an entire block:

if (fd>0)
close(fd);

changed to

if (fd>0)
close(fd), fd=-1;

I don't use that trick often, but IMHO it's worth it in very specific
cases and a useful thing to at least know, even if you never use it
yourself.

S
 
D

Default User

polas said:
Hi - I have a quick question. In C code it seems that some authors
will prefer to use indenting to signify a block, and some will use
braces - for instance one author may use

if (test)
do some action;

To emphasize what some of the others said, the indentation has nothing
to do with anything, other than visually for a reader of the code.
These are all equivalent;

if (test)
do some action;

if (test
do some action;

if (test) do some action;




Brian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top