xref: /original-bsd/admin/style/style (revision 7eb91141)
1/*
2 * Style guide for BSD's KNF (Kernel Normal Form).
3 *
4 *	@(#)style	1.10 (Berkeley) 02/11/92
5 */
6
7/*
8 * VERY important single-line comments look like this.
9 */
10
11/* Most single-line comments look like this. */
12
13/*
14 * Multi-line comments look like this.  Make them real sentences.  Fill
15 * them so they look like real paragraphs.
16 */
17
18/* Include files go at the top of the source module. */
19#include <stdio.h>		/* Non-local includes in brackets. */
20
21/*
22 * Global pathnames are defined in /usr/include/paths.h.  Pathnames local
23 * to the program go in pathnames.h in the local directory.
24 */
25#include <paths.h>		/* Non-local includes in brackets. */
26#include "pathnames.h"		/* Local includes in quotes. */
27
28/*
29 * All ANSI function decls go at the top of the source module.  Use the
30 * __P macro from include file <sys/cdefs.h>.  Only the kernel has a name
31 * associated with the types, i.e. in the kernel use:
32 *
33 *	void function __P((int a));
34 *
35 * in user land use:
36 *
37 *	void function __P((int));
38 */
39void function __P((int, const char *));
40
41/*
42 * Macros are capitalized, parenthesized, and should avoid side-effects.
43 * If they are an inline expansion of a function, the function is defined
44 * all in lowercase, the macro has the same name all in uppercase. If the
45 * macro needs more than a single line, use braces.  Put a space before
46 * the backslashes.
47 */
48#define	MACRO(x, y) { \
49	variable = (x) + (y); \
50	line two; \
51}
52
53/* Enum types are capitalized. */
54enum enumtype { ONE, TWO } et;
55
56/*
57 * When declaring variables in structures, declare them sorted by use, then
58 * by size, and then by alphabetical order.  The first category normally
59 * doesn't apply, but there are exceptions.  Each one gets its own line.
60 * Put a tab after the first word, i.e. use "int^Ix;" and "struct^Ifoo *x;".
61 *
62 * Major structures should be declared at the top of the file they are
63 * used in, or in separate header files, if they are used in multiple
64 * source files. Use of the structures should be by separate declarations
65 * and should be "extern" if they are declared in a header file.
66 */
67struct foo {
68	struct	foo *next;	/* List of active foo */
69	struct	mumble amumble;	/* Comment for mumble */
70	int	bar;
71};
72struct foo *foohead;		/* Head of global foo list */
73
74/*
75 * All major routines should have a comment briefly describing what
76 * they do. The comment before the "main" routine should describe
77 * what the program does.
78 */
79main(argc, argv)
80	int argc;
81	char *argv[];
82{
83	extern char *optarg;
84	extern int optind;
85	long num;
86	int ch;
87	char *ep;
88
89	/*
90	 * For consistency, getopt should be used to parse options.
91	 * Options should be sorted in the getopt call and the switch
92	 * statement, unless they fall through.  Elements in a switch
93	 * statement that fall through should have a FALLTHROUGH comment.
94	 * Numerical arguments should be checked for accuracy.
95	 */
96	while ((ch = getopt(argc, argv, "abn")) != EOF)
97		switch (ch) {		/* Indent the switch. */
98		case 'a':		/* Don't indent the case. */
99			aflag = 1;
100			/* FALLTHROUGH */
101		case 'b':
102			bflag = 1;
103			break;
104		case 'n':
105			num = strtol(optarg, &ep, 10);
106                        if (num <= 0 || *ep)
107                                err("illegal number -- %s", optarg);
108			break;
109		case '?':
110		default:
111			usage();
112		}
113	argc -= optind;
114	argv += optind;
115
116	/*
117	 * Space after keywords (while, for, return, switch).  No braces are
118	 * used for single statement block.
119	 *
120	 * Forever loops are done with for's, not while's.
121	 */
122	for (;;)
123		stmt;
124
125	/*
126	 * Parts of a for loop may be left empty.  Avoid declarations in
127	 * blocks unless the routine is unusually complicated.
128	 */
129	for (; cnt < 15; cnt++) {
130		stmt1;
131		stmt2;
132	}
133
134	while (cnt < 20) {
135		stmt1;		/* Second level indents are four spaces. */
136		z = a + really + long + statment + that + needs + two lines +
137		    gets + indented + four + spaces + on + the + second +
138		    and + subsequent + lines.
139	}
140
141	/*
142	 * Try to put shorter part first.  The closing and opening braces
143	 * go on the same line as the else.
144	 */
145	if (test)
146		stmt;
147	else if (bar) {
148		stmt;
149		stmt;
150	} else
151		stmt;
152
153	/* No space after function names. */
154	if (error = function(a1, a2))
155		exit(error);
156
157	/*
158	 * Unary operators do not require spaces, binary operators do.
159	 * Try not to use too many parenthesis unless the statement is
160	 * really confusing without them.
161	 */
162	a = b->c[0] + ~d == (e || f) || g && h ? i : j >> 1;
163	k = l & FLAGS;
164
165	/*
166	 * Exits should be 0 on success, and 1 on failure.  Don't denote
167	 * all the possible exit points, using the integers 1 through 300.
168	 */
169	exit(0);    /* Avoid obvious comments such as "Exit 0 on success." */
170}
171
172/*
173 * If a function type is declared, it should be on a line
174 * by itself preceeding the function.
175 */
176static char *
177function(a1, a2, a3, a4)
178	int a1, a2, a4;	/* Declare ints too. */
179	float a3;	/* List in order declared, as much as possible. */
180{
181	/*
182	 * When declaring variables in functions declare them sorted by size,
183	 * then in alphabetical order; multiple ones per line are okay.  Old
184	 * style function declarations can go on the same line.  ANSI style
185	 * function declarations should go in the include file "externs.h".
186	 * If a line overflows reuse the type keyword.
187	 *
188	 * Try not to initialize variables in the declarations.
189	 */
190	extern u_char one;
191	extern char two;
192	struct foo three, *four;
193	double five;
194	int *six, seven, eight();
195	char *nine, ten, eleven, twelve, thirteen, fourteen, fifteen, sixteen;
196	char *overflow __P((void));
197	void *mymalloc __P((u_int));
198
199	/*
200	 * Casts and sizeof's are not followed by a space.  NULL is any
201	 * pointer type, and doesn't need to be cast, so use NULL instead
202	 * of (struct foo *)0 or (struct foo *)NULL.  Also, test pointers
203	 * against NULL, i.e. use:
204	 *
205	 * 	(p = f()) == NULL
206	 * not:
207	 *	!(p = f())
208 	 *
209	 * Routines returning void * should not have their return values cast
210	 * to any pointer type.
211	 */
212	if ((four = malloc(sizeof(struct foo))) == NULL)
213		return (NULL);
214	if ((six = (int *)overflow()) == NULL)
215		return (NULL);
216	return (eight);
217}
218
219/* ANSI function braces look like regular function braces. */
220function(int a1, int a2)
221{
222	...
223}
224
225static void
226usage()
227{	/* Insert an empty line if the function has no local variables. */
228
229	/*
230	 * Use printf(3), not fputs/puts/putchar/whatever, it's faster and
231	 * usually cleaner, not to mention avoiding stupid bugs.
232	 *
233	 * Usage statements should look like the manual pages.  Options w/o
234	 * operands come first, in alphabetical order inside a single set of
235	 * braces.  Followed by options with operands, in alphabetical order,
236	 * each in braces.  Followed by required arguments in the order they
237	 * are specified, followed by optional arguments in the order they
238	 * are specified.  A bar ('|') separates either/or options/arguments,
239	 * and multiple options/arguments which are specified together are
240	 * placed in a single set of braces.
241	 *
242	 * "usage: f [-ade] [-b b_arg] [-m m_arg] req1 req2 [opt1 [opt2]]\n"
243	 * "usage: f [-a | -b] [-c [-de] [-n number]]\n"
244	 */
245	(void)fprintf(stderr, "usage: f [-ab]\n");
246	exit(1);
247}
248