Coding Style

E

Eric

I thought it might be fun to run a simple vote to discover the most
preferred spacing style for a simple if statement with a single, simple
boolean test. By my count, there are 32 possible variations for this
case. Here is a complete list.

AA: if ( a > b )
AB: if ( a > b)
AC: if ( a >b )
AD: if ( a >b)
AE: if ( a> b )
AF: if ( a> b)
AG: if ( a>b )
AH: if ( a>b)
AI: if (a > b )
AJ: if (a > b)
AK: if (a >b )
AL: if (a >b)
AM: if (a> b )
AN: if (a> b)
AO: if (a>b )
AP: if (a>b)
AQ: if( a > b )
AR: if( a > b)
AS: if( a >b )
AT: if( a >b)
AU: if( a> b )
AV: if( a> b)
AW: if( a>b )
AX: if( a>b)
AY: if(a > b )
AZ: if(a > b)
BA: if(a >b )
BB: if(a >b)
BC: if(a> b )
BD: if(a> b)
BE: if(a>b )
BF: if(a>b)

So, rank the available options from your most favorite to your least
favorite. It is not necessary fully rank all available options. Two or
more options can be ranked as being essentially equivalent. An example
ballot would look like:

AA > AJ > AG > AP > AQ = AW = AZ = BF

In this case, AA AJ AG AP AQ AQ AZ & BF, are all ranked above those
options which were left unranked. AQ AW AZ & BF are considered
essentially equivalent in terms of which would be preferred over the
other.

Feel free to e-mail your ballot to me to keep the group from filling up
with the votes. I'll announce the results (if there are any to announce)
when I stop receiving votes.

If you're interested in the method I will use to determine the result,
you are welcome to ask me via a private message.

If this receives a sufficient amount of interest, I'll do more just like
it out of some morbid curiosity concerning what is the most preferred
(within well known limitations) style around here.
 
D

Dan Pop

In said:
I thought it might be fun to run a simple vote to discover the most
preferred spacing style for a simple if statement with a single, simple
boolean test.

You're wasting your time. There is no fun in a religious war about the
usage of white space to improve the code readability.
AJ: if (a > b)

IMHO, this is the best, as it uses the minimum amount of white space
for a maximum of effect. if(a > b) comes close, but I prefer to omit
the space before the left parenthesis only for function calls.

Of course, other people have different preferences and there is nothing
fun or interesting in that.

Dan
 
M

Mike Wahler

Eric said:
I thought it might be fun to run a simple vote to discover the most
preferred spacing style for a simple if statement with a single, simple
boolean test. By my count, there are 32 possible variations for this
case. Here is a complete list.

AA: if ( a > b )
AB: if ( a > b)

void f1(void)
{
}

void f2(void)
{
}

void f3(void)
{
void (*f[])(void) = {f1, f2};
f[a > b]();
}

-:)

-Mike
 
A

Andrey Tarasevich

Mike said:
...
void f1(void)
{
}

void f2(void)
{
}

void f3(void)
{
void (*f[])(void) = {f1, f2};
f[a > b]();
}

-:)
...

(a > b && (f1(), 1)) || (f2(), 1)

is more elegant since it doesn't need the extra array :O)
 
C

CBFalconer

Dan said:
You're wasting your time. There is no fun in a religious war
about the usage of white space to improve the code readability.


IMHO, this is the best, as it uses the minimum amount of white
space for a maximum of effect. if(a > b) comes close, but I
prefer to omit the space before the left parenthesis only for
function calls.

Of course, other people have different preferences and there is
nothing fun or interesting in that.

The millenium has arrived, 1229 days late. Dan and I are in
complete agreement.
 
E

E. Robert Tisdale

Style is a very subjective and personal consideration.
C and C++ programmers develop or adopt a style
in order to make their code easier for themselves
and other programmers to read, understand and maintain.
If you are developing your own style, there are no rules
except that you should try to be consistent.
Otherwise, you should try to adopt a style
with which other C and C++ programmers are comfortable,
familiar or that they will at least recognize.
Personally, I try to use the same punctuation rules
that are used for ordinary (mathematical) typesetting.
Here are my recommendations:

Terminators always follow immediately after an expression

x@ for all @ in {?, :, ,, ;}

and are followed by at least one white space.
Write

x? y: z

instead of

x ? y : z

or

x?y:z

and write

void f(int, int, int); void g(double);

instead of

void f(int,int,int);void g(double);

for example.

There is no space
between some binary operators and their operands

x@y for all @ in {::, ., ->, .*, ->*, *, /, %, &, ^, |}

but there is always a space
between other binary operators and their operands

x @ y for all @ in {+, -, <<, >>;, <, <=, >, >=, ==, !=,
&&, ||, =, *=, /=, %=, +=, -=, <<=, >>=, &=, |=, ^=}

except when expressions appear as subscripts.
Write

x + y

instead of

x+y
and

x*y

instead of

x * y

for example.
But you may wish to write

A[i+1][j-1]

instead of

A[i + 1][j - 1]

for example to subscript array A.


Most unary prefix operators never have any whitespace
between themselves and their operands

@x for all @ in {::, ++, --, ~, !, -, +, &, *}

but others do

@ x for all @ in {sizeof, new, delete, delete [], throw}

No unary postfix operators

x@ for all @ in {[], (), ++, --}

ever have any whitespace between themselves and their operands.

Use the normal typesetting rules for parentheses (),
square brackets [], angle brackets <> and curly brackets {}.
No space after (, [, < or { and no space before ), ], > or }.
Write

(x)

instead of

( x )

or

(x )

or

( x)

and write

[x]

instead of

[ x ]

or

[x ]

or

[ x]

for example.
There are, of course, exceptions
where extra white space helps to make your code more readable:

double A[2][3] = {{ 1, -1, 0},
{-10, 11, -21}};


Don't give identifiers cryptic, mangled names.
Use ordinary, meaningful words, conventional symbols
or abbreviations with annotations.
Write

double distance, velocity, acceleration, mass, Force;

Force = mass*acceleration;

or

double x; // distance
double v; // velocity
double a; // acceleration
double m; // mass
double F; // force

F = m*a;

for example.

Don't rely on defaults. Make declarations explicit.
Write

int i = 1;

instead of

i = 1;

to declare and initialize integer i and write

class X {
private:
// Representation
int I;
public:
// Constructors
// ...
};

instead of

class X {
// Representation
int I;
public:
// Constructors
// ...
};

to define the private data members of class X for example.


Use indentation to emphasize scope.
Everybody is comfortable with standard indentation:

void f()
{
// indent
}

But I indent curly brackets to the scope of the function body:

void f()
{
// indent
}

And I include the open curly bracket with the function heading:

void f() {
// indent
}

to save a line of code.

I always indent just two spaces at a time and
I place just one statement on each line so that
there is usually room for a comment at the end of each line
beginning in column 33 or 41.

Write

if (condition) {
// statements
}

instead of

if(condition) {
// statements
}

and

while (condition) {
// statements
}

instead of

while(condition) {
// statements
}

to distinguish flow control structures from function calls.

I use

// comment

for comments in C++ and I reserve

/*
a = b;
// comment
b = c;
*/

to comment out code which may include comments.


If you find yourself in an environment
that requires you to conform to style rules with which you are not
comfortable,
consider investing a little time and effort in a program like astyle

Artistic Style
http://astyle.sourceforge.net/

which changes the appearance of C or C++ programs
by inserting or deleting whitespace.

Write

constant == variable

instead of

variable == constant

when comparing a variable to a constant for equality
so that if you write

constant = variable

by mistake, the compiler will detect the error.

I always write

x < y

or

x <= y

instead of

y > x

or

y >= x

when comparing two values so that the expression is true
when the left hand side is to the left of the right hand side
on the real number line.
 
E

Eric

Dan Pop said:
You're wasting your time. There is no fun in a religious war about the
usage of white space to improve the code readability.

Not interested in discussion about what is best style.

I am only interested in the most preferred style based upon the rankings
of readers of comp.lang.c.
IMHO, this is the best, as it uses the minimum amount of white space
for a maximum of effect. if(a > b) comes close, but I prefer to omit
the space before the left parenthesis only for function calls.

So, you would rank:

AJ > AZ

Do you see any other merit differences between the various options or
would this ballot be the one you would submit?
 
E

Eric

Mike Wahler said:
Eric said:
I thought it might be fun to run a simple vote to discover the most
preferred spacing style for a simple if statement with a single, simple
boolean test. By my count, there are 32 possible variations for this
case. Here is a complete list.

AA: if ( a > b )
AB: if ( a > b)

void f1(void)
{
}

void f2(void)
{
}

void f3(void)
{
void (*f[])(void) = {f1, f2};
f[a > b]();
}

-:)

:p

Ok, fine....there are more variants, but let's just stick to the ones I
defined.
 
M

Martin Ambuhl

Eric said:
I thought it might be fun to run a simple vote to discover the most
preferred spacing style for a simple if statement with a single, simple
boolean test. By my count, there are 32 possible variations for this
case. Here is a complete list.

I reject any style with different spacing after '(' and before ')' or
with different spacing before and after an operator. That means your
list is immediately collapsed to:
AA: if ( a > b )
AG: if ( a>b )
AJ: if (a > b)
AP: if (a>b)
AQ: if( a > b )
AW: if( a>b )
AZ: if(a > b)
BF: if(a>b)
So, rank the available options from your most favorite to your least
favorite.

Count the 24 omitted ones as tied for dead last.
Of the remaining ones, I dislike (with decreasing vehemence)
a) shoving the '(' up next to the 'if',
b) omitting spaces around the operator '>', and
c) spaces afet '(' and before ')'.

This leaves the order as roughly
AJ > AA > AP > AZ > AG > AQ > BF > AW > (All the rest)

I am not adamant about any of this except for finding the non-symmetric
spacing unacceptable. Any style which you find readable is OK for your
own use, obviously. And I would change mine to fit any required house
style.
 
E

Ed Morton

Eric said:
I thought it might be fun to run a simple vote to discover the most
preferred spacing style for a simple if statement with a single, simple
boolean test. By my count, there are 32 possible variations for this
case. Here is a complete list.

AA: if ( a > b )
AJ: if (a > b)
<snip>

AJ > AA

I'd never consider using any of the rest.

Ed.
 
M

Mark McIntyre

I thought it might be fun to run a simple vote to discover the most
preferred spacing style

You have a curious idea of fun..... :)
AJ: if (a > b)
AP: if (a>b)
AX: if( a>b)
BF: if(a>b)

any of those 4, not fussy. The others, with odd spacing round things, look
silly.
 
E

Eric

Ed Morton said:
<snip>

AJ > AA

I'd never consider using any of the rest.

Whether you'd consider using them or not is irrelevent. The only thing
that matters is whether you see any merit differences between them.

For example, based on your two rankings, it would seem that you value a
space between the if statement and the left paren.

So, it might be that you would rank the remaining ones which match that
feature below AA, but above those that do not have that feature.

But, then, AJ > AA could easily be your complete ranking.

thank you.
 
D

Default User

Eric said:
Not interested in discussion about what is best style.

I am only interested in the most preferred style based upon the rankings
of readers of comp.lang.c.


To what end? It's one of the more useless discussion around.



Brian Rodenborn
 
M

Mark McIntyre

AJ > AA

I'd never consider using any of the rest.

Whether you'd consider using them or not is irrelevent. The only thing
that matters is whether you see any merit differences between them. [/QUOTE]

Thats a bit rude. The question you asked was which we preferred. Ed
answered that - AJ or AA.
 
B

Ben Pfaff

Mark McIntyre said:
any of those 4, not fussy. The others, with odd spacing round things, look
silly.

You don't think AX looks silly with its asymmetric spacing?
 
M

Malcolm

AQ > AZ > AA > AJ

all the rest are either too compact or show very odd use of whitespace.
 
E

Eric

Mark McIntyre said:
Whether you'd consider using them or not is irrelevent. The only thing
that matters is whether you see any merit differences between them.

Thats a bit rude. [/QUOTE]

I'm sorry you feel that way.
The question you asked was which we preferred. Ed
answered that - AJ or AA.

I asked people to rank the available options from most to least
preferred, not to rank only the options they would actually consider
using.

It is not necessary to rank all of the available options and two or more
options can be ranked differently.

However, if one see a merit difference between two or more of the
available options, they should be ranked. Only by doing this or assuming
this has been done can the most preferred option be found.

Is this clear now?
 
E

Eric

Eric said:
Whether you'd consider using them or not is irrelevent. The only thing
that matters is whether you see any merit differences between them.

I am sorry if my bluntness caused offense.

I am sincerely interested in whether you see any further merit
differences among the remaining options.
 
E

E. Robert Tisdale

Eric said:
I am sorry if my bluntness caused offense.

I see that you've already met some of our indigenous trolls.
If you decide to continue your subscription to the comp.lang.c newsgroup,
you will learn to recognize and ignore them.
 

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

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,042
Latest member
icassiem

Latest Threads

Top