style(9) question

Greg 'groggy' Lehey grog at FreeBSD.org
Fri Mar 3 00:45:11 GMT 2006


On Thursday,  2 March 2006 at 11:06:31 +0200, Dmitry Pryanishnikov wrote:
>
>   I apologize for asking here my question (it should belong to -questions,
> but the most developers are available here, and I just hope it won't hurt).
> What's the historical reason of the following style(9) advise:
>
> 	Values in return statements should be enclosed in parentheses.

The background is "that's the way our grandfathers did it".  I asked
dmr about this some years ago.  Here his reply:

On Friday, 11 February 2000 at  1:36:53 -0500, Dennis Ritchie wrote:
>> I've been wondering about the tradition of writing 'return
>> (expression)' instead of 'return expression' in C code.  The earliest
>> documentation I have (K&R I) suggests I use the former (page 68),
>> without specifying why, while appendix A (page 218) specifies 'return
>> expression'.
>>
>> I got hold of last1120

This is the last version of the C compiler written for the PDP 11/20,
some time in 1972.

>> and compiled and ran it against a test program, and it seems that
>> this version won't accept the syntax 'return expression': the
>> parentheses are mandatory.  Would it be fair to consider the usage
>> 'return (expression)' as an archaism?
>
> An archaism: just so.  The language and compiler ca. 1973
> did want the parens.  By the 5th edition (1975) I had realized that
> they weren't needed and the syntax was just 'return expression'.
>
> On the other hand, no one seemed to want to make use of the
> new freedom.  I glanced at v7 source (1977) and couldn't
> find any instances of non-parenthesized return values--
> I might have missed an instance, but there couldn't have
> been more than a very few. Evidently it had become wired
> into the mental syntax.
>
> This was certainly true for Brian in K&R 1 and evidently
> for me as well, since the very few examples in the appendix
> use the ().  But the grammar does indeed reflect the
> fact that they weren't required.
>
> 	Dennis

On Thursday,  2 March 2006 at 10:06:08 -0700, M. Warner Losh wrote:
>
> No.  Debates over style(9) aren't productive.

Agreed.

On Thursday,  2 March 2006 at 10:09:22 -0700, M. Warner Losh wrote:
> In message: <4407226D.3050901 at freebsd.org>
>             Colin Percival <cperciva at freebsd.org> writes:
>> Robert Watson wrote:
>>> I can't really think of a good reason
>>> for return (foo) over return foo
>>
>> I'm not sure if this qualifies as a *good* reason, but
>> writing "return (foo)" is more consistent with other
>> keyword usage: "if (foo)", "for (foo)", "while (foo)",
>> "switch (foo)".
>
> Before 'void' existed, many people recommended () so that you could
> make 'return' a macro for extra debugging.  With void functions that
> have naked returns make this impossible these days...

This is actually a useful property.  Pity it's gone.

On Thursday,  2 March 2006 at 17:12:09 +0000, Robert Watson wrote:
> On Thu, 2 Mar 2006, Colin Percival wrote:
>
>> Robert Watson wrote:
>>> I can't really think of a good reason
>>> for return (foo) over return foo
>>
>> I'm not sure if this qualifies as a *good* reason, but writing "return
>> (foo)" is more consistent with other keyword usage: "if (foo)", "for
>> (foo)", "while (foo)", "switch (foo)".
>
> I also find myself doing search and replace on return values more on
> FreeBSD source than I do on other source.  I.e.:  s/return (0)/return
> (NULL)/ and that sort of thing.  I think I would be less comfortable doing
> that without the parens.

Why?  This sounds like the MySQL argument for writing:

  foo= bar;

instead of

  foo = bar;

Greg
--
See complete headers for address and phone numbers.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 187 bytes
Desc: not available
Url : http://lists.freebsd.org/pipermail/freebsd-current/attachments/20060303/974deff2/attachment-0001.bin


More information about the freebsd-current mailing list