Coding style survey

  • Thread starter Papadopoulos Giannis
  • Start date
P

Papadopoulos Giannis

Which do you think is best?

1.
a) type* p;
b) type *p;

2.
a) return (var);
b) return(var);
c) return var;

3.
a) return (ptr->var);
b) return(ptr->var);
c) return ptr->var;

4.
a) return (foo(ptr->var));
b) return(foo(ptr->var));
c) return foo(ptr->var);

5.
a) a = (b+c);
b) a=(b+c);
c) a = b+c;
d) a=b+c;

6.
a)
type foo(type arg1, type arg2, ...) {
declarations;
code;
return;
}
b)
type foo(type arg1, type arg2, ...) {
declarations;

code;

return;
}
c)
type foo(type arg1, type arg2, ...)
{
declarations;
code;
return;
}
d)
type foo(type arg1, type arg2, ...)
{
declarations;

code;

return;
}
e)
type foo(type arg1, type arg2, ...)
{
declarations;
code;
return;
}
f)
type foo(type arg1, type arg2, ...)
{
declarations;

code;

return;
}
 
C

Christian Bau

Papadopoulos Giannis said:
Which do you think is best?

1.
a) type* p;
b) type *p;

It depends on the situation. What is the important object: The pointer
p, or the object pointed to *p ? Occasionally you will see things like

char* *p;

which means that p points to a char-pointer.
2.
a) return (var);
b) return(var);
c) return var;
c

3.
a) return (ptr->var);
b) return(ptr->var);
c) return ptr->var;
Same

4.
a) return (foo(ptr->var));
b) return(foo(ptr->var));
c) return foo(ptr->var);

Same, but it should be foo (ptr->var)
5.
a) a = (b+c);
b) a=(b+c);
c) a = b+c;
d) a=b+c;

None of the above. a = b + c;
6.
a)
type foo(type arg1, type arg2, ...) {
declarations;
code;
return;
}
b)
type foo(type arg1, type arg2, ...) {
declarations;

code;

return;
}
c)
type foo(type arg1, type arg2, ...)
{
declarations;
code;
return;
}
d)
type foo(type arg1, type arg2, ...)
{
declarations;

code;

return;
}
e)
type foo(type arg1, type arg2, ...)
{
declarations;
code;
return;
}
f)
type foo(type arg1, type arg2, ...)
{
declarations;

code;

return;
}

None of the above. (b) with an empty line before declarations.
 
N

Nick Landsberg

It's a matter of personal preference.

If someone doesn't like the way you do it, let them
write a "pretty printer" to reformat it.

If the company you are working for has a standard for any of
these, comply with their standard. It has nothing to do
with the correctness of the code, but may have impact
 
P

Papadopoulos Giannis

Nick said:
It's a matter of personal preference.

If someone doesn't like the way you do it, let them
write a "pretty printer" to reformat it.

If the company you are working for has a standard for any of
these, comply with their standard. It has nothing to do
with the correctness of the code, but may have impact
on how the code will be maintained in the future.

I know.. I have already my style (which is by the way a bit peculiar)..
I just wanted to know how other c programmers code (either experts or
novice)...
 
M

Malcolm

Papadopoulos Giannis said:
Which do you think is best?

1.
a) type* p;
b) type *p;
a) is clearer, since p is a type*. However b) is traditional, because of C's
rules on building compound types. See function pointers for further details.
2.
a) return (var);
b) return(var);
c) return var;
c). return is not a function.
3.
a) return (ptr->var);
b) return(ptr->var);
c) return ptr->var;
c), return is not a function.
4.
a) return (foo(ptr->var));
b) return(foo(ptr->var));
c) return foo(ptr->var);
c), return is not a function. Though you want to ask why you are returning
the return value from another function directly - this is unusual.
5.
a) a = (b+c);
b) a=(b+c);
c) a = b+c;
d) a=b+c;
c).
6.
a)
type foo(type arg1, type arg2, ...) {
declarations;
code;
return;
}
b)
type foo(type arg1, type arg2, ...) {
declarations;

code;

return;
}
c)
type foo(type arg1, type arg2, ...)
{
declarations;
code;
return;
}
d)
type foo(type arg1, type arg2, ...)
{
declarations;

code;

return;
}
e)
type foo(type arg1, type arg2, ...)
{
declarations;
code;
return;
}
f)
type foo(type arg1, type arg2, ...)
{
declarations;

code;

return;
}
c) or d). Curly braces should always be on a line of their own and should
always align with the closing curly brace. A space between declarations and
code is generally useful, but |I wouldn't insist on it.
 
N

Nick Landsberg

Malcolm said:
c) or d). Curly braces should always be on a line of their own and should
always align with the closing curly brace. A space between declarations and
code is generally useful, but |I wouldn't insist on it.
It's again only a matter of personal preference and has nothing
to do with the correctness of the code or C, the language.

Back in the stone age of computing I developed a {} style which
put the curly braces on the same line as the "if, for, while, etc."
statements so I could use *nix system utilities to check for
flow of control, e.g. g/[{}]/p in the editor. Of course, this
did not help all the time. It was just a crutch.

If you are in an organization which has certain edicts for
where braces go, and you don't like them, then write your
own "pretty printer" and "un-pretty printer" to go back
and forth from your preferred style to the organization's
style. (Said pretty printer is a pretty good exercise in
learning the c-language, too.) We've had "brace wars" in
C for longer than I care to remember.
 
E

E. Robert Tisdale

Papadopoulos said:
Which do you think is best?

1.
a) type* p;

2.
c) return var;

3.
c) return ptr->var;

4.
c) return foo(ptr->var);

5.
c) a = b + c;

6.
a)
type foo(type arg1, type arg2, ...) {
declarations;
code;
return arg1;
}


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

CBFalconer

Malcolm said:
.... snip ...
c) or d). Curly braces should always be on a line of their own and
should always align with the closing curly brace. A space between
declarations and code is generally useful, but |I wouldn't insist
on it.

I would. And the only code oriented place I insist on a separate
line for an opening brace is immediately after a function header.
Otherwise it belongs on the end of a line. e.g. my style:

int function foo(int bar)
{
/* declarations */

/* code after a blank line, unless no declarations */
if (bar) {
/* barstuff. Indentation shows controlling clause */
}
} /* foo */ /* I insist on labelling the end of a function */
 
R

Richard Bos

Papadopoulos Giannis said:
Which do you think is best?

1.
a) type* p;
b) type *p;

The latter. The former leads people to believe that

int* p1,p2;

declares two pointers to int, which it does not.
2.
a) return (var);
b) return(var);
c) return var;

The third, but this is purely a matter of taste - except that I think
the second option makes it look too much like a function call. IOW, I'd
write the third; wouldn't frown at the first; but would remark upon the
second.
3.
a) return (ptr->var);
b) return(ptr->var);
c) return ptr->var;
4.
a) return (foo(ptr->var));
b) return(foo(ptr->var));
c) return foo(ptr->var);

Ditto, and ditto.
5.
a) a = (b+c);
b) a=(b+c);
c) a = b+c;
d) a=b+c;

C or D, depending on context.
6.
a)
type foo(type arg1, type arg2, ...) {
declarations;
code;
return;
}

No. I'd use this layout, with the opening brace at the end of the line,
for a block inside a function (for, if, etc.), but not for the function
block itself.
c)
type foo(type arg1, type arg2, ...)
{
declarations;
code;
return;
}
d)
type foo(type arg1, type arg2, ...)
{
declarations;

code;

return;
}

One of these; which I choose depends on the size of the function, but
usually D.
e)
type foo(type arg1, type arg2, ...)
{
declarations;
code;
return;
}

*Grk* No. Never. It's weird.

Richard
 
R

Richard Bos

Malcolm said:
c), return is not a function. Though you want to ask why you are returning
the return value from another function directly - this is unusual.

Not at all. The function containing the return statement could be a
function which prepares the data for foo().
For example, take a deque, implemented as a doubly linked list. The DLL,
of course, has a function called dll_delete(). The deque has two
functions, deq_l_delete() and deq_r_delete(). deq_l_delete(q) finds the
leftmost element of q, then passes it to dll_delete(); deq_r_delete(q)
does the same at the other end. The last statement of deq_l_delete()
could be

return dll_delete(leftmost);

Richard
 
J

James Dow Allen

CBFalconer said:
int function foo(int bar)
{
...
if (bar) {
/* barstuff. Indentation shows controlling clause */
}
} ...

Bravo! Glad to see someone adhering to the TRUE STYLE.
From an earlier discussion I thought comp.lang.c'ers thought it was passe.
Which do you think is best?
1.
a) type* p;
b) type *p;

(a) seems more logical, *except for one big problem*:
type* p1, p2;
won't mean what it looks like it should mean.
Use (b) therefore unless you're looking for trouble.

Someone else wrote
it is unusual to return another function's return value.

Hunh? I do it all the time. Admittedly I soemtimes end up
rewriting it, if only for debug purposes.

James
 
G

gswork

Papadopoulos Giannis said:
Which do you think is best? []

a) return (var);
b) return(var);
c) return var;

If it's an expression then i'll return it in brackets else i'll return
the identifier.
a) a = (b+c);
b) a=(b+c);
c) a = b+c;
d) a=b+c;

If you program in languages that have different operator precedence
rules then brackets are helpful for clarity, IMO. I'd hold that
generally too.

The above is not so complex but, e.g., a=b*c/d*e+f can have many
meanings if you intend parts of the equation to be evaluated in a
certain sequence the language's operator precedence differs from.
6.
a) [function structure]

type function(type arg1)
{
declarations;

code;

return;
}

but as long as it's clear i don't mind.
 
P

pete

Papadopoulos said:
Which do you think is best?

1.
a) type* p;
b) type *p;

b, because
type* p, q, r;
makes you want to think
type *p, *q, *r;

2.
a) return (var);
b) return(var);
c) return var;

c, why not ?

3.
a) return (ptr->var);
b) return(ptr->var);
c) return ptr->var;

c, same same.
4.
a) return (foo(ptr->var));
b) return(foo(ptr->var));
c) return foo(ptr->var);

c, same same.
5.
a) a = (b+c);
b) a=(b+c);
c) a = b+c;
d) a=b+c;

e) a = b + c;
 
K

Kurt Watzka

Papadopoulos said:
Which do you think is best?

1.
a) type* p;
b) type *p;

b) for obvious reasons:

type* p, q;
2.
a) return (var);
b) return(var);
c) return var;

c) there is no reason to put parentheses around expressions, otherwise
I would write

return (((var)));
3.
a) return (ptr->var);
b) return(ptr->var);
c) return ptr->var;

c) there is no reason to put parentheses around expressions, otherwise
I would write

return (((ptr)->var));
4.
a) return (foo(ptr->var));
b) return(foo(ptr->var));
c) return foo(ptr->var);

c) here is no reason to put parentheses around expressions, otherwise
I would write

return (((foo)((ptr)->var)));
5.
a) a = (b+c);
b) a=(b+c);
c) a = b+c;
d) a=b+c;

None of the above. a = b + c;
6.
a)
type foo(type arg1, type arg2, ...) {
declarations;
code;
return;
}
b)
type foo(type arg1, type arg2, ...) {
declarations;

code;

return;
}
c)
type foo(type arg1, type arg2, ...)
{
declarations;
code;
return;
}
d)
type foo(type arg1, type arg2, ...)
{
declarations;

code;

return;
}
e)
type foo(type arg1, type arg2, ...)
{
declarations;
code;
return;
}
f)
type foo(type arg1, type arg2, ...)
{
declarations;

code;

return;
}

none of the above

type foo(type arg1, type arg2, ...)
{
type Result;
declarations;

code;

return Result;
}

and don't use \t to indent code that I post.

Kurt
 
J

Jim Lambert

Papadopoulos Giannis said:
Which do you think is best?

1.
a) type* p;
b) type *p;

type* p

I never use the comma operator since I have every variable on it's own line
so I don't have to worry about the "type* p, q" problem.

As for braces, I know there is a "correct" way but doesn't indenting the
braces with the code make more sense?

if (...)
{
code;
}

makes more sense to me since an if() is followed by a statement which may or
may not be a compound statement. If you indent the statement, then the
braces (which are part of the compound statement, not part of the if()
should be indented as well. Also, the above is easier on the eyes to read
since you read the if() then read straight down the code. No jumping back
and forth.

Either way, I know it is personal preference.

Jim
 
T

Tom St Denis

Jim Lambert said:
type* p

I never use the comma operator since I have every variable on it's own line
so I don't have to worry about the "type* p, q" problem.

So you'd write

type* p,
* q,
r;

???

That seems a bit odd...

Tom
 
R

Richard Bos

Jim Lambert said:
type* p

I never use the comma operator since I have every variable on it's own line

Neither do those of use who have more identifiers on a line; those
commas are separators, not the comma operator...
As for braces, I know there is a "correct" way

Well, only for values of correct that amount to "agrees with me". Not
for values of correct that mean "can be proven to be the only right
one".
but doesn't indenting the braces with the code make more sense?

if (...)
{
code;
}

Ew, no! That way you're guaranteed to overlook the braces in a more
complicated compound statement.

Richard
 
M

Mark A. Odell

Which do you think is best?

1.
a) type* p;
b) type *p;
a


2.
a) return (var);
b) return(var);
c) return var;

c

3.
a) return (ptr->var);
b) return(ptr->var);
c) return ptr->var;

c but how is this notably different from 2?
4.
a) return (foo(ptr->var));
b) return(foo(ptr->var));
c) return foo(ptr->var);

c and again...
5.
a) a = (b+c);
b) a=(b+c);
c) a = b+c;
d) a=b+c;

e) a = b + c;
6.
a)
type foo(type arg1, type arg2, ...) {
declarations;
code;
return;
}
b)
type foo(type arg1, type arg2, ...) {
declarations;

code;

return;
}
c)
type foo(type arg1, type arg2, ...)
{
declarations;
code;
return;
}
d)
type foo(type arg1, type arg2, ...)
{
declarations;

code;

return;
}
e)
type foo(type arg1, type arg2, ...)
{
declarations;
code;
return;
}
f)
type foo(type arg1, type arg2, ...)
{
declarations;

code;

return;
}

d
 

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
474,139
Messages
2,570,806
Members
47,353
Latest member
TamiPutnam

Latest Threads

Top