[ << ] [ >> ]           [Top] [Contents] [Index] [ ? ]

4. Names

name length
Names should be unique in their first 31 characters. Don't bother with 6-character uniqueness as required for strict C89 compliance.

Macros without parameters and function-like macros that evaluate their arguments multiple times or that have unsafe side effects should have names in all capitals (including the prefix). Well-behaved function-like macros should have their names in lowercase.

Macro parameters should have all-uppercase names.

Prefer singular nouns to plural in names. In particular, arrays should have singular names.

Qualifiers such as ttl, sum, avg, min, max, ... go at the end of names following an underscore. (See the table of abbreviations below.)

Do not use num as a qualifier, since it can mean "number of the current item" or "number of items". Use cur or idx for the former meaning, cnt for the latter.

keywords in other languages
When writing a parser, compiler, etc., for a language, write that language's reserved words in all capitals in names; e.g., parse_DATA_LIST() as a function to parse a PSPP DATA LIST construct. Do not use such a capitalized keyword as the only component of a name.

status variables
Name status variables and Boolean variables for their meaning: done, error, found, success, .... There is an implied, unwritten "is": (is_)done, etc.

negative names
Avoid negative names such as not_found.

numbers in names
Names that contain digits should usually be restricted to ranges. Use 0 and 1 for range endpoints, not 1 and 2: x0 and x1.

size versus length
A size is a count of bytes, a length is a count of characters. A buffer has size, but a string has length. The length of a string does not include the null terminator, but the size of the buffer that contains the string does.

reserved names
Avoid names reserved by ANSI C89 and C99 and by POSIX.

A _p suffix indicates a boolean test for the condition described by the rest of the name. (This comes from Lisp.)

Consistently use the following standard abbreviations for English words in C names:
amount amt
average avg
buffer buf
calculate calc
command cmd
compare cmp
count cnt
current cur
decrement dec
destination dst
enumeration enum
expression expr
function func
increment inc
index idx
length len
lexical lex
maximum max
minimum min
number -- Don't use. Substitute cur or idx for an index, cnt for a count.
pointer ptr Don't use in names of actual C pointers, only in the names of array indexes, etc., that act as pointers.
previous prev
procedure proc
quantity qty
record -- Don't use.
source src
statement stmt
structure -- Don't use.
table tbl
target -- Use "destination" (dst) instead.
temporary tmp
variable var
Otherwise, try to avoid abbreviations.

[ << ] [ >> ]           [Top] [Contents] [Index] [ ? ]

This document was generated by Ben Pfaff on September, 11 2001 using texi2html