473,386 Members | 2,129 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Blocks

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
Oct 20 '08 #1
7 1420
polas wrote:
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
Oct 20 '08 #2
On October 20, 2008 11:09, in comp.lang.c, polas (ni**@helpforce.com) wrote:
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. ------
Oct 20 '08 #3
polas wrote:
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.
Oct 20 '08 #4
On Oct 20, 8:09*pm, polas <n...@helpforce.comwrote:
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.
Oct 20 '08 #5
RANNA <ra*******@gmail.comwrites:
On Oct 20, 8:09*pm, polas <n...@helpforce.comwrote:
>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?

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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Oct 20 '08 #6
RANNA wrote:
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
Oct 20 '08 #7
polas wrote:
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
Oct 20 '08 #8

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

Similar topics

20
by: Doug Holton | last post by:
Is there any metaclass trick or something similar to allow anonymous code blocks? I'd like to be able to let users do something like this fictitious example: b = Button() b.OnClick =: print...
4
by: grs | last post by:
Can a class library have a app.config file. Reason for asking is that the microsoft application blocks all read from myApp.exe.config. How can you use the application blocks if you do not have an...
14
by: J.S. | last post by:
In a Windows Form application, which is the better method to concatenate large blocks of code? 1. Reading the text from text files. 2. Adding the text to the VB file itself? Thanks! J.S. ...
11
by: efialtis | last post by:
Hi to all, I am creating a program which plays a game. The game is played on a NxN square board. In every position of the board we may have a ball or not. Take for example the following 5x5...
3
by: craig | last post by:
I was just wondering if anyone else may have incorporated the original Microsoft Exception Management Application Block (EMAB) or Data Access Application Block (DAAB) into one of their applications...
26
by: brenocon | last post by:
Hi all -- Compared to the Python I know and love, Ruby isn't quite the same. However, it has at least one terrific feature: "blocks". Whereas in Python a "block" is just several lines of...
1
by: Achim Domma | last post by:
Hi, I have an ASCX control which himself contains other ASCX controls. The main control is used in a Asp.Net 1.1 application and works fine. The control is also used in a sharepoint web part...
2
by: flyzone | last post by:
Goodmorning people :) I have just started to learn this language and i have a logical problem. I need to write a program to parse various file of text. Here two sample: --------------- trial...
3
by: GaryDean | last post by:
I have just been through the docs on the Data Access Application blocks and it seems that they complicate things more than make things simple. To me it seems that there is nothing more simple and...
12
by: Michael7 | last post by:
Hi Everyone, I've been trying to get two blocks of text to be aligned, one left, one right, on the same line. What I'm trying to mimic is what I did with tables here: ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...

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.