🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.misc
Article #2647

Re: Complexity of syntax

#2647
From: Karl Heuer
Date: Thu, 10 Jan 1991 19:26
57 lines
3269 bytes
In article <1991Jan8.165344.13870@maths.nott.ac.uk> anw@maths.nott.ac.uk (Dr A. N. Walker) writes:
>In article <11883:Jan502:21:0191@kramden.acf.nyu.edu> brnstnd@kramden.acf.nyu.edu (Dan Bernstein) writes:
>>Every coding stylebook I've seen recommends that you give names to
>>everything. It's a very good idea, you know.

I'm afraid I can't agree with that statement in its full generality.

>Really?  Do you write [abridged --kwzh]
>	#define TWENTYFOUR	24
>	#define SIXTY	60
>	secs = (((days*TWENTYFOUR + hours)*SIXTY + minutes)*SIXTY + seconds;
>in preference to
>	secs = (((days * 24 + hours) * 60 + minutes) * 60 + seconds;
>?

On the other hand, I can't agree with this part of the counterexample either.
The first version is silly, but there does exist a non-silly version&:
	#define HOUR_DAY  24
	#define MIN_HOUR  60
	#define SEC_MIN   60
	secs = (((days*HOUR_DAY+hours)*MIN_HOUR+minutes)*SEC_MIN+seconds;
Note that the constant "60" deserves *two* names, since it has two independent
uses.  It's just a historical coincidence that they have the same value.

>Somewhere between that and
>	#define BUFSIZE 4096
>	char buffer[BUFSIZE];
>which I hope everyone prefers to
>	char buffer[4096]

No, I would not (necessarily).  BUFSIZE is already so horribly overloaded as
to be virtually meaningless.  ("Why does linbuf[] have BUFSIZE elements?"
"Oh, all the arrays do.  It's just some number that we hope is big enough to
prevent overflow."  "Is there any reason why linbuf[] and genbuf[] have to
have the same size?"  "No.")  If it is specific to this one buffer, then there
is little reason to prefer
	#define BUFSIZE 4096
	char buffer[BUFSIZE];
	read(fd, buffer, BUFSIZE);
to
	char buffer[4096];
	read(fd, buffer, sizeof(buffer));
which avoids the need to invent a separate name for the size.$

A good first approximation is to name something iff it gets used more than
once.  Anonymous functions would be useful for once-only instances such as
handing them off to qsort().
	qsort((void *)sa, n, sizeof(char *), (int (*)(void const *, void \
	const *)){ return strcmp(*(char const **)%0, *(char const **)%1); });

Karl W. Z. Heuer (karl@ima.isc.com or uunet!ima!karl), The Walking Lint
________
& This is not meant to imply that I always use the named form.  Since these
  particular constants are extremely unlikely to change and will probably be
  understood in context even without being named, I would not object to the
  unnamed version of the code fragment.
$ I'm assuming here that all the references are within the same file.
Technical Details
Message-ID: <1991Jan10.192646.6902@dirtydog.ima.isc.com>
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!bonnie.concordia.ca!thunder.mcrcim.mcgill.edu!snorkelwacker.mit.edu!paperboy!hsdndev!spdcc!ima!dirtydog!karl
References: <1990Dec29.101233.1894@mathrt0.math.chalmers.se> <18747:Jan220:02:1091@kramden.acf.nyu.edu> <1991Jan4.152846.15917@maths.nott.ac.uk> <11883:Jan502:21:0191@kramden.acf.nyu.edu> <1991Jan8.165344.13870@maths.nott.ac.uk>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!bonnie.concordia.ca!thunder.mcrcim.mcgill.edu!snorkelwacker.mit.edu!paperboy!hsdndev!spdcc!ima!dirtydog!karl
From: karl@ima.isc.com (Karl Heuer)
Newsgroups: comp.lang.misc
Subject: Re: Complexity of syntax
Message-ID: <1991Jan10.192646.6902@dirtydog.ima.isc.com>
Date: 10 Jan 91 19:26:46 GMT
References: <1990Dec29.101233.1894@mathrt0.math.chalmers.se> <18747:Jan220:02:1091@kramden.acf.nyu.edu> <1991Jan4.152846.15917@maths.nott.ac.uk> <11883:Jan502:21:0191@kramden.acf.nyu.edu> <1991Jan8.165344.13870@maths.nott.ac.uk>
Sender: news@dirtydog.ima.isc.com (NEWS ADMIN)
Organization: Interactive Systems
Lines: 57