473,883 Members | 1,787 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
63 3543
Papadopoulos Giannis wrote:
1.
a) type* p;
b) type *p;
B
2.
a) return (var);
b) return(var);
c) return var;
B
3.
a) return (ptr->var);
b) return(ptr->var);
c) return ptr->var;
B
4.
a) return (foo(ptr->var));
b) return(foo(ptr->var));
c) return foo(ptr->var);
B
5.
a) a = (b+c);
b) a=(b+c);
c) a = b+c;
d) a=b+c;
None. I use "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;
}


B

--
gabriel
Nov 14 '05 #31
Papadopoulos Giannis wrote:
Which do you think is best?
[...]


LOL! I wrote my own reply, and the started to notice some of the other
replies... Jeez! Some people even pick fights over this without an
opponent!

Gotta loooove C programmers!

--
gabriel
Nov 14 '05 #32
> 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.


The comma in "type* p, q" is NOT an operator.
Nov 14 '05 #33
Papadopoulos Giannis <ip******@inf.u th.gr> wrote:
Which do you think is best?
1.
a) type* p;
b) type *p;
Always the second. This is made clear in when you have multiple
declarers:

char *p, q, c, *x;

If the * were moved next to the type, you can see how this might be
confusing.
2, 3, 4.
a) return (var);
b) return(var);
c) return var;
c) is correct and requires fewer characters. Otherwise there is no
difference.
5.
a) a = (b+c);
b) a=(b+c);
c) a = b+c;
d) a=b+c;
I prefer a = b + c; which I guess is closest to choice c). You should
only use extra parenthesis where one is intending to be explicit about
the order of operations.
6. (spacing, and parentheses for functions ...)


I usually put an extra space between declarations and code, however I
don't add an extra one for the return (especially since a function
might return from any number of places from within the code.) So my
style would be closest to:

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

code;
return ...;
}

--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/
Nov 14 '05 #34
"Malcolm" <ma*****@55bank .freeserve.co.u k> wrote:
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.


Its not unusual, and it might be a macro rather than a function.

--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/
Nov 14 '05 #35
Paul Hsieh wrote:
Papadopoulos Giannis wrote:
Which do you think is best?
1.
a) type* p;
b) type *p;


Always the second.
This is made clear in when you have multiple declarers:

char *p, q, c, *x;


This is considered very *poor* programming style
for a number of reasons:

1. you declare variables where constants may be required,
2. your variables are undefined and
3. your variables are undocumented.

const
char c = 'c'; // character constant
char q = '\0'; // character variable
const
char* const x = "some constant string";
const
char* p = &c; // reseatable pointer to a
// character constant

These *definitions* should be placed as close as possible
to the point where they are first used.

Nov 14 '05 #36

On Wed, 28 Jan 2004, E. Robert Tisdale wrote:

Paul Hsieh wrote:
Papadopoulos Giannis wrote:
a) type* p;
b) type *p;
Always the second.
This is made clear in when you have multiple declarers:

char *p, q, c, *x;


This is considered very *poor* programming style
for a number of reasons:

1. you declare variables where constants may be required,
2. your variables are undefined and
3. your variables are undocumented.

const
char c = 'c'; // character constant


This was about the point where I realized that this must be
another of Trollsdale's joke posts. It's just so blatantly stupid!
[Looking back, I see I should have seen that his "1." was nonsensical,
but he's clever with words that way -- it slipped right by me. :) ]

Paul -- Indeed, the declaration style of intermixing pointers
and scalars in the same line of variable declarations is a Bad
Idea, precisely because it is so confusing and hard to read.
But I think you know this, and were merely giving a contrived
example.

Newbies who have little experience with Trollsdale's joke
posts -- I would hope it's obvious that the following lines of
code are *also* incredibly bad style.
Don't use C++ comments in C code that's supposed to be read
and used by real-world C compilers (which includes all code posted
to this newsgroup). Don't put 'const' on a separate line from
the rest of a declaration. Don't put random tabs before variable
names, unless it's with a very good reason. (Oh, and don't use
hard tabs in Usenet posts to begin with.) Also, don't use the
style of pointer declaration exhibited by Trollsdale's last line,
in which he puts the asterisk way over on the left, where it can
be hidden in the mass of 'const's and 'char's. Oh, and don't use
comments like "character variable" in your code, either.

char q = '\0'; // character variable
const
char* const x = "some constant string";
const
char* p = &c; // reseatable pointer to a
// character constant
These *definitions* should be placed as close as possible
to the point where they are first used.


This is good advice. (That doesn't mean you should assume that
Trollsdale will *always* include a nugget of good advice in each
of his joke posts, though -- quite often he doesn't. You're better
off ignoring his posts.)

HTH,
-Arthur

Nov 14 '05 #37
"Arthur J. O'Dwyer" <aj*@nospam.and rew.cmu.edu> wrote:
On Wed, 28 Jan 2004, E. Robert Tisdale wrote:
char c = 'c'; // character constant
This was about the point where I realized that this must be
another of Trollsdale's joke posts.


Oh, I just read the "From:" part of the message to know that.
[...] It's just so blatantly stupid!
[Looking back, I see I should have seen that his "1." was nonsensical,
but he's clever with words that way -- it slipped right by me. :) ]

Paul -- Indeed, the declaration style of intermixing pointers
and scalars in the same line of variable declarations is a Bad
Idea, precisely because it is so confusing and hard to read.
But I think you know this, and were merely giving a contrived
example.


I disagree, and I think this is at best a matter of opinion. Since
there is nothing controvertial about what the compiler does (except
the issue of the "*" being associated with the variable/type being
declared instead of the defining type) there is nothing specific that
it can be confused with so long as "*"'s are pushed to be adjacent to
the variable.

Also, its not contrived -- go download Bstrlib and see for yourself.
These *definitions* should be placed as close as possible
to the point where they are first used.


This is good advice.


Most good compilers already help you with this potential problem, and
this is not a practical/useful suggestion for a large struct or ADT.
Since initialization, is of arbitrary complexity, attempting to do so
solely within the declaration section is kind of futile in general.

--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/
Nov 14 '05 #38
Paul Hsieh wrote:
"Arthur J. O'Dwyer" <aj*@nospam.and rew.cmu.edu> wrote:
On Wed, 28 Jan 2004, E. Robert Tisdale wrote:
char c = 'c'; // character constant


This was about the point where I realized that this must be
another of Trollsdale's joke posts.

Oh, I just read the "From:" part of the message to know that.


Why is mr. Tisdale hated so much??

PS. I am new to comp.lang.c

Nov 14 '05 #39
Papadopoulos Giannis <ip******@inf.u th.gr> scribbled the following:
Paul Hsieh wrote:
"Arthur J. O'Dwyer" <aj*@nospam.and rew.cmu.edu> wrote:
On Wed, 28 Jan 2004, E. Robert Tisdale wrote:
char c = 'c'; // character constant

This was about the point where I realized that this must be
another of Trollsdale's joke posts.
Oh, I just read the "From:" part of the message to know that.

Why is mr. Tisdale hated so much??


Because he frequently gives bad advice to others. Not only that, but he
sometimes also edits others' posts to make them read what he wanted them
to read.

--
/-- Joona Palaste (pa*****@cc.hel sinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Normal is what everyone else is, and you're not."
- Dr. Tolian Soran
Nov 14 '05 #40

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

Similar topics

1
5726
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
2014
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
2557
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
6999
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
1206
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
2067
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
2139
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
9796
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
10420
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9583
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...
1
7975
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7134
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6002
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4620
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4225
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3239
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.