Velocity Reviews > Coding Style

# Coding Style

Eric
Guest
Posts: n/a

 05-12-2004
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 )
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)

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.

Dan Pop
Guest
Posts: n/a

 05-12-2004
In <1gdo7jq.3gkwmyn064qoN%(E-Mail Removed)> http://www.velocityreviews.com/forums/(E-Mail Removed) (Eric) writes:

>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
--
Dan Pop
DESY Zeuthen, RZ group
Email: (E-Mail Removed)

Mike Wahler
Guest
Posts: n/a

 05-12-2004

"Eric" <(E-Mail Removed)> wrote in message
news:1gdo7jq.3gkwmyn064qoN%(E-Mail Removed)...
> 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

Andrey Tarasevich
Guest
Posts: n/a

 05-12-2004
Mike Wahler wrote:
> ...
> 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)

--
Best regards,
Andrey Tarasevich

CBFalconer
Guest
Posts: n/a

 05-12-2004
Dan Pop wrote:
> (E-Mail Removed) (Eric) writes:
>
>> 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.

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

--
A: Because it fouls the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

E. Robert Tisdale
Guest
Posts: n/a

 05-12-2004
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

x ? y : z

or

x?y:z

and write

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

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

x+y
and

x*y

x * y

for example.
But you may wish to write

A[i+1][j-1]

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)

( x )

or

(x )

or

( x)

and write

[x]

[ x ]

or

[x ]

or

[ x]

for example.
There are, of course, exceptions

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;

i = 1;

to declare and initialize integer i and write

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

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
}

if(condition) {
// statements
}

and

while (condition) {
// statements
}

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

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

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.

Alan Balmer
Guest
Posts: n/a

 05-12-2004
On Wed, 12 May 2004 18:53:33 GMT, CBFalconer <(E-Mail Removed)>
wrote:

>Dan Pop wrote:
>> (E-Mail Removed) (Eric) writes:
>>
>>> 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.

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

Another vote for AJ - that's how I've configured my editor to do it.

--
Al Balmer
Balmer Consulting
(E-Mail Removed)

Eric
Guest
Posts: n/a

 05-12-2004
Dan Pop <(E-Mail Removed)> wrote:

> In <1gdo7jq.3gkwmyn064qoN%(E-Mail Removed)> (E-Mail Removed)
> (Eric) writes:
>
> >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.

Not interested in discussion about what is best style.

I am only interested in the most preferred style based upon the rankings

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

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?

Eric
Guest
Posts: n/a

 05-12-2004
Mike Wahler <(E-Mail Removed)> wrote:

> "Eric" <(E-Mail Removed)> wrote in message
> news:1gdo7jq.3gkwmyn064qoN%(E-Mail Removed)...
> > 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]();
> }
>
> -

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

Martin Ambuhl
Guest
Posts: n/a

 05-12-2004
Eric wrote:
> 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.