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



  | On Tue, Apr 11, 2023 at 08:30:19PM +0200, Roland Illig wrote:
  | The style guide says:
  |    When declaring variables in functions declare them sorted by size

That one ought be deleted, if only because absolutely no-one follows it
(properly).   To do that, one would need to fill the declarations with
#if noise - so that the vars can be placed in proper size order, when
we don't know for sure what that is until compile time (and it can vary
depending upon machine arch) - and for arrays, perhaps depending upon
a size passed in via a header file, or even on the cc command line.

Absolutely no-one does that - that "rule" should be discarded if only
for that reason.

Personally, I like to put related variables (by purpose, not spelling
of the name, which may change several times during development, and perhaps
even later, and not the size the compiler might eventually allocate) so
that when one looks to find the declaration of one variable, others that
are related to it are nearby (despite perhaps being different types, of
wildly different sizes) and others, which are unrelated, are further away
even if they have the same type.    But that's just me...

I also like to know where to look to find the var declaration, so unless
there's a verv very good reason (func scope vars, not globals obviously)
they all go at the head of the function, nowhere else - and never anywhere
other than after a '{' that starts some block or other, regardless of what
newer versions of C might now allow (I see no point generating code to require
a recent compiler, for no better reason than where the var decls are placed).

kre



Home | Main Index | Thread Index | Old Index