[comp.lang.c] C style peeve

jgk@osc.COM (Joe Keane) (03/17/90)

Here's my biggest C style peeve.  For some reason, many C programmers insist
on always putting parentheses around return values, even when they're not
necessary, which happens to be always.  God knows why, but they write
`return(2+2);' instead of the obvious and K&R-approved style `return 2+2;'.

It's a shame the compiler lets them get away with it, and i think the extra
parentheses should be strictly illegal.  Unfortunately, for some reason the
ANSI committee didn't like this suggestion.  Apparently it would break
existing code, you know the same code that assumes you can determine whether
your program is demand-pages by examining the contents of word 0.

So why do people do this?  I guess they think return is a function, although
you never know what's inside some people's heads.  I think they don't
understand what a control structure is, and what the parentheses in if, while,
and switch statements are for.  I wonder how many times they write `break();'
or `goto(label);' and scratch their heads trying to figure out what's wrong
with the compiler.

I know there are a lot of people that do this, so i'm probably putting my life
in jeopardy by making this post.  I'm sure some of you will be insulted by my
criticism of your coding style, and will get all defensive.  I've heard all
the defenses, like ``it makes it clearer'' (yeah right!), so don't bother
posting yours.  Just think about it.  Thanks.  Oh yeah, the second paragraph
is completely false.

henry@utzoo.uucp (Henry Spencer) (03/18/90)

In article <2205@osc.COM> jgk@osc.COM (Joe Keane) writes:
>Here's my biggest C style peeve.  For some reason, many C programmers insist
>on always putting parentheses around return values...
>So why do people do this? ...

Partly because it used to be required, long ago, and partly because most
of the other C control structures that contain expressions still require it
and so people are used to it.  There might also be some PL/I influence on
some of the newcomers.
-- 
MSDOS, abbrev:  Maybe SomeDay |     Henry Spencer at U of Toronto Zoology
an Operating System.          | uunet!attcan!utzoo!henry henry@zoo.toronto.edu

ark@alice.UUCP (Andrew Koenig) (03/18/90)

In article <2205@osc.COM>, jgk@osc.COM (Joe Keane) writes:

> It's a shame the compiler lets them get away with it, and i think the extra
> parentheses should be strictly illegal.  Unfortunately, for some reason the
> ANSI committee didn't like this suggestion.

For one thing, it would be the only place in the entire language
where it would not be legal to wrap a pair of parentheses around
an expression without changing its meaning.

That is,  (x)  is a perfectly good expression, so why shouldn't I
be able to use it in a return statement, as

	return  (x)  ;

??

A historical note: parentheses are required in return statements in
PL/I.  This is because PL/I keywords are not reserved words, so the
parentheses are necessary to resolve the following ambiguity:

	return (x) = (y);

Does this return the Boolean value of the comparison between x and y,
or does it set element number x of an array named `return' to y?
-- 
				--Andrew Koenig
				  ark@europa.att.com

darcy@druid.uucp (D'Arcy J.M. Cain) (03/19/90)

In article <2205@osc.COM> jgk@osc.COM (Joe Keane) writes:
>Here's my biggest C style peeve.  For some reason, many C programmers insist
>on always putting parentheses around return values, even when they're not
>necessary, which happens to be always.  God knows why, but they write
>`return(2+2);' instead of the obvious and K&R-approved style `return 2+2;'.
>
Boy, some people will complain about anything.

-- 
D'Arcy J.M. Cain (darcy@druid)     |   Thank goodness we don't get all 
D'Arcy Cain Consulting             |   the government we pay for.
West Hill, Ontario, Canada         |
(416) 281-6094                     |

scjones@sdrc.UUCP (Larry Jones) (03/19/90)

In article <2205@osc.COM>, jgk@osc.COM (Joe Keane) writes:
> [ return(2+2) instead of return 2+2 ]
> 
> It's a shame the compiler lets them get away with it, and i think the extra
> parentheses should be strictly illegal.  Unfortunately, for some reason the
> ANSI committee didn't like this suggestion.

Say what?!?  The thing after the return keyword in an
>expression<.  Expressions, last I heard, are allowed to contain
parentheses -- even redundant ones.  Do you want to disallow all
redundant parentheses?  Some redundant parentheses?  What?

Personally, I find the idea morally repugnant.  I happen to agree
with you that the parentheses on return statements are ugly and
people who use them should be severely teased, but trying to
change the language to enforce a particular style is way out of
line.
----
Larry Jones                         UUCP: uunet!sdrc!scjones
SDRC                                      scjones@SDRC.UU.NET
2000 Eastman Dr.                    BIX:  ltl
Milford, OH  45150-2789             AT&T: (513) 576-2070
"You know how Einstein got bad grades as a kid?  Well MINE are even WORSE!"
-Calvin

raymond@hilbert.berkeley.edu (Raymond Chen) (03/19/90)

In article <2205@osc.COM>, jgk@osc.COM (Joe Keane) writes:
> It's a shame the compiler lets them get away with it, and i think the extra
> parentheses should be strictly illegal. ...

In many subsequent articles people reply:
> [ You blathering idiot!  The thing after the "return" keyword
>   is an expression, and the last time I checked, an expression
>   enclosed in parentheses is still an expression. ]

Unfortunately, those people went off half-cocked and didn't finish
reading Mr. Keane's article.  His last sentence is particularly
illuminating:

>                                              Oh yeah, the second paragraph
>is completely false.

'Nuff said.
--
raymond@math.berkeley.edu     Maintainer of the csip Frequently Asked Questions

eric@snark.uu.net (Eric S. Raymond) (03/20/90)

In <1990Mar18.005833.12944@utzoo.uucp> Henry Spencer wrote:
> In article <2205@osc.COM> jgk@osc.COM (Joe Keane) writes:
> >Here's my biggest C style peeve.  For some reason, many C programmers insist
> >on always putting parentheses around return values...
> >So why do people do this? ...
> 
> Partly because it used to be required, long ago, and partly because most
> of the other C control structures that contain expressions still require it
> and so people are used to it.  There might also be some PL/I influence on
> some of the newcomers.

I was a `second-generation' C programmer -- I date back to the waning days of
V7. I have the quirk Mr. Keane dislikes, and I think it's because it `used to
be required' -- but I didn't know that when I was learning, I just emulated
the style of the good C code I had around as examples.

I use monocase function and variable names and avoid bitfields and enums for
the same reason, early habits that have just stuck with me. I suspect the same
is true of many C programmers besides myself. No PL/I hypothesis required!

My indentation style is still influenced by previous years of Pascal coding, 
but that's another story...
-- 
      Eric S. Raymond = eric@snark.uu.net    (mad mastermind of TMN-Netnews)

richard@aiai.ed.ac.uk (Richard Tobin) (03/20/90)

In article <2205@osc.COM> jgk@osc.COM (Joe Keane) writes:
>Here's my biggest C style peeve.  For some reason, many C programmers insist
>on always putting parentheses around return values, even when they're not
>necessary, which happens to be always.

When this question came up some years ago, someone posted an excellent
fake manual page for return(3).  Unfortunately I no longer have a copy -
perhaps someone could repost it?

-- Richard
-- 
Richard Tobin,                       JANET: R.Tobin@uk.ac.ed             
AI Applications Institute,           ARPA:  R.Tobin%uk.ac.ed@nsfnet-relay.ac.uk
Edinburgh University.                UUCP:  ...!ukc!ed.ac.uk!R.Tobin

decot@hpisod2.HP.COM (Dave Decot) (03/20/90)

/ hpisod2:comp.lang.c / jgk@osc.COM (Joe Keane) /  6:14 pm  Mar 16, 1990 /

> Oh yeah, the second paragraph is completely false.
----------

Whoops.  Nobody reads anymore, do they...

Dave

boyne@hplvli.HP.COM (Art Boyne) (03/23/90)

jgk@osc.COM (Joe Keane) writes:
>Here's my biggest C style peeve.  For some reason, many C programmers insist
>on always putting parentheses around return values, even when they're not
>necessary, which happens to be always.  God knows why, but they write
>`return(2+2);' instead of the obvious and K&R-approved style `return 2+2;'.

While 'return value;' is legal according to K&R1 (see page 203), *every*
example in my copy of the book uses 'return (value);'.  Pretty obvious,
therefore, why people code that way.  Also, 'while', 'for', 'if', etc.,
all *require* parenthesis, so it's a reasonable habit to acquire.

Art Boyne, boyne@hplvla.hp.com

brnstnd@stealth.acf.nyu.edu (04/08/90)

In article <1060002@hparc0.HP.COM> graham@hparc0.HP.COM (Graham Eddy) writes:
  [ return(expr) versus return expr ]
> Are you really arguing that the extra two chars

You mean the extra one char. Or three keypresses, under wimpy editors.

---Dan