Article View: comp.lang.c.moderated
Article #2144Re: Bad Style Contest - We Have a Winner!
From: Network News
Date: Fri, 01 Mar 1996 16:06
Date: Fri, 01 Mar 1996 16:06
69 lines
2396 bytes
2396 bytes
David Thomas Richard Given <dtrg@st-andrews.ac.uk> wrote:
>Definately. I use lots of white space:
<pet peeve>
Defin*i*tely.
</pet peeve>
>void foo(int size)
>{
> int variable1;
> char *variable2;
>
> /* Comment here, usually */
>
> variable2 = (char *)malloc(size);
> if (variable2 == NULL)
> barf();
>
> /* Comment here, as well */
>
> do_something_with(variable2);
>
> /* Free storage (I should have used alloca()) */
>
> free(variable2);
>}
>
>This could be compressed into:
>
>void foo(int size)
>{
> int variable1;
> char *variable2;
> if (!variable2 = (char *)malloc(size))
> barf();
> do_something_with(variable2);
> free(variable2);
>}
>
>...but is a lot harder to follow. Okay, it's a contrived example, but you
>get my point.
(Sorry about the excessive quoting. I think it's necessary in this
case.)
For a contrived example, I find it rather fails to make your point. I
find the second version of the function far more understandable. The
code is all visually close together, which helps a lot with larger
sections of code. Blank lines waste vertical space. Looking at the
code, it is immediately obvious that it allocates memory, with error
checking, then does_something_with the memory, and then frees the
memory. Comments and blank lines do not help, and in your first
version of the code distract one's attention from what really matters
-- the code.
BTW, there's a bug in the second version.
if (!variable2 = (char *)malloc(size))
should be
if (!(variable2 = (char *)malloc(size)))
I'm not sure whether I would have noticed this error as quickly in the
first version of the code (it isn't there), but I suspect not.
-zefram
--
Andrew Main <zefram@dcs.warwick.ac.uk>
Technical Details
Message-ID:
<4h779j$5ep@solutions.solon.com>Path:
arc1-ca.pugleaf.net!solon.com!not-for-mailReferences:
<4fjmdt$og4@solutions.solon.com> <4g27nn$qaj@solutions.solon.com> <4h2thl$1io@solutions.solon.com>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail From: Network News <news@dcs.warwick.ac.uk> Newsgroups: comp.lang.c.moderated Subject: Re: Bad Style Contest - We Have a Winner! Date: 1 Mar 1996 10:06:11 -0600 Organization: Department of Computer Science, Warwick University, England Sender: clc@solutions.solon.com Approved: clc@solutions.solon.com Message-ID: <4h779j$5ep@solutions.solon.com> References: <4fjmdt$og4@solutions.solon.com> <4g27nn$qaj@solutions.solon.com> <4h2thl$1io@solutions.solon.com> NNTP-Posting-Host: solutions.solon.com X-Nntp-Posting-Host: stone X-Stardate: [-31]7133.27 X-Us-Congress: Moronic fuckers