473,656 Members | 2,824 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Coding style survey

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;
}

Nov 14 '05 #1
63 3490
In article <bv***********@ ulysses.noc.ntu a.gr>,
Papadopoulos Giannis <ip******@inf.u th.gr> wrote:
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.
Nov 14 '05 #2
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.

Papadopoulos Giannis wrote:
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;
}


--
"It is impossible to make anything foolproof because fools are so
ingenious" - A. Bloch

Nov 14 '05 #3
Nick Landsberg wrote:
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)...

Nov 14 '05 #4

"Papadopoul os Giannis" <ip******@inf.u th.gr> wrote in message
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.
Nov 14 '05 #5


Malcolm wrote:
"Papadopoul os Giannis" <ip******@inf.u th.gr> wrote in message
Which do you think is best?
{snip} <--- not curly braces :)6.
a)
type foo(type arg1, type arg2, ...) {
declaration s;
code;
return;
}
b)
type foo(type arg1, type arg2, ...) {
declaration s;

code;

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

code;

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

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.

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.

--
"It is impossible to make anything foolproof because fools are so
ingenious" - A. Bloch

Nov 14 '05 #6
Papadopoulos Giannis wrote:
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*accelerati on;

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.

Nov 14 '05 #7
On Tue, 27 Jan 2004 00:15:20 +0200, Papadopoulos Giannis
<ip******@inf.u th.gr> wrote in comp.lang.c:
Which do you think is best?


What I think is that people who ask about "style" issues are asking
the wrong question.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #8
Malcolm wrote:

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

--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!
Nov 14 '05 #9
Papadopoulos Giannis <ip******@inf.u th.gr> wrote:
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
Nov 14 '05 #10

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

Similar topics

1
5703
by: GTF | last post by:
PHP Web Survey Idea.. I have been given an opportunity to create a web based survey. This is a fairly lengthy survey of 60 pages on paper (various multiple choice and free form). These are the Requirements: -Provide a web interface to a database -Database stores the data (duh), but the capacity to extract the data
1
1996
by: Nico Baumgarten | last post by:
Dear Madam/Sir, You are invited to participate in an international research study. This research project is headed and led by Cambridge student Nico Baumgarten. What is it all about? It is a well-known fact that there exist many differences between cultures. However, how these differences effect motivation if there are effects at all is not yet clear. This survey is set out to change this. With your help, it will be possible to...
18
2539
by: craig | last post by:
I am curious about how many of you prefer style 1 vs. style 2, and why. Are there names for these style? style 1: method { }
144
6842
by: Natt Serrasalmus | last post by:
After years of operating without any coding standards whatsoever, the company that I recently started working for has decided that it might be a good idea to have some. I'm involved in this initiative. Typically I find that coding standards are written by some guy in the company who has a way of coding that he likes and then tries to force everybody else to write code the way he likes it, not for any rational reason, but simply for the...
2
1200
by: David | last post by:
Hello all -- Is it possible to code XML to launch a survey/ad when a user clicks on a listed podcast? In other words, you are subscribed to myfeed.xml. You click on a podcast listing. Is it possible for this click to also launch a popup window or a video stream, in addition to launching the traditional audio stream? If yes, where could I find information on how to code such a feature?
13
2053
by: benben | last post by:
Is there an effort to unify the c++ coding standard? Especially identifier naming. Not a big issue but it would be annoying to have to incorporate different coding styles simultaneously when using more than one library. The standard library seems to have everything lower-cased while a lot of other libraries do their own way. Ben
0
2101
by: Janet93 | last post by:
If you are involved in the development of scientific computing software, you are invited to participate in a survey on developing this kind of software. If you have already received this request, I apologize for the cross-posting, but I am attempting to advertise to as many developers as possible. I would appreciate it if you could take 20-30 minutes to complete this questionnaire. If you know others involved in the development of...
0
8382
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8297
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8816
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8717
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8498
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7311
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4300
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1930
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1600
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.