xref: /freebsd/share/man/man9/style.9 (revision b633e08c705fe43180567eae26923d6f6f98c8d9)
1.\"-
2.\" Copyright (c) 1995-2019 The FreeBSD Project
3.\"
4.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions
6.\" are met:
7.\" 1. Redistributions of source code must retain the above copyright
8.\"    notice, this list of conditions and the following disclaimer.
9.\" 2. Redistributions in binary form must reproduce the above copyright
10.\"    notice, this list of conditions and the following disclaimer in the
11.\"    documentation and/or other materials provided with the distribution.
12.\"
13.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16.\" ARE DISCLAIMED.  IN NO EVENT SHALL [your name] OR CONTRIBUTORS BE LIABLE
17.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23.\" SUCH DAMAGE.
24.\"
25.\"	From: @(#)style	1.14 (Berkeley) 4/28/95
26.\" $FreeBSD$
27.\"
28.Dd October 28, 2020
29.Dt STYLE 9
30.Os
31.Sh NAME
32.Nm style
33.Nd "kernel source file style guide"
34.Sh DESCRIPTION
35This file specifies the preferred style for kernel source files in the
36.Fx
37source tree.
38It is also a guide for the preferred userland code style.
39The preferred line width is 80 characters, but some exceptions are
40made when a slightly longer line is clearer or easier to read.
41Anything that is frequently grepped for, such as diagnostic, error or panic
42messages, should not be broken up over multiple lines despite this rule.
43Many of the style rules are implicit in the examples.
44Be careful to check the examples before assuming that
45.Nm
46is silent on an issue.
47.Bd -literal
48/*
49 * Style guide for FreeBSD.  Based on the CSRG's KNF (Kernel Normal Form).
50 *
51 *	@(#)style	1.14 (Berkeley) 4/28/95
52 * $FreeBSD$
53 */
54
55/*
56 * VERY important single-line comments look like this.
57 */
58
59/* Most single-line comments look like this. */
60
61/*
62 * Multi-line comments look like this.  Make them real sentences.  Fill
63 * them so they look like real paragraphs.
64 */
65.Ed
66.Pp
67The copyright header should be a multi-line comment, with the first
68line of the comment having a dash after the star like so:
69.Bd -literal
70/*-
71 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
72 *
73 * Copyright (c) 1984-2025 John Q. Public
74 *
75 * Long, boring license goes here, but trimmed for brevity
76 */
77.Ed
78.Pp
79An automatic script collects license information from the tree for
80all comments that start in the first column with
81.Dq Li "/*-" .
82If you desire to flag
83.Xr indent 1
84to not reformat a comment that starts in the first column which is not a
85license or copyright notice, change the dash to a star for those
86comments.
87Comments starting in columns other than the first are never
88considered license statements.
89Use the appropriate SPDX-License-Identifier line before the copyright.
90If the copyright assertion contains the phrase
91.Dq Li "All Rights Reserved"
92that should be on the same line as the word
93.Dq Li "Copyright" .
94You should not insert a new copyright line between an old
95copyright line and this phrase.
96Instead, you should insert a new copyright phrase after
97a pre-existing
98.Dq Li "All Rights Reserved"
99line.
100When making changes, it is acceptable to fold an
101.Dq Li "All Rights Reserved"
102line with each of the
103.Dq Li "Copyright"
104lines.
105For files that have the
106.Dq Li "All Rights Reserved"
107line on the same line(s) as the word
108.Dq Li "Copyright" ,
109new copyright assertions should be added last.
110New
111.Dq Li "Copyright"
112lines should only be added when making substantial changes to the file,
113not for trivial changes.
114.Pp
115After any copyright and license comment, there is a blank line.
116If your code needs to be merged into stable/12 or earlier, it
117needs to have the
118.Li $\&FreeBSD$
119tag.
120Otherwise, this tag should be omitted in new code.
121Legacy code will have the tag removed in the future.
122For non C/C++ language source files,
123.Li $\&FreeBSD$
124is next, if applicable.
125Non-C/C++ source files follow the example above, while C/C++ source files
126follow the one below.
127Version control system ID tags should only exist once in a file
128(unlike in this one).
129All VCS (version control system) revision identification in files obtained
130from elsewhere should be maintained, including, where applicable, multiple IDs
131showing a file's history.
132In general, do not edit foreign IDs or their infrastructure.
133Unless otherwise wrapped (such as
134.Dq Li "#if defined(LIBC_SCCS)" ) ,
135enclose both in
136.Dq Li "#if 0 ... #endif"
137to hide any uncompilable bits
138and to keep the IDs out of object files.
139Only add
140.Dq Li "From: "
141in front of foreign VCS IDs if the file is renamed.
142Add
143.Dq Li "From: "
144and FreeBSD git hash with full path name if the file was derived
145from another FreeBSD file and include relevant copyright info
146from the original file.
147.Bd -literal
148/* From: @(#)style	1.14 (Berkeley) 4/28/95 */
149
150#include <sys/cdefs.h>
151__FBSDID("$FreeBSD$");
152.Ed
153.Pp
154Leave one blank line before the header files.
155.Pp
156Kernel include files
157.Pa ( sys/*.h )
158come first.
159If
160.In sys/cdefs.h
161is needed for
162.Fn __FBSDID ,
163include it first.
164If either
165.In sys/types.h
166or
167.In sys/param.h
168is needed, include it before other include files.
169.Po
170.In sys/param.h
171includes
172.In sys/types.h ;
173do not include both.
174.Pc
175Next, include
176.In sys/systm.h ,
177if needed.
178The remaining kernel headers should be sorted alphabetically.
179.Bd -literal
180#include <sys/types.h>	/* Non-local includes in angle brackets. */
181#include <sys/systm.h>
182#include <sys/endian.h>
183#include <sys/lock.h>
184#include <sys/queue.h>
185.Ed
186.Pp
187For a network program, put the network include files next.
188.Bd -literal
189#include <net/if.h>
190#include <net/if_dl.h>
191#include <net/route.h>
192#include <netinet/in.h>
193#include <protocols/rwhod.h>
194.Ed
195.Pp
196Do not include files from
197.Pa /usr/include
198in the kernel.
199.Pp
200Leave a blank line before the next group, the
201.Pa /usr/include
202files,
203which should be sorted alphabetically by name.
204.Bd -literal
205#include <stdio.h>
206.Ed
207.Pp
208Global pathnames are defined in
209.In paths.h .
210Pathnames local
211to the program go in
212.Qq Pa pathnames.h
213in the local directory.
214.Bd -literal
215#include <paths.h>
216.Ed
217.Pp
218Leave another blank line before the local include files.
219.Bd -literal
220#include "pathnames.h"		/* Local includes in double quotes. */
221.Ed
222.Pp
223Do not
224.Ic #define
225or declare names in the implementation namespace except
226for implementing application interfaces.
227.Pp
228The names of
229.Dq unsafe
230macros (ones that have side effects), and the names of macros for
231manifest constants, are all in uppercase.
232The expansions of expression-like macros are either a single token
233or have outer parentheses.
234Put a single space or tab character between the
235.Ic #define
236and the macro name, but be consistent within a file.
237If a macro is an inline expansion of a function, the function name is
238all in lowercase and the macro has the same name all in uppercase.
239.\" XXX the above conflicts with ANSI style where the names are the
240.\" same and you #undef the macro (if any) to get the function.
241.\" It is not followed for MALLOC(), and not very common if inline
242.\" functions are used.
243Right-justify the
244backslashes; it makes it easier to read.
245If the macro encapsulates a compound statement, enclose it in a
246.Ic do
247loop,
248so that it can safely be used in
249.Ic if
250statements.
251Any final statement-terminating semicolon should be
252supplied by the macro invocation rather than the macro, to make parsing easier
253for pretty-printers and editors.
254.Bd -literal
255#define	MACRO(x, y) do {						\e
256	variable = (x) + (y);						\e
257	(y) += 2;							\e
258} while (0)
259.Ed
260.Pp
261When code is conditionally compiled using
262.Ic #ifdef
263or
264.Ic #if ,
265a comment may be added following the matching
266.Ic #endif
267or
268.Ic #else
269to permit the reader to easily discern where conditionally compiled code
270regions end.
271This comment should be used only for (subjectively) long regions, regions
272greater than 20 lines, or where a series of nested
273.Ic #ifdef 's
274may be confusing to the reader.
275The comment should be separated from the
276.Ic #endif
277or
278.Ic #else
279by a single space.
280For short conditionally compiled regions, a closing comment should not be
281used.
282.Pp
283The comment for
284.Ic #endif
285should match the expression used in the corresponding
286.Ic #if
287or
288.Ic #ifdef .
289The comment for
290.Ic #else
291and
292.Ic #elif
293should match the inverse of the expression(s) used in the preceding
294.Ic #if
295and/or
296.Ic #elif
297statements.
298In the comments, the subexpression
299.Dq Li defined(FOO)
300is abbreviated as
301.Dq Li FOO .
302For the purposes of comments,
303.Dq Ic #ifndef Li FOO
304is treated as
305.Dq Ic #if Li !defined(FOO) .
306.Bd -literal
307#ifdef KTRACE
308#include <sys/ktrace.h>
309#endif
310
311#ifdef COMPAT_43
312/* A large region here, or other conditional code. */
313#else /* !COMPAT_43 */
314/* Or here. */
315#endif /* COMPAT_43 */
316
317#ifndef COMPAT_43
318/* Yet another large region here, or other conditional code. */
319#else /* COMPAT_43 */
320/* Or here. */
321#endif /* !COMPAT_43 */
322.Ed
323.Pp
324The project prefers the use of
325.St -isoC-99
326unsigned integer identifiers of the form
327.Vt uintXX_t
328rather than the older
329.Bx Ns -style
330integer identifiers of the form
331.Vt u_intXX_t .
332New code should use the former, and old code should be converted to
333the new form if other major work is being done in that area and
334there is no overriding reason to prefer the older
335.Bx Ns -style .
336Like white-space commits, care should be taken in making
337.Vt uintXX_t
338only commits.
339.Pp
340Similarly, the project prefers the use of
341ISO C99
342.Vt bool
343rather than the older
344.Vt int
345or
346.Vt boolean_t .
347New code should use
348.Vt bool ,
349and old code may be converted if it is
350reasonable to do so.
351Literal values are named
352.Dv true
353and
354.Dv false .
355These are preferred to the old spellings
356.Dv TRUE
357and
358.Dv FALSE .
359Userspace code should include
360.In stdbool.h ,
361while kernel code should include
362.In sys/types.h .
363.Pp
364Likewise, the project prefers
365ISO C99
366designated initializers when it makes sense to do so.
367.Pp
368Enumeration values are all uppercase.
369.Bd -literal
370enum enumtype { ONE, TWO } et;
371.Ed
372.Pp
373The use of internal_underscores in identifiers is preferred over
374camelCase or TitleCase.
375.Pp
376In declarations, do not put any whitespace between asterisks and
377adjacent tokens, except for tokens that are identifiers related to
378types.
379(These identifiers are the names of basic types, type
380qualifiers, and
381.Ic typedef Ns -names
382other than the one being declared.)
383Separate these identifiers from asterisks using a single space.
384.Pp
385When declaring variables in structures, declare them sorted by use, then
386by size (largest to smallest), and then in alphabetical order.
387The first category normally does not apply, but there are exceptions.
388Each one gets its own line.
389Try to make the structure
390readable by aligning the member names using either one or two tabs
391depending upon your judgment.
392You should use one tab only if it suffices to align at least 90% of
393the member names.
394Names following extremely long types
395should be separated by a single space.
396.Pp
397Major structures should be declared at the top of the file in which they
398are used, or in separate header files if they are used in multiple
399source files.
400Use of the structures should be by separate declarations
401and should be
402.Ic extern
403if they are declared in a header file.
404.Bd -literal
405struct foo {
406	struct foo	*next;		/* List of active foo. */
407	struct mumble	amumble;	/* Comment for mumble. */
408	int		bar;		/* Try to align the comments. */
409	struct verylongtypename *baz;	/* Does not fit in 2 tabs. */
410};
411struct foo *foohead;			/* Head of global foo list. */
412.Ed
413.Pp
414Use
415.Xr queue 3
416macros rather than rolling your own lists, whenever possible.
417Thus,
418the previous example would be better written:
419.Bd -literal
420#include <sys/queue.h>
421
422struct foo {
423	LIST_ENTRY(foo)	link;		/* Use queue macros for foo lists. */
424	struct mumble	amumble;	/* Comment for mumble. */
425	int		bar;		/* Try to align the comments. */
426	struct verylongtypename *baz;	/* Does not fit in 2 tabs. */
427};
428LIST_HEAD(, foo) foohead;		/* Head of global foo list. */
429.Ed
430.Pp
431Avoid using typedefs for structure types.
432Typedefs are problematic because they do not properly hide their
433underlying type; for example you need to know if the typedef is
434the structure itself or a pointer to the structure.
435In addition they must be declared exactly once, whereas an
436incomplete structure type can be mentioned as many times as
437necessary.
438Typedefs are difficult to use in stand-alone header files:
439the header that defines the typedef must be included
440before the header that uses it, or by the header that uses
441it (which causes namespace pollution), or there must be a
442back-door mechanism for obtaining the typedef.
443.Pp
444When convention requires a
445.Ic typedef ,
446make its name match the struct tag.
447Avoid typedefs ending in
448.Dq Li _t ,
449except as specified in Standard C or by POSIX.
450.Bd -literal
451/* Make the structure name match the typedef. */
452typedef	struct bar {
453	int	level;
454} BAR;
455typedef	int		foo;		/* This is foo. */
456typedef	const long	baz;		/* This is baz. */
457.Ed
458.Pp
459All functions are prototyped somewhere.
460.Pp
461Function prototypes for private functions (i.e., functions not used
462elsewhere) go at the top of the first source module.
463Functions
464local to one source module should be declared
465.Ic static .
466.Pp
467Functions used from other parts of the kernel are prototyped in the
468relevant include file.
469Function prototypes should be listed in a logical order, preferably
470alphabetical unless there is a compelling reason to use a different
471ordering.
472.Pp
473Functions that are used locally in more than one module go into a
474separate header file, e.g.,
475.Qq Pa extern.h .
476.Pp
477Do not use the
478.Dv __P
479macro.
480.Pp
481In general code can be considered
482.Dq "new code"
483when it makes up about 50% or more of the file(s) involved.
484This is enough
485to break precedents in the existing code and use the current
486.Nm
487guidelines.
488.Pp
489The kernel has a name associated with parameter types, e.g., in the kernel
490use:
491.Bd -literal
492void	function(int fd);
493.Ed
494.Pp
495In header files visible to userland applications, prototypes that are
496visible must use either
497.Dq protected
498names (ones beginning with an underscore)
499or no names with the types.
500It is preferable to use protected names.
501E.g., use:
502.Bd -literal
503void	function(int);
504.Ed
505.Pp
506or:
507.Bd -literal
508void	function(int _fd);
509.Ed
510.Pp
511Prototypes may have an extra space after a tab to enable function names
512to line up:
513.Bd -literal
514static char	*function(int _arg, const char *_arg2, struct foo *_arg3,
515		    struct bar *_arg4);
516static void	 usage(void);
517
518/*
519 * All major routines should have a comment briefly describing what
520 * they do.  The comment before the "main" routine should describe
521 * what the program does.
522 */
523int
524main(int argc, char *argv[])
525{
526	char *ep;
527	long num;
528	int ch;
529.Ed
530.Pp
531For consistency,
532.Xr getopt 3
533should be used to parse options.
534Options
535should be sorted in the
536.Xr getopt 3
537call and the
538.Ic switch
539statement, unless
540parts of the
541.Ic switch
542cascade.
543Elements in a
544.Ic switch
545statement that cascade should have a
546.Li FALLTHROUGH
547comment.
548Numerical arguments should be checked for accuracy.
549Code which is unreachable for non-obvious reasons may be marked /*
550.Li NOTREACHED
551*/.
552.Bd -literal
553	while ((ch = getopt(argc, argv, "abNn:")) != -1)
554		switch (ch) {		/* Indent the switch. */
555		case 'a':		/* Do not indent the case. */
556			aflag = 1;	/* Indent case body one tab. */
557			/* FALLTHROUGH */
558		case 'b':
559			bflag = 1;
560			break;
561		case 'N':
562			Nflag = 1;
563			break;
564		case 'n':
565			num = strtol(optarg, &ep, 10);
566			if (num <= 0 || *ep != '\e0') {
567				warnx("illegal number, -n argument -- %s",
568				    optarg);
569				usage();
570			}
571			break;
572		case '?':
573		default:
574			usage();
575		}
576	argc -= optind;
577	argv += optind;
578.Ed
579.Pp
580Space after keywords
581.Pq Ic if , while , for , return , switch .
582Two styles of braces
583.Ql ( \&{
584and
585.Ql \&} )
586are allowed for single line statements.
587Either they are used for all single statements, or
588they are used only where needed for clarity.
589Usage within a function should be consistent.
590Forever loops are done with
591.Ic for Ns 's ,
592not
593.Ic while Ns 's .
594.Bd -literal
595	for (p = buf; *p != '\e0'; ++p)
596		;	/* nothing */
597	for (;;)
598		stmt;
599	for (;;) {
600		z = a + really + long + statement + that + needs +
601		    two + lines + gets + indented + four + spaces +
602		    on + the + second + and + subsequent + lines;
603	}
604	for (;;) {
605		if (cond)
606			stmt;
607	}
608	if (val != NULL)
609		val = realloc(val, newsize);
610.Ed
611.Pp
612Parts of a
613.Ic for
614loop may be left empty.
615.Bd -literal
616	for (; cnt < 15; cnt++) {
617		stmt1;
618		stmt2;
619	}
620.Ed
621.Pp
622A
623.Ic for
624loop may declare and initialize its counting variable.
625.Bd -literal
626	for (int i = 0; i < 15; i++) {
627		stmt1;
628	}
629.Ed
630.Pp
631Indentation is an 8 character tab.
632Second level indents are four spaces.
633If you have to wrap a long statement, put the operator at the end of the
634line.
635.Bd -literal
636	while (cnt < 20 && this_variable_name_is_too_long &&
637	    ep != NULL)
638		z = a + really + long + statement + that + needs +
639		    two + lines + gets + indented + four + spaces +
640		    on + the + second + and + subsequent + lines;
641.Ed
642.Pp
643Do not add whitespace at the end of a line, and only use tabs
644followed by spaces
645to form the indentation.
646Do not use more spaces than a tab will produce
647and do not use spaces in front of tabs.
648.Pp
649Closing and opening braces go on the same line as the
650.Ic else .
651Braces that are not necessary may be left out.
652.Bd -literal
653	if (test)
654		stmt;
655	else if (bar) {
656		stmt;
657		stmt;
658	} else
659		stmt;
660.Ed
661.Pp
662No spaces after function names.
663Commas have a space after them.
664No spaces
665after
666.Ql \&(
667or
668.Ql \&[
669or preceding
670.Ql \&]
671or
672.Ql \&)
673characters.
674.Bd -literal
675	error = function(a1, a2);
676	if (error != 0)
677		exit(error);
678.Ed
679.Pp
680Unary operators do not require spaces, binary operators do.
681Do not use parentheses unless they are required for precedence or unless the
682statement is confusing without them.
683Remember that other people may
684confuse easier than you.
685Do YOU understand the following?
686.Bd -literal
687	a = b->c[0] + ~d == (e || f) || g && h ? i : j >> 1;
688	k = !(l & FLAGS);
689.Ed
690.Pp
691Exits should be 0 on success, or 1 on failure.
692.Bd -literal
693	exit(0);	/*
694			 * Avoid obvious comments such as
695			 * "Exit 0 on success."
696			 */
697}
698.Ed
699.Pp
700The function type should be on a line by itself
701preceding the function.
702The opening brace of the function body should be
703on a line by itself.
704.Bd -literal
705static char *
706function(int a1, int a2, float fl, int a4, struct bar *bar)
707{
708.Ed
709.Pp
710When declaring variables in functions declare them sorted by size,
711then in alphabetical order; multiple ones per line are okay.
712If a line overflows reuse the type keyword.
713Variables may be initialized where declared especially when they
714are constant for the rest of the scope.
715Declarations may be placed before executable lines at the start
716of any block.
717Calls to complicated functions should be avoided when initializing variables.
718.Bd -literal
719	struct foo one, *two;
720	struct baz *three = bar_get_baz(bar);
721	double four;
722	int *five, six;
723	char *seven, eight, nine, ten, eleven, twelve;
724
725	four = my_complicated_function(a1, f1, a4);
726.Ed
727.Pp
728Do not declare functions inside other functions; ANSI C says that
729such declarations have file scope regardless of the nesting of the
730declaration.
731Hiding file declarations in what appears to be a local
732scope is undesirable and will elicit complaints from a good compiler.
733.Pp
734Casts and
735.Ic sizeof Ns 's
736are not followed by a space.
737Note that
738.Xr indent 1
739does not understand this rule.
740.Ic sizeof Ns 's
741are written with parenthesis always.
742The redundant parenthesis rules do not apply to
743.Fn sizeof var
744instances.
745.Pp
746.Dv NULL
747is the preferred null pointer constant.
748Use
749.Dv NULL
750instead of
751.Vt ( "type *" ) Ns 0
752or
753.Vt ( "type *" ) Ns Dv NULL
754in contexts where the compiler knows the
755type, e.g., in assignments.
756Use
757.Vt ( "type *" ) Ns Dv NULL
758in other contexts,
759in particular for all function args.
760(Casting is essential for
761variadic args and is necessary for other args if the function prototype
762might not be in scope.)
763Test pointers against
764.Dv NULL ,
765e.g., use:
766.Bd -literal
767(p = f()) == NULL
768.Ed
769.Pp
770not:
771.Bd -literal
772!(p = f())
773.Ed
774.Pp
775Do not use
776.Ic \&!
777for tests unless it is a boolean, e.g., use:
778.Bd -literal
779if (*p == '\e0')
780.Ed
781.Pp
782not:
783.Bd -literal
784if (!*p)
785.Ed
786.Pp
787Routines returning
788.Vt "void *"
789should not have their return values cast
790to any pointer type.
791.Pp
792Values in
793.Ic return
794statements should be enclosed in parentheses.
795.Pp
796Use
797.Xr err 3
798or
799.Xr warn 3 ,
800do not roll your own.
801.Bd -literal
802	if ((four = malloc(sizeof(struct foo))) == NULL)
803		err(1, (char *)NULL);
804	if ((six = (int *)overflow()) == NULL)
805		errx(1, "number overflowed");
806	return (eight);
807}
808.Ed
809.Pp
810When converting K&R style declarations to ANSI style, preserve
811any comments about parameters.
812.Pp
813Long parameter lists are wrapped with a normal four space indent.
814.Pp
815Variable numbers of arguments should look like this:
816.Bd -literal
817#include <stdarg.h>
818
819void
820vaf(const char *fmt, ...)
821{
822	va_list ap;
823
824	va_start(ap, fmt);
825	STUFF;
826	va_end(ap);
827	/* No return needed for void functions. */
828}
829
830static void
831usage(void)
832{
833	/* Optional blank line goes here. */
834.Ed
835.Pp
836Optionally, insert a blank line at the beginning of functions with no local
837variables.
838Older versions of this
839.Nm
840document required the blank line convention, so it is widely used in existing
841code.
842.Pp
843Do not insert a blank line at the beginning of functions with local variables.
844Instead, these should have local variable declarations first, followed by one
845blank line, followed by the first statement.
846.Pp
847Use
848.Xr printf 3 ,
849not
850.Xr fputs 3 ,
851.Xr puts 3 ,
852.Xr putchar 3 ,
853whatever; it is faster and usually cleaner, not
854to mention avoiding stupid bugs.
855.Pp
856Usage statements should look like the manual pages
857.Sx SYNOPSIS .
858The usage statement should be structured in the following order:
859.Bl -enum
860.It
861Options without operands come first,
862in alphabetical order,
863inside a single set of brackets
864.Ql ( \&[
865and
866.Ql \&] ) .
867.It
868Options with operands come next,
869also in alphabetical order,
870with each option and its argument inside its own pair of brackets.
871.It
872Required arguments
873(if any)
874are next,
875listed in the order they should be specified on the command line.
876.It
877Finally,
878any optional arguments should be listed,
879listed in the order they should be specified,
880and all inside brackets.
881.El
882.Pp
883A bar
884.Pq Ql \&|
885separates
886.Dq either-or
887options/arguments,
888and multiple options/arguments which are specified together are
889placed in a single set of brackets.
890.Bd -literal -offset 4n
891"usage: f [-aDde] [-b b_arg] [-m m_arg] req1 req2 [opt1 [opt2]]\en"
892"usage: f [-a | -b] [-c [-dEe] [-n number]]\en"
893.Ed
894.Bd -literal
895	(void)fprintf(stderr, "usage: f [-ab]\en");
896	exit(1);
897}
898.Ed
899.Pp
900Note that the manual page options description should list the options in
901pure alphabetical order.
902That is, without regard to whether an option takes arguments or not.
903The alphabetical ordering should take into account the case ordering
904shown above.
905.Pp
906New core kernel code should be reasonably compliant with the
907.Nm
908guides.
909The guidelines for third-party maintained modules and device drivers are more
910relaxed but at a minimum should be internally consistent with their style.
911.Pp
912Stylistic changes (including whitespace changes) are hard on the source
913repository and are to be avoided without good reason.
914Code that is approximately
915.Fx
916KNF
917.Nm
918compliant in the repository must not diverge from compliance.
919.Pp
920Whenever possible, code should be run through a code checker
921(e.g., various static analyzers or
922.Nm cc Fl Wall )
923and produce minimal warnings.
924.Pp
925New code should use
926.Fn _Static_assert
927instead of the older
928.Fn CTASSERT .
929.Sh FILES
930.Bl -tag -width indent
931.It Pa /usr/src/tools/tools/editing/freebsd.el
932An Emacs plugin to follow the
933.Fx
934.Nm
935indentation rules.
936.It Pa /usr/src/tools/tools/editing/freebsd.vim
937A Vim plugin to follow the
938.Fx
939.Nm
940indentation rules.
941.El
942.Sh SEE ALSO
943.Xr indent 1 ,
944.Xr err 3 ,
945.Xr warn 3 ,
946.Xr style.Makefile 5 ,
947.Xr style.mdoc 5 ,
948.Xr style.lua 9
949.Sh HISTORY
950This manual page is largely based on the
951.Pa src/admin/style/style
952file from the
953.Bx 4.4 Lite2
954release, with occasional updates to reflect the current practice and
955desire of the
956.Fx
957project.
958.Pa src/admin/style/style
959is a codification by the CSRG of the programming style of Ken Thompson and
960Dennis Ritchie in
961.At v6 .
962