style revision 1.10
11.10Sscottr/* $NetBSD: style,v 1.10 1999/01/29 07:24:20 scottr Exp $ */ 21.6Sthorpej 31.1Scgd/* 41.2Scgd * Style guide for the 4BSD KNF (Kernel Normal Form). 51.1Scgd * 61.2Scgd * from: @(#)style 1.12 (Berkeley) 3/18/94 71.10Sscottr */ 81.10Sscottr/* 91.10Sscottr * An indent(1) profile approximating the style outlined in 101.10Sscottr * this document lives in /usr/share/misc/indent.pro. It is a 111.10Sscottr * useful tool to assist in converting code to KNF, but indent(1) 121.10Sscottr * output generated using this profile must not be considered to 131.10Sscottr * be an authoritative reference. 141.1Scgd */ 151.1Scgd 161.1Scgd/* 171.1Scgd * VERY important single-line comments look like this. 181.1Scgd */ 191.1Scgd 201.1Scgd/* Most single-line comments look like this. */ 211.1Scgd 221.1Scgd/* 231.1Scgd * Multi-line comments look like this. Make them real sentences. Fill 241.1Scgd * them so they look like real paragraphs. 251.1Scgd */ 261.1Scgd 271.2Scgd/* 281.2Scgd * Kernel include files come first; normally, you'll need <sys/types.h> 291.2Scgd * OR <sys/param.h>, but not both! <sys/types.h> includes <sys/cdefs.h>, 301.2Scgd * and it's okay to depend on that. 311.2Scgd */ 321.2Scgd#include <sys/types.h> /* Non-local includes in brackets. */ 331.2Scgd 341.2Scgd/* If it's a network program, put the network include files next. */ 351.2Scgd#include <net/if.h> 361.2Scgd#include <net/if_dl.h> 371.2Scgd#include <net/route.h> 381.2Scgd#include <netinet/in.h> 391.2Scgd#include <protocols/rwhod.h> 401.2Scgd 411.2Scgd/* 421.2Scgd * Then there's a blank line, followed by the /usr include files. 431.2Scgd * The /usr include files should be sorted! 441.2Scgd */ 451.2Scgd#include <stdio.h> 461.1Scgd 471.1Scgd/* 481.1Scgd * Global pathnames are defined in /usr/include/paths.h. Pathnames local 491.1Scgd * to the program go in pathnames.h in the local directory. 501.1Scgd */ 511.2Scgd#include <paths.h> 521.2Scgd 531.2Scgd/* Then, there's a blank line, and the user include files. */ 541.2Scgd#include "pathnames.h" /* Local includes in double quotes. */ 551.1Scgd 561.1Scgd/* 571.2Scgd * ANSI function declarations for private functions (i.e. functions not used 581.2Scgd * elsewhere) go at the top of the source module. Use the __P macro from 591.2Scgd * the include file <sys/cdefs.h>. Only the kernel has a name associated with 601.2Scgd * the types, i.e. in the kernel use: 611.1Scgd * 621.1Scgd * void function __P((int a)); 631.1Scgd * 641.1Scgd * in user land use: 651.1Scgd * 661.1Scgd * void function __P((int)); 671.1Scgd */ 681.2Scgdstatic char *function __P((int, const char *)); 691.2Scgdstatic void usage __P((void)); 701.1Scgd 711.1Scgd/* 721.1Scgd * Macros are capitalized, parenthesized, and should avoid side-effects. 731.1Scgd * If they are an inline expansion of a function, the function is defined 741.1Scgd * all in lowercase, the macro has the same name all in uppercase. If the 751.2Scgd * macro needs more than a single line, use braces. Right-justify the 761.2Scgd * backslashes, it makes it easier to read. 771.1Scgd */ 781.2Scgd#define MACRO(x, y) { \ 791.2Scgd variable = (x) + (y); \ 801.2Scgd (y) += 2; \ 811.1Scgd} 821.1Scgd 831.1Scgd/* Enum types are capitalized. */ 841.1Scgdenum enumtype { ONE, TWO } et; 851.1Scgd 861.1Scgd/* 871.1Scgd * When declaring variables in structures, declare them sorted by use, then 881.1Scgd * by size, and then by alphabetical order. The first category normally 891.1Scgd * doesn't apply, but there are exceptions. Each one gets its own line. 901.1Scgd * Put a tab after the first word, i.e. use "int^Ix;" and "struct^Ifoo *x;". 911.1Scgd * 921.2Scgd * Major structures should be declared at the top of the file in which they 931.2Scgd * are used, or in separate header files, if they are used in multiple 941.2Scgd * source files. Use of the structures should be by separate declarations 951.1Scgd * and should be "extern" if they are declared in a header file. 961.1Scgd */ 971.1Scgdstruct foo { 981.1Scgd struct foo *next; /* List of active foo */ 991.1Scgd struct mumble amumble; /* Comment for mumble */ 1001.1Scgd int bar; 1011.1Scgd}; 1021.1Scgdstruct foo *foohead; /* Head of global foo list */ 1031.2Scgd 1041.2Scgd/* Make the structure name match the typedef. */ 1051.2Scgdtypedef struct _bar { 1061.2Scgd int level; 1071.2Scgd} BAR; 1081.1Scgd 1091.1Scgd/* 1101.1Scgd * All major routines should have a comment briefly describing what 1111.2Scgd * they do. The comment before the "main" routine should describe 1121.1Scgd * what the program does. 1131.1Scgd */ 1141.2Scgdint 1151.1Scgdmain(argc, argv) 1161.1Scgd int argc; 1171.1Scgd char *argv[]; 1181.1Scgd{ 1191.1Scgd extern char *optarg; 1201.1Scgd extern int optind; 1211.1Scgd long num; 1221.1Scgd int ch; 1231.1Scgd char *ep; 1241.1Scgd 1251.1Scgd /* 1261.2Scgd * For consistency, getopt should be used to parse options. Options 1271.2Scgd * should be sorted in the getopt call and the switch statement, unless 1281.2Scgd * parts of the switch cascade. Elements in a switch statement that 1291.2Scgd * cascade should have a FALLTHROUGH comment. Numerical arguments 1301.2Scgd * should be checked for accuracy. Code that cannot be reached should 1311.2Scgd * have a NOTREACHED comment. 1321.1Scgd */ 1331.5Sscottr while ((ch = getopt(argc, argv, "abn")) != -1) 1341.1Scgd switch (ch) { /* Indent the switch. */ 1351.1Scgd case 'a': /* Don't indent the case. */ 1361.1Scgd aflag = 1; 1371.1Scgd /* FALLTHROUGH */ 1381.1Scgd case 'b': 1391.1Scgd bflag = 1; 1401.1Scgd break; 1411.1Scgd case 'n': 1421.1Scgd num = strtol(optarg, &ep, 10); 1431.2Scgd if (num <= 0 || *ep != '\0') 1441.1Scgd err("illegal number -- %s", optarg); 1451.1Scgd break; 1461.1Scgd case '?': 1471.1Scgd default: 1481.1Scgd usage(); 1491.2Scgd /* NOTREACHED */ 1501.1Scgd } 1511.1Scgd argc -= optind; 1521.1Scgd argv += optind; 1531.1Scgd 1541.1Scgd /* 1551.1Scgd * Space after keywords (while, for, return, switch). No braces are 1561.2Scgd * used for control statements with zero or only a single statement. 1571.1Scgd * 1581.1Scgd * Forever loops are done with for's, not while's. 1591.1Scgd */ 1601.2Scgd for (p = buf; *p != '\0'; ++p); 1611.1Scgd for (;;) 1621.1Scgd stmt; 1631.1Scgd 1641.1Scgd /* 1651.2Scgd * Parts of a for loop may be left empty. Don't put declarations 1661.2Scgd * inside blocks unless the routine is unusually complicated. 1671.1Scgd */ 1681.1Scgd for (; cnt < 15; cnt++) { 1691.1Scgd stmt1; 1701.1Scgd stmt2; 1711.1Scgd } 1721.1Scgd 1731.2Scgd /* Second level indents are four spaces. */ 1741.2Scgd while (cnt < 20) 1751.1Scgd z = a + really + long + statment + that + needs + two lines + 1761.1Scgd gets + indented + four + spaces + on + the + second + 1771.7Senami and + subsequent + lines; 1781.1Scgd 1791.1Scgd /* 1801.2Scgd * Closing and opening braces go on the same line as the else. 1811.2Scgd * Don't add braces that aren't necessary. 1821.1Scgd */ 1831.1Scgd if (test) 1841.1Scgd stmt; 1851.1Scgd else if (bar) { 1861.1Scgd stmt; 1871.1Scgd stmt; 1881.1Scgd } else 1891.1Scgd stmt; 1901.1Scgd 1911.2Scgd /* No spaces after function names. */ 1921.1Scgd if (error = function(a1, a2)) 1931.1Scgd exit(error); 1941.1Scgd 1951.1Scgd /* 1961.2Scgd * Unary operators don't require spaces, binary operators do. Don't 1971.2Scgd * use parenthesis unless they're required for precedence, or the 1981.9Slukem * statement is really confusing without them, such as: 1991.9Slukem * a = b->c[0] + ~d == (e || f) || g && h ? i : j >> 1; 2001.1Scgd */ 2011.9Slukem a = ((b->c[0] + ~d == (e || f)) || (g && h)) ? i : (j >> 1); 2021.2Scgd k = !(l & FLAGS); 2031.1Scgd 2041.1Scgd /* 2051.1Scgd * Exits should be 0 on success, and 1 on failure. Don't denote 2061.1Scgd * all the possible exit points, using the integers 1 through 300. 2071.1Scgd */ 2081.1Scgd exit(0); /* Avoid obvious comments such as "Exit 0 on success." */ 2091.1Scgd} 2101.1Scgd 2111.1Scgd/* 2121.8Ssimonb * The function type must be declared on a line by itself 2131.8Ssimonb * preceeding the function. 2141.1Scgd */ 2151.1Scgdstatic char * 2161.2Scgdfunction(a1, a2, fl, a4) 2171.2Scgd int a1, a2, a4; /* Declare ints, too, don't default them. */ 2181.2Scgd float fl; /* List in order declared, as much as possible. */ 2191.1Scgd{ 2201.1Scgd /* 2211.1Scgd * When declaring variables in functions declare them sorted by size, 2221.1Scgd * then in alphabetical order; multiple ones per line are okay. Old 2231.1Scgd * style function declarations can go on the same line. ANSI style 2241.3Scgd * function declarations should go in the include file "extern.h". 2251.1Scgd * If a line overflows reuse the type keyword. 2261.1Scgd * 2271.2Scgd * DO NOT initialize variables in the declarations. 2281.1Scgd */ 2291.1Scgd extern u_char one; 2301.1Scgd extern char two; 2311.1Scgd struct foo three, *four; 2321.1Scgd double five; 2331.1Scgd int *six, seven, eight(); 2341.1Scgd char *nine, ten, eleven, twelve, thirteen, fourteen, fifteen, sixteen; 2351.1Scgd char *overflow __P((void)); 2361.1Scgd void *mymalloc __P((u_int)); 2371.1Scgd 2381.1Scgd /* 2391.1Scgd * Casts and sizeof's are not followed by a space. NULL is any 2401.1Scgd * pointer type, and doesn't need to be cast, so use NULL instead 2411.1Scgd * of (struct foo *)0 or (struct foo *)NULL. Also, test pointers 2421.1Scgd * against NULL, i.e. use: 2431.1Scgd * 2441.1Scgd * (p = f()) == NULL 2451.1Scgd * not: 2461.1Scgd * !(p = f()) 2471.2Scgd * 2481.2Scgd * Don't use '!' for tests unless it's a boolean, e.g. use 2491.2Scgd * "if (*p == '\0')", not "if (!*p)". 2501.1Scgd * 2511.1Scgd * Routines returning void * should not have their return values cast 2521.1Scgd * to any pointer type. 2531.2Scgd * 2541.2Scgd * Use err/warn(3), don't roll your own! 2551.1Scgd */ 2561.1Scgd if ((four = malloc(sizeof(struct foo))) == NULL) 2571.2Scgd err(1, NULL); 2581.1Scgd if ((six = (int *)overflow()) == NULL) 2591.2Scgd errx(1, "Number overflowed."); 2601.1Scgd return (eight); 2611.1Scgd} 2621.1Scgd 2631.2Scgd/* 2641.4Sbriggs * Don't use ANSI function declarations unless you absolutely have to, 2651.2Scgd * i.e. you're declaring functions with variable numbers of arguments. 2661.2Scgd * 2671.2Scgd * ANSI function braces look like regular function braces. 2681.2Scgd */ 2691.9Slukemvoid 2701.1Scgdfunction(int a1, int a2) 2711.1Scgd{ 2721.1Scgd ... 2731.2Scgd} 2741.2Scgd 2751.2Scgd/* Variable numbers of arguments should look like this. */ 2761.2Scgd#if __STDC__ 2771.2Scgd#include <stdarg.h> 2781.2Scgd#else 2791.2Scgd#include <varargs.h> 2801.2Scgd#endif 2811.2Scgd 2821.2Scgdvoid 2831.2Scgd#if __STDC__ 2841.2Scgdvaf(const char *fmt, ...) 2851.2Scgd#else 2861.2Scgdvaf(fmt, va_alist) 2871.2Scgd char *fmt; 2881.2Scgd va_dcl 2891.2Scgd#endif 2901.2Scgd{ 2911.2Scgd va_list ap; 2921.2Scgd#if __STDC__ 2931.2Scgd va_start(ap, fmt); 2941.2Scgd#else 2951.2Scgd va_start(ap); 2961.2Scgd#endif 2971.2Scgd STUFF; 2981.2Scgd 2991.2Scgd va_end(ap); /* No return needed for void functions. */ 3001.1Scgd} 3011.1Scgd 3021.1Scgdstatic void 3031.1Scgdusage() 3041.1Scgd{ /* Insert an empty line if the function has no local variables. */ 3051.1Scgd 3061.1Scgd /* 3071.1Scgd * Use printf(3), not fputs/puts/putchar/whatever, it's faster and 3081.1Scgd * usually cleaner, not to mention avoiding stupid bugs. 3091.1Scgd * 3101.1Scgd * Usage statements should look like the manual pages. Options w/o 3111.1Scgd * operands come first, in alphabetical order inside a single set of 3121.1Scgd * braces. Followed by options with operands, in alphabetical order, 3131.1Scgd * each in braces. Followed by required arguments in the order they 3141.1Scgd * are specified, followed by optional arguments in the order they 3151.1Scgd * are specified. A bar ('|') separates either/or options/arguments, 3161.1Scgd * and multiple options/arguments which are specified together are 3171.1Scgd * placed in a single set of braces. 3181.1Scgd * 3191.1Scgd * "usage: f [-ade] [-b b_arg] [-m m_arg] req1 req2 [opt1 [opt2]]\n" 3201.1Scgd * "usage: f [-a | -b] [-c [-de] [-n number]]\n" 3211.1Scgd */ 3221.1Scgd (void)fprintf(stderr, "usage: f [-ab]\n"); 3231.1Scgd exit(1); 3241.1Scgd} 325