🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c.moderated
Article #2107

Re: Bad Style Contest - We Have a Winner!

#2107
From: David Thomas Richard Given
Date: Thu, 29 Feb 1996 00:55
85 lines
3170 bytes
In article <4g27nn$qaj@solutions.solon.com>,
Konrad Schwarz <schwarz@a3.complang.tuwien.ac.at> wrote:
>I don't think this is true in the general case.  Perhaps I can nudge you in my
>direction with an example:
>
>	if (-1 == (fd = some_unix_system_call (with, at + least,
>		 three * parameters)))
>            error_exit (fmt, ...);
>
>I find this more legible than
(snip)

Ouch. I have to say (IMHO) that if I was maintaining this, the first 
thing I'd do is split it into:

	fd = some_unix_system_call(...);
	if (fd == -1)
		error_exit(...);

The reasoning goes: sod the efficiency, readibility is everything. A good 
compiler will produce the same code anyway. Doing it this way makes the 
code more linear, resulting in a more straightforward path of execution, 
and also avoids having a = in a conditional (is this the #1 source of C 
bugs? It certainly rates in the top five).

(snip)
>By writing the check first, I mentally ``clear the decks''
>for the ``hard work'' of finding the correct parameters for the call.
>The second method necessitates having to remember ``don't forget the check''
>in the back of your head when you are looking for the right arguments for
>the call, and then, after doing the ``hard work,'' when your brain would
>like to take a breather, remembering to include the check.

I can see the logic here. But to me, a system call standing by itself 
calls attention to itself, shouting `you've forgotten to error-check me!' 
to all and sundry. :) Sort of.

(attribute lost)
>|> That isn't necessary. The eye/brain parses expression in logical groups,
>|> not simply left to right. That's why layout is important.
>
>I think visual grouping is important too.  See above.

Definately. I use lots of white space:

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.

-- 
-------------------------------------------------------------------------------
   Why do people surf the Information Superhighway? Won't they get run over?
               http://www-hons-cs.cs.st-andrews.ac.uk/~dg
              Sun-Earther David Daton Given  of Lochcarron
Technical Details
Message-ID: <4h2thl$1io@solutions.solon.com>
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail
References: <4fjmdt$og4@solutions.solon.com> <4g27nn$qaj@solutions.solon.com>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail
From: dtrg@st-andrews.ac.uk (David Thomas Richard Given)
Newsgroups: comp.lang.c.moderated
Subject: Re: Bad Style Contest - We Have a Winner!
Date: 28 Feb 1996 18:55:17 -0600
Organization: University Of St. Andrews, Fife, UK.
Sender: clc@solutions.solon.com
Approved: clc@solutions.solon.com
Message-ID: <4h2thl$1io@solutions.solon.com>
References: <4fjmdt$og4@solutions.solon.com> <4g27nn$qaj@solutions.solon.com>
NNTP-Posting-Host: solutions.solon.com