coding.html revision 7e985260692958285fc3c128fdb27867a865db26
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews<H2>C Language</H2>
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews
819fe493f97078521bb6b9a7b97583bef89f5abcMark AndrewsAn ANSI standard C compiler and library are assumed. Feel free to use any
819fe493f97078521bb6b9a7b97583bef89f5abcMark AndrewsANSI C feature.<P>
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews<H2>Warnings</H2>
819fe493f97078521bb6b9a7b97583bef89f5abcMark AndrewsGiven a reasonable set of things to warn about (e.g. -W -Wall for gcc), the
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrewsgoal is to compile with no warnings.
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews<H2>C Source Code</H2>
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews<H3>Copyright</H3>
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews
819fe493f97078521bb6b9a7b97583bef89f5abcMark AndrewsAll source files should have a copyright. The copyright year(s)
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrewsshould be kept current. The files and the copyright year(s) should be
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrewslisted in util/copyrights.<P>
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews<H3>Line Formatting</H3>
26440aaebba1acb5c8810f7faa26ad3b7553762eMark Andrews<H4>Indentation</H4>
33d96fbbc8aa221508f3c780539bf44810fd2c9cMark AndrewsUse tabs. Spaces are only allowed when needed to line up a continued
26440aaebba1acb5c8810f7faa26ad3b7553762eMark Andrewsexpression. In the following example, spaces used for indentation are
33d96fbbc8aa221508f3c780539bf44810fd2c9cMark Andrewsindicated with "_":
33d96fbbc8aa221508f3c780539bf44810fd2c9cMark Andrews<PRE><CODE>
33d96fbbc8aa221508f3c780539bf44810fd2c9cMark Andrews printf("this is going to be %s very long %s statement\n",
33d96fbbc8aa221508f3c780539bf44810fd2c9cMark Andrews _______"a", "printf");
33d96fbbc8aa221508f3c780539bf44810fd2c9cMark Andrews</CODE></PRE>
33d96fbbc8aa221508f3c780539bf44810fd2c9cMark Andrews
33d96fbbc8aa221508f3c780539bf44810fd2c9cMark Andrews<H4>Vertical Whitespace</H4>
33d96fbbc8aa221508f3c780539bf44810fd2c9cMark AndrewsVertical whitespace is also encouraged for improved code legibility by
33d96fbbc8aa221508f3c780539bf44810fd2c9cMark Andrewsgrouping closely related statements and then separating them with a
33d96fbbc8aa221508f3c780539bf44810fd2c9cMark Andrewssingle empty line. There should not, however, be more than one empty
33d96fbbc8aa221508f3c780539bf44810fd2c9cMark Andrewsadjacent line anywhere.
33d96fbbc8aa221508f3c780539bf44810fd2c9cMark Andrews
33d96fbbc8aa221508f3c780539bf44810fd2c9cMark Andrews<H4>Line Length</H4>
33d96fbbc8aa221508f3c780539bf44810fd2c9cMark AndrewsLines should not be longer than 79 characters, even if it requires
33d96fbbc8aa221508f3c780539bf44810fd2c9cMark Andrewsviolating the indentation rules to do so. Since ANSI is assumed, the
33d96fbbc8aa221508f3c780539bf44810fd2c9cMark Andrewsbest way to deal with strings that extend past column 79 is to break
33d96fbbc8aa221508f3c780539bf44810fd2c9cMark Andrewsthem into two or more sections separated from each other by a newline
33d96fbbc8aa221508f3c780539bf44810fd2c9cMark Andrewsand indentation:
33d96fbbc8aa221508f3c780539bf44810fd2c9cMark Andrews
33d96fbbc8aa221508f3c780539bf44810fd2c9cMark Andrews<PRE><CODE>
33d96fbbc8aa221508f3c780539bf44810fd2c9cMark Andrews puts("This string got very far to the "
33d96fbbc8aa221508f3c780539bf44810fd2c9cMark Andrews "left and wrapped. ANSI catenation "
33d96fbbc8aa221508f3c780539bf44810fd2c9cMark Andrews "rules will turn this into one
33d96fbbc8aa221508f3c780539bf44810fd2c9cMark Andrews "long string.");
33d96fbbc8aa221508f3c780539bf44810fd2c9cMark Andrews</CODE></PRE>
33d96fbbc8aa221508f3c780539bf44810fd2c9cMark Andrews
33d96fbbc8aa221508f3c780539bf44810fd2c9cMark Andrews<H3>Comments</H3>
33d96fbbc8aa221508f3c780539bf44810fd2c9cMark AndrewsComments should be used anytime they improve the readability of the code.<P>
33d96fbbc8aa221508f3c780539bf44810fd2c9cMark Andrews
33d96fbbc8aa221508f3c780539bf44810fd2c9cMark AndrewsComments may be single-line or multiline. A single-line comment should be
33d96fbbc8aa221508f3c780539bf44810fd2c9cMark Andrewsat the end of the line of there is other text on the line, and should start
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrewsin the same column as other nearby end-of-line comments. The comment
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrewsshould be at the same indentation level as the text it is referring to.
819fe493f97078521bb6b9a7b97583bef89f5abcMark AndrewsMultiline comments should start with "/*" on a line by itself. Subsequent
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrewslines should have " *" lined-up with the "*" above. The end of the comment
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrewsshould be " */" on a line by itself, again with the "*" lined-up with the
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrewsone above. Comments should start with a capital letter and end with a
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrewsperiod.<P>
819fe493f97078521bb6b9a7b97583bef89f5abcMark AndrewsGood:<P>
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews<PRE><CODE>
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews /*
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews * Private variables.
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews */
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews static int a /* Description of 'a'. */
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews static int b /* Description of 'b'. */
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews static char * c /* Description of 'c'. */
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews</CODE></PRE>
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews
819fe493f97078521bb6b9a7b97583bef89f5abcMark AndrewsThe following lint and lint-like comments should be used where appropriate:
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews<PRE><CODE>
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews /* ARGSUSED */
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews /* FALLTHROUGH */
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews /* NOTREACHED */
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews /* VARARGS */
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews</CODE></PRE>
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews<H3>.h files</H3>
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews.h files should not rely on other files having been included. .h
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrewsfiles should prevent multiple inclusion. The OS is assumed to prevent
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrewsmultiple inclusion of its .h files.<P>
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews.h files that define modules should have a structure like the
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrewsfollowing. Note that <isc/lang.h> should be included by any public
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrewsheader file to get the ISC_LANG_BEGINDECLS and ISC_LANG_ENDDECLS
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrewsmacros used so the correct name-mangling happens for function
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrewsdeclarations when C++ programs include the file. <isc/lang.h> should
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrewsbe included for private header files or for public files that do not
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrewsdeclare any functions.<P>
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews<PRE><CODE>
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews/*
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews * Copyright (C) 1998 Internet Software Consortium.
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews *
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews * Permission to use, copy, modify, and distribute this software for any
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews * purpose with or without fee is hereby granted, provided that the above
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews * copyright notice and this permission notice appear in all copies.
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews *
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews * SOFTWARE.
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews */
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews#ifndef ISC_WHATEVER_H
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews#define ISC_WHATEVER_H 1
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews/*****
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews ***** Module Info
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews *****/
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews/*
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews * (Module name here.)
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews *
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews * (One line description here.)
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews *
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews * (Extended description and notes here.)
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews *
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews * MP:
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews * (Information about multiprocessing considerations here, e.g. locking
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews * requirements.)
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews *
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews * Reliability:
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews * (Any reliability concerns should be mentioned here.)
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews *
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews * Resources:
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews * (A rough guide to how resources are used by this module.)
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews *
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews * Security:
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews * (Any security issues are discussed here.)
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews *
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews * Standards:
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews * (Any standards relevant to the module are listed here.)
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews */
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews/***
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews *** Imports
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews ***/
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews/* #includes here. */
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews#include &lt;isc/lang.h&gt;
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews/***
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews *** Types
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews ***/
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews/* (Type definitions here.) */
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews/***
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews *** Functions
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews ***/
819fe493f97078521bb6b9a7b97583bef89f5abcMark AndrewsISC_LANG_BEGINDECLS
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews/* (Function declarations here, with full prototypes.) */
819fe493f97078521bb6b9a7b97583bef89f5abcMark AndrewsISC_LANG_ENDDECLS
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews#endif /* ISC_WHATEVER_H */
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews</CODE></PRE>
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews<H3>C Source</H3>
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews<H4>Including Interfaces (.h files)</H4>
819fe493f97078521bb6b9a7b97583bef89f5abcMark AndrewsThe first file to be included in a C source file must be config.h.
819fe493f97078521bb6b9a7b97583bef89f5abcMark AndrewsThe config.h file must never be included by any public header file
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews(that is, any header file that will be installed by "make install").
819fe493f97078521bb6b9a7b97583bef89f5abcMark AndrewsTry to include only necessary files, not everything under the sun.<P>
819fe493f97078521bb6b9a7b97583bef89f5abcMark AndrewsOperating-system-specific files should not be included by most modules.<P>
819fe493f97078521bb6b9a7b97583bef89f5abcMark AndrewsInclude UNIX "sys" .h files before ordinary C includes.<P>
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews<H4>Statements</H4>
819fe493f97078521bb6b9a7b97583bef89f5abcMark AndrewsThere should be at most one statement per line. The comma operator
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrewsshould not be used to form compound statements.<P>
819fe493f97078521bb6b9a7b97583bef89f5abcMark AndrewsBad:<P>
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews<PRE><CODE>
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews if (i > 0) {
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews printf("yes\n"); i = 0; j = 0;
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews x = 4, y *= 2;
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews }
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews</CODE></PRE>
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews<H4>Functions</H4>
819fe493f97078521bb6b9a7b97583bef89f5abcMark AndrewsThe use of ANSI C function prototypes is required.<P>
819fe493f97078521bb6b9a7b97583bef89f5abcMark AndrewsThe return type of the function should be listed on a line by itself when
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrewsspecifying the implementation of the function. The opening curly brace should
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrewsoccur on the same line as the argument list, unless the argument list is
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrewsmore than one line long.<P>
819fe493f97078521bb6b9a7b97583bef89f5abcMark AndrewsGood:<P>
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews<PRE><CODE>
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrewsstatic inline void
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrewsf(int i) {
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews /* whatever */
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews}
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrewsint
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrewsg(int i, /* other args here */
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews int last_argument)
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews{
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews return (i * i);
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews}
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews</CODE></PRE>
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews
819fe493f97078521bb6b9a7b97583bef89f5abcMark AndrewsTo suppress compiler warnings, unused function arguments are declared
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrewsusing the <CODE>UNUSED()</CODE> macro.<P>
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews
819fe493f97078521bb6b9a7b97583bef89f5abcMark AndrewsIn the function body, any <CODE>REQUIRE()</CODE>s are placed
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrewsimmediately after the local variabled, followed by any
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews<CODE>UNUSED()</CODE> declarations.<P>
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews<H4>Curly Braces</H4>
819fe493f97078521bb6b9a7b97583bef89f5abcMark AndrewsCurly Braces do not get their own indentation.
819fe493f97078521bb6b9a7b97583bef89f5abcMark AndrewsAn opening brace does not start a new line. The statements enclosed
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrewsby the braces should not be on the same line as the opening or closing
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrewsbrace. A closing brace should be the only thing on the line, unless
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrewsit's part of an else clause.<P>
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews
819fe493f97078521bb6b9a7b97583bef89f5abcMark AndrewsGenerally speaking, when a control statement (<CODE>if, for</CODE> or
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews<CODE>while</CODE>) has only a single action associated with it, then no
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrewsbracing is used around the statement. Exceptions include when the
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrewscompiler would complain about an ambiguous else clause, or when extra
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrewsbracing improves the readability (a judgement call biased toward not
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrewshaving the braces).<P>
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews
819fe493f97078521bb6b9a7b97583bef89f5abcMark AndrewsGood:<P>
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews<PRE><CODE>
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrewsstatic void
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrewsf(int i) {
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews if (i > 0) {
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews printf("yes\n");
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews i = 0;
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews } else
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews printf("no\n");
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews}
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews</CODE></PRE>
819fe493f97078521bb6b9a7b97583bef89f5abcMark AndrewsBad:<P>
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews<PRE><CODE>
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrewsvoid f(int i)
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews {
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews if(i<0){i=0;printf("was negative\n");}
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews if (i > 0)
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews {
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews printf("yes\n");
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews i = 0;
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews }}
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews</CODE></PRE>
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews<H4>Spaces</H4>
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews<UL>
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews<LI>Do put a space between operators like '=', '+', '==', etc.
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews<LI>Do put a space after ','.
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews<LI>Do put a space after ';' in a 'for' statement.
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews<LI>Do put a space after 'return', and also parenthesize the return value.
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews</UL>
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews<UL>
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews<LI>Do not put a space between a variable or function name and '(' or '['.
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews<LI>Do not put a space after the "sizeof" operator name, and also
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrewsparenthesize its argument, as in <CODE>malloc(4 * sizeof(long))</CODE>.
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews<LI>Do not put a space immediately after a '(' or immediately before a ')',
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrewsunless it improves readability. The same goes for '[' and ']'.
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews<LI>Do not put a space before '++' or '--' when used in
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrewspost-increment/decrement mode, or after them when used in
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrewspre-increment/decrement mode.
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews<LI>Do not put a space before ';' when terminating a statement or in a 'for'
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrewsstatement.
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews<LI>Do not put a space after '*' when used to dereference a pointer, or on
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrewseither side of '->'.
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews<LI>Do not put a space after '~'.
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews<LI>The '|' operator may either have a space on both sides or it may have no
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrewsspaces.
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews<LI>Do not put a space after a cast.
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews</UL>
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews<H4>Return Values</H4>
819fe493f97078521bb6b9a7b97583bef89f5abcMark AndrewsIf a function returns a value, it should be cast to (void) if you don't
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrewscare what the value is, except for <CODE>printf</CODE><P>
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews
819fe493f97078521bb6b9a7b97583bef89f5abcMark AndrewsAll error conditions must be handled.<P>
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews
819fe493f97078521bb6b9a7b97583bef89f5abcMark AndrewsMixing of error status and valid results within a single type should be
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrewsavoided.<P>
819fe493f97078521bb6b9a7b97583bef89f5abcMark AndrewsGood:
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews<PRE><CODE>
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews os_descriptor_t s;
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews os_result_t result;
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews result = os_socket_create(AF_INET, SOCK_STREAM, 0, &s);
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews if (result != OS_R_SUCCESS) {
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews /* Do something about the error. */
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews return;
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews }
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews</CODE></PRE>
819fe493f97078521bb6b9a7b97583bef89f5abcMark AndrewsNot so good:
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews<PRE><CODE>
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews int s;
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews /*
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews * Obviously using interfaces like socket() (below) is allowed
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews * since otherwise you couldn't call operating system routines; the
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews * point is not to write more interfaces like them.
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews */
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews s = socket(AF_INET, SOCK_STREAM, 0);
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews if (s < 0) {
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews /* Do something about the error using errno. */
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews return;
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews }
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews</CODE></PRE>
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrews<H4>Integral Types</H4>
819fe493f97078521bb6b9a7b97583bef89f5abcMark AndrewsCareful thought should be given to whether an integral type should be
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrewssigned or unsigned, and to whether a specific size is required. "int"
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrewsshould be used for generic variables (e.g. iteration counters, array
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrewssubscripts). Other than for generic variables, if a negative value isn't
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrewsmeaningful, the variable should be unsigned. Assignments and
819fe493f97078521bb6b9a7b97583bef89f5abcMark Andrewscomparisons between signed and unsigned integers should be avoided;
33d96fbbc8aa221508f3c780539bf44810fd2c9cMark Andrewssuppressing the warnings with casts is not desireable.<P>
33d96fbbc8aa221508f3c780539bf44810fd2c9cMark Andrews
33d96fbbc8aa221508f3c780539bf44810fd2c9cMark Andrews<H4>Casting</H4>
33d96fbbc8aa221508f3c780539bf44810fd2c9cMark AndrewsCasting should be avoided when possible. When it is necessary, there
33d96fbbc8aa221508f3c780539bf44810fd2c9cMark Andrewsshould be no space between the cast and what is being cast.<P>
26440aaebba1acb5c8810f7faa26ad3b7553762eMark Andrews
819fe493f97078521bb6b9a7b97583bef89f5abcMark AndrewsBad (obviously for more than one reason ...):
<PRE><CODE>
(void) malloc(SMBUF);
</CODE></PRE>
<H4>Clear Success or Failure</H4>
A function should report success or failure, and do so accurately. It
should never fail silently. Use of Design by Contract can help here.<P>
<H4>Testing Bits</H4>
Bit testing should be as follows:<P>
Good:
<PRE><CODE>
/* Test if flag set. */
if ((flags & FOO) != 0) {
}
/* Test if flag clear. */
if ((flags & BAR) == 0) {
}
/* Test if both flags set. */
if ((flags & (FOO|BAR)) == (FOO|BAR)) {
}
</CODE></PRE>
Bad:
<PRE><CODE>
/* Test if flag set. */
if (flags & FOO) {
}
/* Test if flag clear. */
if (! (flags & BAR)) {
}
</CODE></PRE>
<H4>Pointers</H4>
<H5>Null Pointer</H5>
The null pointer value should be referred to with "NULL", not with "0".
Testing to see whether a pointer is NULL should be explicit.<P>
Good:
<PRE><CODE>
char *c = NULL;
/* ... */
if (c == NULL) {
/* Do something. */
}
</CODE></PRE>
<H5>Invalidating Pointers</H5>
When the data a pointer points to has been freed, or is otherwise no longer
valid, the pointer should be set to NULL unless the pointer is part of a
structure which is itself going to be freed immediately.<P>
Good:
<PRE><CODE>
char *text;
/* text is initalized here. */
free(text);
text = NULL;
</CODE></PRE>
<H4>Testing for Zero or Non-zero</H4>
Explicit testing against zero is required for numeric, non-boolean variables.
<P>
Good:
<PRE><CODE>
int i = 10;
/* ... */
if (i != 0) {
/* Do something. */
}
</CODE></PRE>
Bad:
<PRE><CODE>
int i = 10;
/* ... */
if (i) {
/* Do something. */
}
</CODE></PRE>
<H4>The Ternary Operator</H4>
The ?: operator should mostly be avoided. It is tolerated when
deciding what value to pass as a parameter to a function, such as
frequently happens with printf, and also when a simple (non-compound)
value is being used in assignment or as part of a calculation.
In particular, using the ternary operator to specify a return value is
verboten.<P>
Good:
<PRE><CODE>
printf("%c is%s a number.\n", c, isdigit(c) ? "" " NOT");
l = (l1 < l2) ? l1 : l2;
if (gp.length + (go < 16384 ? 2 : 3) >= name->length) {
...
}
</CODE></PRE>
Bad:
<PRE><CODE>
return (success ? ISC_R_SUCESS : ISC_R_FAILURE);
</CODE></PRE>
<H4>Assignment in Parameters</H4>
Variables should not have their values assigned or changed when being
passed as parameters, except perhaps for the increment and decrement
operators.<P>
Bad:
<PRE><CODE>
malloc(size = 20);
</CODE></PRE>
Ok:
<PRE><CODE>
fputc(c++, stdout);
</CODE></PRE>
<H3>Namespace</H3>
<H4>Public Interfaces</H4>
All public interfaces to functions, macros, typedefs, and
variables provided by the library, should use names of the form
{library}_{module}_{what}, such as:
<PRE><CODE>
isc_buffer_t /* typedef */
dns_name_setbuffer(name, buffer) /* function */
ISC_LIST_HEAD(list) /* macro */
isc_commandline_argument /* variable */
</CODE></PRE>
however, structures which are typedef'd generally have the name of the
typedef sans the final _t:
<PRE><CODE>
struct dns_rbtnode {
/* ... members ... */
}
</CODE></PRE>
Generally speaking macros are defined with all capital letters, but
this is not universally consistent (eg, numerous isc_buffer_{foo}
macros).<P>
The {module} and {what} segments of the name do not have underscores
separating natural word elements, as demonstrated in
isc_commandline_argument and dns_name_setbuffer above. The {module}
part is usually the same as the basename of the source file, but
sometimes other {module} interfaces appear within one file, such as
dns_label_* interfaces in lib/dns/name.c. However, in the public
libraries the file name must be the same as some module interface
provided by the file; e.g., dns_rbt_* interfaces would not be declared
in a file named redblack.c (in lieu of any other dns_redblack_*
interfaces in the file).<P>
The one notable exception to this naming rule is the interfaces
provided by <isc/util.h>. There's a large caveat associated with the
public description of this file that it is hazardous to use because it
pollutes the general namespace.<P>
<H4>Shared Private Interfaces</H4>
When a module provides an interface for internal use by other modules
in the library, it should use the same naming convention
described for the public interfaces, except {library} and {module}
are separated by a double-underscore. This indicates that the name is
internal, its API is not as formal as the public API, and thus it
might change without any sort of notice.
<H3>Initialization</H3>
When an object is allocated from the heap, all fields in the object must be
initialized.<P>
<H3>Dead Code Pruning</H3>
Source which becomes obsolete should be removed, not just disabled with
#if 0 ... #endif.<P>