GNU Coding Standards
Encyclopedia
The GNU Coding Standards are a set of rules and guidelines for writing program
s that work consistently within the GNU
system. The GNU Coding Standards were written by Richard Stallman
and other GNU Project volunteers. The standards document is part of the GNU Project
and is available from the GNU website http://www.gnu.org/prep/standards/. Though it focuses on writing free software
for GNU in C
, much of it can be applied more generally. In particular, the GNU Project encourages its contributors to always try to follow the standards—whether or not their programs are implemented in C. The C code formatting style is well-known within the free software community
, but of course, anyone can choose to follow it.
constructs. Here is a characteristic example:
The consistent treatment of blocks as statements (for the purpose of indentation) is a very distinctive feature of the GNU C code formatting style; as is the mandatory space before parentheses. All code formatted in the GNU style has the property that each closing brace, bracket or parenthesis appears to the right of its corresponding opening delimiter, or in the same column.
As a general principle, GNU Emacs can be considered a reliable authority on the GNU code formatting style. As such, it is desirable that any piece of code that looks ugly when indented by Emacs is changed into a more Emacs-friendly form—for example, by inserting additional parentheses.
Comments should consist of complete, capitalized sentences, each followed by two spaces (so that Emacs can tell where one sentence ends and the next begins).
For long or complex preprocessor conditionals, every
read-only. Therefore, files that are modified for internal purposes (log files, lock files, temporary files, etc.) should not be stored in either
world means 'between Unixes'; in a GNU program this kind of portability is desirable, but not vitally important.
According to the standard, portability problems are very limited as GNU programs are designed to be compiled with one compiler, the GNU C Compiler
, and only run on one system, which is the GNU system.
There is one form of portability problem though, and that is the fact that the standard makes it clear that a program should run on different CPU
types. The standard says that GNU doesn't and won't support 16-bit systems, but handling all the different 32- and 64-bit systems is absolutely necessary.
Computer program
A computer program is a sequence of instructions written to perform a specified task with a computer. A computer requires programs to function, typically executing the program's instructions in a central processor. The program has an executable form that the computer can use directly to execute...
s that work consistently within the GNU
GNU
GNU is a Unix-like computer operating system developed by the GNU project, ultimately aiming to be a "complete Unix-compatible software system"...
system. The GNU Coding Standards were written by Richard Stallman
Richard Stallman
Richard Matthew Stallman , often shortened to rms,"'Richard Stallman' is just my mundane name; you can call me 'rms'"|last= Stallman|first= Richard|date= N.D.|work=Richard Stallman's homepage...
and other GNU Project volunteers. The standards document is part of the GNU Project
GNU Project
The GNU Project is a free software, mass collaboration project, announced on September 27, 1983, by Richard Stallman at MIT. It initiated GNU operating system development in January, 1984...
and is available from the GNU website http://www.gnu.org/prep/standards/. Though it focuses on writing free software
Free software
Free software, software libre or libre software is software that can be used, studied, and modified without restriction, and which can be copied and redistributed in modified or unmodified form either without restriction, or with restrictions that only ensure that further recipients can also do...
for GNU in C
C (programming language)
C is a general-purpose computer programming language developed between 1969 and 1973 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system....
, much of it can be applied more generally. In particular, the GNU Project encourages its contributors to always try to follow the standards—whether or not their programs are implemented in C. The C code formatting style is well-known within the free software community
Free software community
The free-software community is an informal term that refers to the users and developers of free software as well as supporters of the free-software movement. The movement is sometimes referred to as the open-source software community or a subset thereof...
, but of course, anyone can choose to follow it.
Code formatting
The GNU Coding Standards specify exactly how to format most C programming languageC (programming language)
C is a general-purpose computer programming language developed between 1969 and 1973 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system....
constructs. Here is a characteristic example:
The consistent treatment of blocks as statements (for the purpose of indentation) is a very distinctive feature of the GNU C code formatting style; as is the mandatory space before parentheses. All code formatted in the GNU style has the property that each closing brace, bracket or parenthesis appears to the right of its corresponding opening delimiter, or in the same column.
As a general principle, GNU Emacs can be considered a reliable authority on the GNU code formatting style. As such, it is desirable that any piece of code that looks ugly when indented by Emacs is changed into a more Emacs-friendly form—for example, by inserting additional parentheses.
Comments
The standards greatly emphasise the importance of English-language comments:Please write the comments in a GNU program in English, because English is the one language that nearly all programmers in all countries can read. If you do not write English well, please write comments in English as well as you can, then ask other people to help rewrite them. If you can't write comments in English, please find someone to work with you and translate your comments into English.
Comments should consist of complete, capitalized sentences, each followed by two spaces (so that Emacs can tell where one sentence ends and the next begins).
For long or complex preprocessor conditionals, every
#else
and #endif
should have a comment explaining the condition for the code below (for #else
) or above (for #endif
).Files
The standards require that all programs be able to operate when/usr
and /etc
are mountedMount (computing)
Mounting takes place before a computer can use any kind of storage device . The user or their operating system must make it accessible through the computer's file system. A user can access only files on mounted media.- Mount point :A mount point is a physical location in the partition used as a...
read-only. Therefore, files that are modified for internal purposes (log files, lock files, temporary files, etc.) should not be stored in either
/usr
or /etc
. An exception is made for programs whose job it is to update system configuration files in /etc
. Another exception is made for storing files in a directory when the user has explicitly asked to modify a file in the same directory.Portability
The GNU Coding Standards define the issue of portability in this way: portability in the UnixUnix
Unix is a multitasking, multi-user computer operating system originally developed in 1969 by a group of AT&T employees at Bell Labs, including Ken Thompson, Dennis Ritchie, Brian Kernighan, Douglas McIlroy, and Joe Ossanna...
world means 'between Unixes'; in a GNU program this kind of portability is desirable, but not vitally important.
According to the standard, portability problems are very limited as GNU programs are designed to be compiled with one compiler, the GNU C Compiler
GNU Compiler Collection
The GNU Compiler Collection is a compiler system produced by the GNU Project supporting various programming languages. GCC is a key component of the GNU toolchain...
, and only run on one system, which is the GNU system.
There is one form of portability problem though, and that is the fact that the standard makes it clear that a program should run on different CPU
Central processing unit
The central processing unit is the portion of a computer system that carries out the instructions of a computer program, to perform the basic arithmetical, logical, and input/output operations of the system. The CPU plays a role somewhat analogous to the brain in the computer. The term has been in...
types. The standard says that GNU doesn't and won't support 16-bit systems, but handling all the different 32- and 64-bit systems is absolutely necessary.
External links
- The GNU Coding Standards on the GNU website
- Eclipse Code Style Formatter for GNU Coding Standards