tech-userlevel archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: Rationale for some rules in style guide



> The style guide says:
>> Avoid initializing variables in the declarations
> Why?

Well, I didn't write the style guide, so I'm not authoritative on
questions of why something is there.

And, it's a _guide_, not _mandates_.  Every style rule I've ever seen,
except really vague ones like "write readable code", I think is better
broken at least occasionally.  I've worked on projects where style
guides have been elevated to the status of mandates; I've always
thought it a mistake.

Those said....

> In functions that consist of a few small paragraphs, I like
> paragraphs of this form, which are compact yet readable:

> 	command cmd = xmalloc(sizeof(cmd));
> 	cmd->name = "name";
> 	cmd->args = build_arguments();
> 	cmd->env = build_environment();

There is a bug in that code.  Given how you are using cmd, command must
be a pointer-to-struct type (or pointer-to-union, but then the code,
while perhaps valid C, is semantic nonsense), but you a allocating
enough space for the pointer, not enough space for the struct.

Readability is subjective.  There is, strictly, no such thing as
"readable code" or "unreadable code", code is readable or unreadable
only with respect to particular (putative) readers.  (While admittedly
there is often at least rough consensus about (un)readbility in extreme
cases, this is not the IOCCC.)

Initializing variables in their declarations tends to textually mix
executable code and variable declarations.  I find that impairs
readability.

As for your snippet, I find its readability impaired in multiple ways:

(1) The type name "command" is not visually distinctive.  I would
uppercase that.  (This admittedly is not relevant to the particular
point you started out talking about.)

(2) command is apparently a pointer type, but there is no * in the
declaration.  (This too is independent of your initial point.)

(3) There is no visual separation between variable declarations and
code.  I would add a blank line between lines 1 and 2 of your example.

(4) Given the initialization of cmd in its declaration, there cannot be
visual separation between its declaration and the executable code of
its initializer...not unless you insert a blank line into that
declaration, which (a) would be even more visually confusing and (b)
can work for at most one variable per block.

/~\ The ASCII				  Mouse
\ / Ribbon Campaign
 X  Against HTML		mouse%rodents-montreal.org@localhost
/ \ Email!	     7D C8 61 52 5D E7 2D 39  4E F1 31 3E E8 B3 27 4B


Home | Main Index | Thread Index | Old Index