Individual projects will no doubt have their own naming conventions. There are some general rules however.
getchar
and
putchar
)
are in lower case
since they may also exist as functions.
Lower-case macro names are only acceptable if the macros behave
like a function call,
that is, they evaluate their parameters exactly once and
do not assign values to named parameters.
Sometimes it is impossible to write a macro that behaves like a
function even though the arguments are evaluated exactly once.
In general, global names (including
enum
s)
should have a
common prefix identifying the module that they belong with.
Globals may alternatively be grouped in a global structure.
Typedeffed names often have
``_t
''
appended to their name.
Avoid names that might conflict with various standard library names. Some systems will include more library code than you want. Also, your program may be extended someday.
Also note the following (from [15]):
``Length is not a virtue in a name; clarity of expression is. A global variable rarely used may deserve a long name,
maxphysaddr
say. An array index used on every line of a loop needn't be named any more elaborately thani
. Sayingindex
orelementnumber
is more to type (or calls upon your text editor) and obscures the details of the computation. When the variable names are huge, it's harder to see what's going on. This is partly a typographic issue; considervs.for(i=0 to 100) array[i]=0The problem gets worse fast with real examples. Indices are just notation, so treat them as such.''for(elementnumber=0 to 100) array[elementnumber]=0;``Pointers also require sensible notation.
np
is just as mnemonic asnodepointer
if you consistently use a naming convention from whichnp
means ``node pointer'' is easily derived.''As in all other aspects of readable programming, consistency is important in naming. If you call one variable
maxphysaddr
, don't call its cousinlowestaddress
.''``Finally, I prefer minimum-length but maximum-information names, and then let the context fill in the rest. Globals, for instance, typically have little context when they are used, so their names need to be relatively evocative. Thus I say
maxphysaddr
(notMaximumPhysicalAddress
) for a global variable, butnp
notNodePointer
for a pointer locally defined and used. This is largely a matter of taste, but taste is relevant to clarity.I eschew embedded capital letters in names; to my prose-oriented eyes, they are too awkward to read comfortably. They jangle like bad typography.'' ``Procedure names should reflect what they do; function names should reflect what they return. Functions are used in expressions, often in things like
if
's, so they need to read appropriately.is unhelpful because we can't deduce whether checksize returns true on error or non-error; insteadif(checksize(x))makes the point clear and makes a future mistake in using the routine less likely.''if(validsize(x))