473,902 Members | 4,610 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 3545

"E. Robert Tisdale" <E.************ **@jpl.nasa.gov > wrote in message
news:40******** ******@jpl.nasa .gov...
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,


Well I'm assuming you'd have one statement for constants and one for not.
E.g.

const char *p, c;
char q, *x;
2. your variables are undefined and
So far. That's just the declaration my friend.
3. your variables are undocumented.
True that. But it depends. Sometimes variables are trivial, e.g. "int x"
would often be just a "I want to step through an array".
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.


Your style is just plain messy. Try writing a function that has say 10 or
so variables. Now try maintaining 400 similar functions ;-). Giving them
values initially is just plain bad coding [hint constants other than trivial
ones should be #define not set in local variables].

I think this is just another "bad advice post" which you seem good at...

Tom
Nov 14 '05 #41
Papadopoulos Giannis wrote:
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??


As Joona has said, because of his virtually universal bad advice,
and especially because of his habit of altering quotations of
other posters. This means he has to be watched and rebutted
continuously, just so newbies such as yourself don't get
misinformed. We can't just plonk and forget him. Like
BullSchildt, he often sounds good to the uninformed.

--
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 #42
On Thu, 29 Jan 2004 12:25:18 +0000, CBFalconer wrote:
Papadopoulos Giannis wrote:
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??


As Joona has said, because of his virtually universal bad advice, and
especially because of his habit of altering quotations of other posters.
This means he has to be watched and rebutted continuously, just so
newbies such as yourself don't get misinformed. We can't just plonk and
forget him. Like BullSchildt, he often sounds good to the uninformed.


For what it's worth, as a lurker who seldom posts, I don't hate Tisdale at
all. He's one of the posters I sometimes look for in clc, if he's gotten a
response from a clued regular. He is most certainly a troll, and most of
his "advice" is ludicrous, but this means he often provides a laugh. Also,
because (as Chuck says) clued regulars rebut him, newbies get to see some
good advice as a result of ERTroll's bad advice. OTOH, his dishonest habit
of editing others' posts is disturbing. It's one thing to know that you
shouldn't trust what he says. It's much worse to know that you can't trust
that people have said what he quotes them as saying. Perhaps he could get
a job with a major U.S. newspaper.

Nov 14 '05 #43
Papadopoulos Giannis wrote:

<snip>
Why is mr. Tisdale hated so much??
"Hate" is, in my opinion, too strong a word. Suffice to say that his
knowledge of C is rather shaky, compared to his determination to give
advice about it. If he could only learn, he might even become... well,
given his past track-record, there is no point in speculating on that
possibility.
PS. I am new to comp.lang.c


For a while at least, read every article you find. After a while, you'll get
a feel for who knows what they're talking about and who doesn't.

--
Richard Heathfield : bi****@eton.pow ernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 14 '05 #44
Tom St Denis wrote:
Well I'm assuming you'd have one statement for constants and one for not.
E.g.

const char *p, c; cat main.c #include <stdio.h>

int main(int argc, char* argv[]) {
const char *p, c;
p = NULL;
c = 'c';
return 0;
}
gcc -Wall -std=c99 -pedantic -o main main.c

main.c: In function `main':
main.c:6: warning: assignment of read-only variable `c'

Nov 14 '05 #45

On Thu, 29 Jan 2004, 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.


Yeah, sometimes that works for me, too. ;-) I was just reading
on autopilot, and was jolted awake by the incredibly bad advice
Trollsdale was dishing out.

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)


Funny, I'd say the opposite -- the association of the "*" is
absolutely, positively NON-controversial! It's dictated by an
international standard, for Pete's sake! ;) The controversial bit
of your post was where you seemed to advocate writing

char c, *p, q, *x;

or whatever it really was; I'm not going to take the time to look
back right now. I maintain that this is a Bad Idea.
Also, its not contrived -- go download Bstrlib and see for yourself.
I don't see any Bad Idea declarations in the CVS tree for bstrlib
at first glance. You do use 'int i,j;' and 'int i,v,n;' several times,
but I hope you can appreciate that declaring two or three closely
related objects of the same type on a single line is a far cry from
declaring four or five objects of different types on the same line!
As it happens, I *would* have re-written

bstring bBase64Encode (const bstring b) {
int i, c0, c1, c2, c3;
as
bstring bBase64Encode (bstring b)
{
int c0, c1, c2, c3;
int i;

which IMHO even without the (IMVHO) more-readable indentation makes
it more obvious that 'i' is unrelated to 'c0' etc. [And yes, IMO
it does *not* make more sense to use a four-element array here,
just in case anyone was going to piggy-back on this post. ;-) ]

But when I was talking about Bad Ideas, I was thinking of the common
newbie and ancient-scientific-code practice of writing

/* 3d************* *@news.tin.it */
int compare(FILE* , FILE* , long* , long* ), j;

/* 6e************* *************** **...gramm ing.com */
float sig_in[30], numerator[3], denominator[3], x[3], y[2], s_out[30];

which is just plain write-only code.

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.


If you remove the line I underscored above, your comment makes sense
and is true. I don't know why you added that second line, though --
it doesn't make any sense at all. Define variables when you need
them, and not [too far] before. As a corollary, if you find you need
an arbitrarily complex initialization scheme, it makes sense to put
that initialization in a function and write

struct myADT foo;
myADT_initializ e(&foo, bar, baz, quux);

bstrlib does not seem at a glance to suffer from this problem, though.

-Arthur
Nov 14 '05 #46
Arthur J. O'Dwyer wrote:
Define variables when you need them, and not [too far] before.
As a corollary,
if you find you need an arbitrarily complex initialization scheme,
it makes sense to put that initialization in a function and write

struct myADT foo;
myADT_initializ e(&foo, bar, baz, quux);


This is very bad advice because you can't define a const struct myADT.
Instead, you should implement a "pseudoconstruc tor":

struct myADT myADT_create(Ba r, Baz, Quux);

so that you can define a constant:

const struct myADT foo = myADT_create(ba r, baz, quux);

Or you could use it to define a variable:

struct myADT foo = myADT_create(ba r, baz, quux);

and define a function

struct myADT* myADT_modify(st ruct myADT*, Bar, Baz, Quux);

so that you can *modify* the variable:

myADT_modify(&f oo, bar, baz, quux);

Note that the myADT_modify function returns a pointer to foo so that
it can be used in any expression where &foo might otherwise appear
subsequent to the application of myADT_modify.

Nov 14 '05 #47

On Thu, 29 Jan 2004, E. Robert Tisdale wrote:

Arthur J. O'Dwyer wrote:

struct myADT foo;
myADT_initializ e(&foo, bar, baz, quux);
This is very bad advice because you can't define a const struct myADT.
Instead, you should implement a "pseudoconstruc tor":

struct myADT myADT_create(Ba r, Baz, Quux);

so that you can define a constant:

const struct myADT foo = myADT_create(ba r, baz, quux);


That's good advice. I myself religiously avoid returning struct
types by value, and I also rarely 'const'ify anything anyway
(preferring to #define CONSTANT if it's never going to change, or
make a static global 'Constant' if the user might want to change
it, or make it a parameter if it's local to a function). However,
*if* one wanted to make a 'const struct myADT', this would be an
excellent way to do so.

Or you could use it to define a variable:

struct myADT foo = myADT_create(ba r, baz, quux);

and define a function

struct myADT* myADT_modify(st ruct myADT*, Bar, Baz, Quux);

so that you can *modify* the variable:

myADT_modify(&f oo, bar, baz, quux);

Note that the myADT_modify function returns a pointer to foo so that
it can be used in any expression where &foo might otherwise appear
subsequent to the application of myADT_modify.


Yes. I tend to use this more C++-looking idiom with pointers,
occasionally:

struct myADT *foo = new_myADT(bar, baz, quux);
myADT_modify(fo o, bar, baz, quux);
delete_myADT(fo o);

but it is a widely followed practice to let functions operate
on "buffers" rather than "OO objects" when writing in C. That
is, it becomes the caller's responsibility to allocate space for
a "buffer" object, on which the function operates; rather than
the function's responsibility to allocate a temporary struct
object and then copy the data over to the caller [which in C is
done automagically, but still has to happen somewhere].
I hope that makes sense. IOW: both ways work, and have their
uses, but I find my way more sensible more often -- in my code,
of course.

-Arthur
Nov 14 '05 #48
Arthur J. O'Dwyer wrote:
I tend to use this more C++-looking idiom with pointers,
occasionally:

struct myADT *foo = new_myADT(bar, baz, quux);
myADT_modify(fo o, bar, baz, quux);
delete_myADT(fo o);

but it is a widely followed practice to let functions operate
on "buffers" rather than "OO objects" when writing in C.
That is, it becomes the caller's responsibility to allocate
space for a "buffer" object, on which the function operates;
rather than the function's responsibility
to allocate a temporary struct object
and then copy the data over to the caller
[which, in C, is done automagically
but still has to happen somewhere]. I hope that makes sense.


That's just the point.
It isn't necessary for the C *programmer*
to explicitly allocate a "buffer" object.
This is done automatically by your optimizing C *compiler*.
Almost all modern C compilers implement
the Named Return Value Optimization (NRVO).
The function does *not* "allocate a temporary struct object
and then copy the data over to the caller".
Instead, it recognizes the temporary
as a reference to the return value and initializes it directly.
In the definition of object foo:

struct myADT foo = myADT_create(ba r, baz, quux);

the optimizing C compiler allocates [automatic] storage for foo
then passes a pointer to foo as a [hidden] argument to myADT_create
which is used to reference to the return value.
*No* copies of a struct myADT are ever required.

No delete_myADT function is required
but the implementation should provide a function

void myADT_destroy(c onst struct myADT* p) {
}

which should be called

myADT_destroy(& foo);

before the thread of execution passes out of the scope of foo
in case the struct myADT is ever redefined to include a pointer
to an object allocated from free storage when it is created.

Note that you still need myADT_new (and myADT_delete)
to create objects that must survive the current scope.

Nov 14 '05 #49
> char c, *p, q, *x;

or whatever it really was; I'm not going to take the time to look
back right now. I maintain that this is a Bad Idea.


What do you think of
char , *p[], q(), *x;
then? :)

Recently I wrote some code like:

const unsigned char s[] = {
foo(1),
foo(2),
/* ... (about 10 entries) */
}, *ps = s;

so that later on I could iterate with ps, or use the results
from the functioncalls in many places. Using this syntax
avoided having to type out "const unsigned char" twice (or use typedef)
Nov 14 '05 #50

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

Similar topics

1
5729
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
2017
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
2558
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
7006
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
2071
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
2142
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
9997
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
9845
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
11279
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
10870
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
10981
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
10499
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
7205
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
6085
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3323
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.