Updated: 2006-04-20
Created: 2004-05-09
Pragmatics is with semantics and syntax one of the central aspects of a program. While syntax is about language and semantics about effects, pragmatics is about quality, that is usefulness.
Under UNIX/Linux there are some established programming conventions that amount to good pragmatics, and are inspired by some important aspects of the UNIX/Linux architecture:
stdio
library with FILE
pointers
and by the shell with file descriptor redirection.cat
) to actualy record a program text, and it is a
good idea to make it easy for that program to process, and in
the case of an editor to reprocess, that text.These aspects are radically different from those that pertain to many other popular operating systems.
There is also a general principle of programming, that program texts should be speak for themselves, as their purpose is to communicate precisely and clearly a program (both to humans and other programs or hardware that reads them).
The ostensible purpose of a program is to achieve an intended effect, but programmers (as well as compilers, CPUs, etc.) cannot write or read programs, only program texts.
The quality of a program is then a consequence of the quality of the program text, as the program text is much more important to the lifetime of a software project.
Many of the conventions listed below apply to any platform, and they will be marked as such.
stderr
.Configuration unavailabledoes not help anywhere as much as
Cannot use configuration '/etc/prog.conf
'
,
which is in turn rather inferior to Cannot open for reading
file '/etc/prog.conf
'
, if that is what
actually was attempted.if
or while
.const
as possible.
(any platform)PATH
environment
variable. Especially if the file is a configuration
file. As a rule the program should have a default
directory list and this should be overridable with an
environment variable./usr/local
,
the root directory and /usr
in this
order. For example configuration files should be
searched for in a directory path like:
.:$HOME/etc:/usr/local/etc:/etc:/usr/etcjust like executables should be searched for in a directory path like:
.:$HOME/bin:/usr/local/bin:/bin:/usr/bin
sort
or perl
, and for easy reading
and writing by humans.sort
.Cannot open file %s., which becomes
Cannot open file .
if the argument
is the empty string.include
s should be listed
in most generic to most specific top to bottom, first thing in a
file.
(any platform)static
.
(any platform)getopt()
library function, and preferably
with the getopt_long()
GNU variant.man
pages.man
pages are the summary of reference
documentation and they are very useful as to avoid putting too
much help material inside the program itself; programs should
not be documentation processors, man
does that.if (length > MIN_WEIGHT)
or #define MAX_VOLUME -10
tend to suggest something is amiss.assert()
to document expected invariants.
(any platform)assert()
can be a debugging aid, but it is
mostly a code reading aid, as it documents what the author
expects at that point.assert()
to document
assumptions about their parameters.fopen
for file open. This respects the principle that names should be in most specific to most generic part order.
sort
, which in particular is extremely important.
Stupid things like /proc/meminfo
are hard to
easily split and process.#if 0
or if (0)
, not with commenting.
(any platform)/etc/fstab
corresponds to the syntax for the
arguments to mount
./proc
in some versions of the
system.