home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Fresh Fish 8
/
FreshFishVol8-CD1.bin
/
gnu
/
info
/
standards.info-2
(
.txt
)
< prev
next >
Wrap
GNU Info File
|
1994-12-22
|
44KB
|
1,198 lines
This is Info file ../standards.info, produced by Makeinfo-1.55 from the
input file ../standards.texi.
START-INFO-DIR-ENTRY
* Standards: (standards). GNU coding standards.
END-INFO-DIR-ENTRY
GNU Coding Standards Copyright (C) 1992, 1993, 1994 Free Software
Foundation, Inc.
Permission is granted to make and distribute verbatim copies of this
manual provided the copyright notice and this permission notice are
preserved on all copies.
Permission is granted to copy and distribute modified versions of
this manual under the conditions for verbatim copying, provided that
the entire resulting derived work is distributed under the terms of a
permission notice identical to this one.
Permission is granted to copy and distribute translations of this
manual into another language, under the above conditions for modified
versions, except that this permission notice may be stated in a
translation approved by the Free Software Foundation.
File: standards.info, Node: Syntactic Conventions, Next: Names, Prev: Comments, Up: Top
Clean Use of C Constructs
*************************
Please explicitly declare all arguments to functions. Don't omit
them just because they are `int's.
Declarations of external functions and functions to appear later in
the source file should all go in one place near the beginning of the
file (somewhere before the first function definition in the file), or
else should go in a header file. Don't put `extern' declarations inside
functions.
It used to be common practice to use the same local variables (with
names like `tem') over and over for different values within one
function. Instead of doing this, it is better declare a separate local
variable for each distinct purpose, and give it a name which is
meaningful. This not only makes programs easier to understand, it also
facilitates optimization by good compilers. You can also move the
declaration of each local variable into the smallest scope that includes
all its uses. This makes the program even cleaner.
Don't use local variables or parameters that shadow global
identifiers.
Don't declare multiple variables in one declaration that spans lines.
Start a new declaration on each line, instead. For example, instead of
this:
int foo,
bar;
write either this:
int foo, bar;
or this:
int foo;
int bar;
(If they are global variables, each should have a comment preceding it
anyway.)
When you have an `if'-`else' statement nested in another `if'
statement, always put braces around the `if'-`else'. Thus, never write
like this:
if (foo)
if (bar)
win ();
else
lose ();
always like this:
if (foo)
{
if (bar)
win ();
else
lose ();
}
If you have an `if' statement nested inside of an `else' statement,
either write `else if' on one line, like this,
if (foo)
...
else if (bar)
...
with its `then'-part indented like the preceding `then'-part, or write
the nested `if' within braces like this:
if (foo)
...
else
{
if (bar)
...
}
Don't declare both a structure tag and variables or typedefs in the
same declaration. Instead, declare the structure tag separately and
then use it to declare the variables or typedefs.
Try to avoid assignments inside `if'-conditions. For example, don't
write this:
if ((foo = (char *) malloc (sizeof *foo)) == 0)
fatal ("virtual memory exhausted");
instead, write this:
foo = (char *) malloc (sizeof *foo);
if (foo == 0)
fatal ("virtual memory exhausted");
Don't make the program ugly to placate `lint'. Please don't insert
any casts to `void'. Zero without a cast is perfectly fine as a null
pointer constant.
File: standards.info, Node: Names, Next: Using Extensions, Prev: Syntactic Conventions, Up: Top
Naming Variables and Functions
******************************
Please use underscores to separate words in a name, so that the Emacs
word commands can be useful within them. Stick to lower case; reserve
upper case for macros and `enum' constants, and for name-prefixes that
follow a uniform convention.
For example, you should use names like `ignore_space_change_flag';
don't use names like `iCantReadThis'.
Variables that indicate whether command-line options have been
specified should be named after the meaning of the option, not after
the option-letter. A comment should state both the exact meaning of
the option and its letter. For example,
/* Ignore changes in horizontal whitespace (-b). */
int ignore_space_change_flag;
When you want to define names with constant integer values, use
`enum' rather than `#define'. GDB knows about enumeration constants.
Use file names of 14 characters or less, to avoid creating gratuitous
problems on System V. You can use the program `doschk' to test for
this. `doschk' also tests for potential name conflicts if the files
were loaded onto an MS-DOS file system--something you may or may not
care about.
File: standards.info, Node: Using Extensions, Next: System Functions, Prev: Names, Up: Top
Using Non-standard Features
***************************
Many GNU facilities that already exist support a number of convenient
extensions over the comparable Unix facilities. Whether to use these
extensions in implementing your program is a difficult question.
On the one hand, using the extensions can make a cleaner program.
On the other hand, people will not be able to build the program unless
the other GNU tools are available. This might cause the program to
work on fewer kinds of machines.
With some extensions, it might be easy to provide both alternatives.
For example, you can define functions with a "keyword" `INLINE' and
define that as a macro to expand into either `inline' or nothing,
depending on the compiler.
In general, perhaps it is best not to use the extensions if you can
straightforwardly do without them, but to use the extensions if they
are a big improvement.
An exception to this rule are the large, established programs (such
as Emacs) which run on a great variety of systems. Such programs would
be broken by use of GNU extensions.
Another exception is for programs that are used as part of
compilation: anything that must be compiled with other compilers in
order to bootstrap the GNU compilation facilities. If these require
the GNU compiler, then no one can compile them without having them
installed already. That would be no good.
Since most computer systems do not yet implement ANSI C, using the
ANSI C features is effectively using a GNU extension, so the same
considerations apply. (Except for ANSI features that we discourage,
such as trigraphs--don't ever use them.)
File: standards.info, Node: System Functions, Next: Semantics, Prev: Using Extensions, Up: Top
Calling System Functions
************************
C implementations differ substantially. ANSI C reduces but does not
eliminate the incompatibilities; meanwhile, many users wish to compile
GNU software with pre-ANSI compilers. This chapter gives
recommendations for how to use the more or less standard C library
functions to avoid unnecessary loss of portability.
* Don't use the value of `sprintf'. It returns the number of
characters written on some systems, but not on all systems.
* Don't declare system functions explicitly.
Almost any declaration for a system function is wrong on some
system. To minimize conflicts, leave it to the system header
files to declare system functions. If the headers don't declare a
function, let it remain undeclared.
While it may seem unclean to use a function without declaring it,
in practice this works fine for most system library functions on
the systems where this really happens. The problem is only
theoretical. By contrast, actual declarations have frequently
caused actual conflicts.
* If you must declare a system function, don't specify the argument
types. Use an old-style declaration, not an ANSI prototype. The
more you specify abo