GNU coding standards

This article includes a list of references, but its sources remain unclear because it has insufficient inline citations. Please help to improve this article by introducing more precise citations. (September 2009) (Learn how and when to remove this template message)

|

For the quantum error correcting code, see gnu code.

The GNU coding standards are a set of rules and guidelines for writing programs 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. 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.

## Contents

* 1 Code formatting
* 1.1 Splitting long lines
* 2 Comments
* 3 Files
* 4 Portability
* 5 Gnits standards
* 6 Criticism
* 7 See also
* 8 References
* 9 External links

## Code formatting

The GNU Coding Standards specify exactly how to format most C programming language constructs. Here is a characteristic example:

int
main (int argc, char *argv[])

struct gizmo foo;

fetch_gizmo (&foo, argv[1]);

check:
if (foo.type == MOOMIN)
puts ("It's a moomin.");
else if (foo.bar < GIZMO_SNUFKIN_THRESHOLD / 2
|| (strcmp (foo.class_name, "snufkin") == 0)
&& foo.bar < GIZMO_SNUFKIN_THRESHOLD)
puts ("It's a snufkin.");
else

char *barney;  /* Pointer to the first character after
the last slash in the file name.  */
int wilma;     /* Approximate size of the universe.  */
int fred;      /* Max value of the `bar' field.  */

frobnicate (&foo, GIZMO_SNUFKIN_THRESHOLD,
&barney, &wilma, &fred);
twiddle (&foo, barney, wilma + fred);

while (foo.bar >= GIZMO_SNUFKIN_THRESHOLD);

store_size (wilma);

goto check;

return 0;

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.

Being tightly integrated with the GNU system, GNU Emacs provides automatic formatting of C code to match the GNU coding standards.[1] Rather than manually modifying code formatting in a way that strays from the GNU coding standards, the formatted layout of the code can be tweaked by writing it in a more Emacs-friendly form—for example, by inserting additional parentheses.

### Splitting long lines

"When you split an expression into multiple lines, split it before an operator, not after one."[2]

For example:

if (foo_this_is_long && bar > win (x, y, z)
&& remaining_condition)

## 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 mounted 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 Unix 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.

## Gnits standards

The Gnits standards are a collection of standards and recommendations for programming, maintaining, and distributing software. They are published by a group of GNU project maintainers who call themselves "Gnits", which is short for "GNU nit-pickers". As such, they represent advice, not Free Software Foundation or GNU policy, but parts of the Gnits' standards have seen widespread adoption among free software programmers in general.

The Gnits standards are extensions to, refinements of, and annotations for the GNU Standards. However, they are in no way normative in GNU; GNU maintainers are not required to follow them. Nevertheless, maintainers and programmers often find in Gnits standards good ideas on the way to follow GNU Standards themselves, as well as tentative, non-official explanations about why some GNU standards were decided the way they are. There are very few discrepancies between Gnits and GNU standards, and they are always well noted as such.

The standards address aspects of software architecture, program behaviour, human–computer interaction, C programming, documentation, and software releases.

As of 2008, the Gnits standards carry a notice that they are moribund and no longer actively maintained, and points readers to the manuals of Gnulib, Autoconf, and Automake, which are said to cover many of the same topics.

## Criticism

The GNU coding standards are primarily used by GNU projects, though its use is not limited to GNU projects alone.

The Linux kernel strongly discourages this style for kernel code, and refers to the style pejoratively: "First off, I’d suggest printing out a copy of the GNU coding standards, and NOT read it. Burn them, it’s a great symbolic gesture.".[3] Steve McConnell, in his book Code Complete, also advises against using this style; he marks a code sample which uses it with a "Coding Horror" icon, symbolizing especially dangerous code, and states that it impedes readability by requiring an extra level of indentation for braces.[4]

## See also

* Gnits standards

## References

1. ↑ "CC Mode Manual". https://www.gnu.org/software/emacs/manual/html_mono/ccmode.html#index-GNU-style.
2. ↑ "GNU Coding Standards" (in en). https://www.gnu.org/prep/standards/standards.html#Formatting.
3. ↑ "Linux kernel coding style — The Linux Kernel documentation" (in en). https://www.kernel.org/doc/html/v4.10/process/coding-style.html.
4. ↑ McConnell, Steve (2004). Code Complete: A practical handbook of software construction. Redmond, WA: Microsoft Press. pp. 746–747. ISBN 0-7356-1967-0. https://archive.org/details/codecomplete0000mcco/page/746.

## External links

* The GNU Coding Standards on the GNU website
* Eclipse Code Style Formatter for GNU Coding Standards

0.00

(0 votes)

Original source: https://en.wikipedia.org/wiki/GNU coding standards. Read more

|

Retrieved from "https://handwiki.org/wiki/index.php?title=GNU_coding_standards&oldid=3985993"