473,883 Members | 1,663 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 3541
"Jim Lambert" <ja*********@fu trx.com> wrote in
news:b6******** ********@eagle. america.net:
No, I mean I would write...
And here comes junior programmer dude... "I'll just add another pointer
type here..."

type* p, anotherPointer;
type* q;
type* r;


"Hey, I got a 'type' instead of a 'pointer to type', what gives?"

--
- Mark ->
--
Nov 14 '05 #21
"Mark A. Odell" <no****@embedde dfw.com> wrote in message
news:Xn******** *************** *********@130.1 33.1.4...
"Jim Lambert" <ja*********@fu trx.com> wrote in
news:b6******** ********@eagle. america.net:
No, I mean I would write...


And here comes junior programmer dude... "I'll just add another pointer
type here..."

type* p, anotherPointer;
type* q;
type* r;


"Hey, I got a 'type' instead of a 'pointer to type', what gives?"


True, JPD could do that but he could just as easily do it with the * being
next to the p instead of the type. :-)

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

Why re-writing something that exists and is free ?-)

Bruno

Nov 14 '05 #23
Papadopoulos Giannis wrote:
Which do you think is best?

1.
a) type* p;
b) type *p;
b if you write C code (in C++, the idiom is a)
2.
a) return (var);
b) return(var);
c) return var;
c, of course.
3.
a) return (ptr->var);
b) return(ptr->var);
c) return ptr->var;
c, of course.
4.
a) return (foo(ptr->var));
b) return(foo(ptr->var));
c) return foo(ptr->var);
c, of course.
5.
a) a = (b+c);
b) a=(b+c);
c) a = b+c;
d) a=b+c;
None. The One Right Way(tm) is
a = b + c;

6.
(snip)
d)
type foo(type arg1, type arg2, ...)
{
declarations;

code;

return;
}


(snip)

But all this is pretty
1/ a matter of taste
2/ more or less language specific

Bruno

Nov 14 '05 #24

"Richard Bos" <rl*@hoekstra-uitgeverij.nl> wrote in message
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().

So there are exceptions. As a rule of thumb, however, if a function calls
another function that returns a value, then the caller is the appropriate
level to process that value - not simply to return it to a higher level.
If you need to prepare data for foo() then that would be an indication that
foo() is badly written. Just an indication, mind you, there might be some
cases where the problems of formatting input neatly are so deep-rooted that
foo() needs a setup function..
My recent MiniBasic program was over 3000 lines. I don't think there is a
single case of returning a sub-function's value directly in all that code.
Nov 14 '05 #25
"Malcolm" <ma*****@55bank .freeserve.co.u k> wrote:

"Richard Bos" <rl*@hoekstra-uitgeverij.nl> wrote in message
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().

So there are exceptions. As a rule of thumb, however, if a function calls
another function that returns a value, then the caller is the appropriate
level to process that value - not simply to return it to a higher level.


Any reasons for that statement?
If you need to prepare data for foo() then that would be an indication that
foo() is badly written.


Nonsense. Look at the example I gave: it could easily be an indication
that foo() can work on any data, and the calling function uses it to
work on specific data.

Richard
Nov 14 '05 #26
On Tue, 27 Jan 2004 22:57:04 -0000, "Malcolm"
<ma*****@55bank .freeserve.co.u k> wrote:

"Richard Bos" <rl*@hoekstra-uitgeverij.nl> wrote in message
> 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().

So there are exceptions. As a rule of thumb, however, if a function calls
another function that returns a value, then the caller is the appropriate
level to process that value - not simply to return it to a higher level.
If you need to prepare data for foo() then that would be an indication that
foo() is badly written. Just an indication, mind you, there might be some
cases where the problems of formatting input neatly are so deep-rooted that
foo() needs a setup function.


Bullshit. There are many useful and valid reasons to return foo().
My recent MiniBasic program was over 3000 lines. I don't think there is a
single case of returning a sub-function's value directly in all that code.


My current application is over 1,000,000 lines. And there are quite a
few functions that return foo(), for various reasons (some elegant,
some for readability, and some just old and gnarly). So there!

- Sev

Nov 14 '05 #27
Richard Bos wrote:
"Malcolm" <ma*****@55bank .freeserve.co.u k> wrote:
.... snip ...
Any reasons for that statement?
If you need to prepare data for foo() then that would be an
indication that foo() is badly written.


Nonsense. Look at the example I gave: it could easily be an
indication that foo() can work on any data, and the calling
function uses it to work on specific data.


Example:

/* function with side effects returning 0/1 for success/fail */
int foo(/* params */)
{
...
if (conditions) return 0;
else if (failureconditi on) return 1;
else {
....
return foo(/* revised params */);
}
}

Yes, it can easily have recursion removed.

--
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 #28
On Tue, 27 Jan 2004 22:57:04 -0000, "Malcolm"
<ma*****@55bank .freeserve.co.u k> wrote:
If you need to prepare data for foo() then that would be an indication that
foo() is badly written.


No, it simply means that the data you have is not appropriate for
foo() without preparation. foo() may be common processing for data in
more than one form, or foo() may be a standard library function.

--
Al Balmer
Balmer Consulting
re************* ***********@att .net
Nov 14 '05 #29

"Richard Bos" <rl*@hoekstra-uitgeverij.nl> wrote in message
So there are exceptions. As a rule of thumb, however, if a function > > calls another function that returns a value, then the caller is the appropriate level to process that value - not simply to return it to a
higher level.
Any reasons for that statement?

Sure. The basic idea of structured programming is that the program is
divided into a hierarchy of functions, becoming more general-purpose as you
move down the hierachy, more specific as you move up.
The other principle is that each function performs one closely-defined task,
and is a black box to the function that calls it.
This means that if we have a function bar() that calls foo(), and barb()
calls bar(), barb() shouldn't know that bar() is calling foo().
This means that barb() shouldn't know the intricacies of foo(). Any change
to foo() shouldn't imply a change in barb(), but only in bar() (where it is
unavoidable).
This does not absolutely preclude returning a result directly from foo() in
barb(), but it does mean that occasions for doing so will be rare, and can
be used as a marker for code that is likely to be poorly-designed, because
the function hierarchy isn't abstracting foo() from bar().

Let's give a concrete example.

void barb()
{
char *str;

str = bar();
if(!str)
out_of_memory_e rror();
}

char *bar()
{
return foo("duplicate this string\n");
}

char *foo(char *str)
{
char *answer = malloc(strlen(s tr)+1);
strcpy(answer, str);
return answer;
}

Now let's modify foo(). Let's say that we only handle strings of ten
characters or less. If passed more than ten, return 0.
Now you will notice that barb() is now broken, because it is reporting an
out-of-memory condition when in fact the error is "string too long". However
barb() doesn't call foo(), so a search for all instances of the identifier
foo() won't pick this up.
Of course in a short program we can easily correct the error, and in a long
program bar() should be correctly-documented (but how likely is the
programmer who wrote bar() to say "returns 0 on error" when he really meant
"returns 0 on out-of-memory"?).
By not handling the return value at the correct level you are spoiling the
structure of your program.
If you need to prepare data for foo() then that would be an
indication that foo() is badly written.


Nonsense. Look at the example I gave: it could easily be an indication
that foo() can work on any data, and the calling function uses it to
work on specific data.

It means that foo() is too general for the real caller to use. The function
cinterp(char *cfunction) which runs a c interpreter on arbitrary source is
completely general, but also not much use.
Now there can be good reasons for providing a very general function. For
instance you could impement sqrt() as return pow(x, 0.5). However see the
problem - sqrt() is a special case of exponentiation, and it can be
implemented a lot more efficiently. If you already have a working pow() nad
you don't care about run-time efficiency then go ahead. This is not an
untypical case. Take quad( ... four points ... ) axis_aligned_re ctangle( ...
two points ...) could be implemented as a call to quad(), but again this
only makes sense in the special circumstances of having a working quad() and
no access to its display buffer.
Microsoft have provided a very customisable OpenFile() dialog. For programs
intended for distribution to end users it probably makes sense. However in a
testing and quick tools environment you generally want to write fname =
getopenfile(), rather than go to the help pages to see how to set up a
complex input structure. So arguably the function is badly-designed.
Nov 14 '05 #30

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
9933
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
9781
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
11121
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
10407
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...
1
7960
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
7114
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
5793
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
4210
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3230
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.