style revision 1.7
11.7Senami/* $NetBSD: style,v 1.7 1998/02/09 06:58:39 enami 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.1Scgd */ 81.1Scgd 91.1Scgd/* 101.1Scgd * VERY important single-line comments look like this. 111.1Scgd */ 121.1Scgd 131.1Scgd/* Most single-line comments look like this. */ 141.1Scgd 151.1Scgd/* 161.1Scgd * Multi-line comments look like this. Make them real sentences. Fill 171.1Scgd * them so they look like real paragraphs. 181.1Scgd */ 191.1Scgd 201.2Scgd/* 211.2Scgd * Kernel include files come first; normally, you'll need <sys/types.h> 221.2Scgd * OR <sys/param.h>, but not both! <sys/types.h> includes <sys/cdefs.h>, 231.2Scgd * and it's okay to depend on that. 241.2Scgd */ 251.2Scgd#include <sys/types.h> /* Non-local includes in brackets. */ 261.2Scgd 271.2Scgd/* If it's a network program, put the network include files next. */ 281.2Scgd#include <net/if.h> 291.2Scgd#include <net/if_dl.h> 301.2Scgd#include <net/route.h> 311.2Scgd#include <netinet/in.h> 321.2Scgd#include <protocols/rwhod.h> 331.2Scgd 341.2Scgd/* 351.2Scgd * Then there's a blank line, followed by the /usr include files. 361.2Scgd * The /usr include files should be sorted! 371.2Scgd */ 381.2Scgd#include <stdio.h> 391.1Scgd 401.1Scgd/* 411.1Scgd * Global pathnames are defined in /usr/include/paths.h. Pathnames local 421.1Scgd * to the program go in pathnames.h in the local directory. 431.1Scgd */ 441.2Scgd#include <paths.h> 451.2Scgd 461.2Scgd/* Then, there's a blank line, and the user include files. */ 471.2Scgd#include "pathnames.h" /* Local includes in double quotes. */ 481.1Scgd 491.1Scgd/* 501.2Scgd * ANSI function declarations for private functions (i.e. functions not used 511.2Scgd * elsewhere) go at the top of the source module. Use the __P macro from 521.2Scgd * the include file <sys/cdefs.h>. Only the kernel has a name associated with 531.2Scgd * the types, i.e. in the kernel use: 541.1Scgd * 551.1Scgd * void function __P((int a)); 561.1Scgd * 571.1Scgd * in user land use: 581.1Scgd * 591.1Scgd * void function __P((int)); 601.1Scgd */ 611.2Scgdstatic char *function __P((int, const char *)); 621.2Scgdstatic void usage __P((void)); 631.1Scgd 641.1Scgd/* 651.1Scgd * Macros are capitalized, parenthesized, and should avoid side-effects. 661.1Scgd * If they are an inline expansion of a function, the function is defined 671.1Scgd * all in lowercase, the macro has the same name all in uppercase. If the 681.2Scgd * macro needs more than a single line, use braces. Right-justify the 691.2Scgd * backslashes, it makes it easier to read. 701.1Scgd */ 711.2Scgd#define MACRO(x, y) { \ 721.2Scgd variable = (x) + (y); \ 731.2Scgd (y) += 2; \ 741.1Scgd} 751.1Scgd 761.1Scgd/* Enum types are capitalized. */ 771.1Scgdenum enumtype { ONE, TWO } et; 781.1Scgd 791.1Scgd/* 801.1Scgd * When declaring variables in structures, declare them sorted by use, then 811.1Scgd * by size, and then by alphabetical order. The first category normally 821.1Scgd * doesn't apply, but there are exceptions. Each one gets its own line. 831.1Scgd * Put a tab after the first word, i.e. use "int^Ix;" and "struct^Ifoo *x;". 841.1Scgd * 851.2Scgd * Major structures should be declared at the top of the file in which they 861.2Scgd * are used, or in separate header files, if they are used in multiple 871.2Scgd * source files. Use of the structures should be by separate declarations 881.1Scgd * and should be "extern" if they are declared in a header file. 891.1Scgd */ 901.1Scgdstruct foo { 911.1Scgd struct foo *next; /* List of active foo */ 921.1Scgd struct mumble amumble; /* Comment for mumble */ 931.1Scgd int bar; 941.1Scgd}; 951.1Scgdstruct foo *foohead; /* Head of global foo list */ 961.2Scgd 971.2Scgd/* Make the structure name match the typedef. */ 981.2Scgdtypedef struct _bar { 991.2Scgd int level; 1001.2Scgd} BAR; 1011.1Scgd 1021.1Scgd/* 1031.1Scgd * All major routines should have a comment briefly describing what 1041.2Scgd * they do. The comment before the "main" routine should describe 1051.1Scgd * what the program does. 1061.1Scgd */ 1071.2Scgdint 1081.1Scgdmain(argc, argv) 1091.1Scgd int argc; 1101.1Scgd char *argv[]; 1111.1Scgd{ 1121.1Scgd extern char *optarg; 1131.1Scgd extern int optind; 1141.1Scgd long num; 1151.1Scgd int ch; 1161.1Scgd char *ep; 1171.1Scgd 1181.1Scgd /* 1191.2Scgd * For consistency, getopt should be used to parse options. Options 1201.2Scgd * should be sorted in the getopt call and the switch statement, unless 1211.2Scgd * parts of the switch cascade. Elements in a switch statement that 1221.2Scgd * cascade should have a FALLTHROUGH comment. Numerical arguments 1231.2Scgd * should be checked for accuracy. Code that cannot be reached should 1241.2Scgd * have a NOTREACHED comment. 1251.1Scgd */ 1261.5Sscottr while ((ch = getopt(argc, argv, "abn")) != -1) 1271.1Scgd switch (ch) { /* Indent the switch. */ 1281.1Scgd case 'a': /* Don't indent the case. */ 1291.1Scgd aflag = 1; 1301.1Scgd /* FALLTHROUGH */ 1311.1Scgd case 'b': 1321.1Scgd bflag = 1; 1331.1Scgd break; 1341.1Scgd case 'n': 1351.1Scgd num = strtol(optarg, &ep, 10); 1361.2Scgd if (num <= 0 || *ep != '\0') 1371.1Scgd err("illegal number -- %s", optarg); 1381.1Scgd break; 1391.1Scgd case '?': 1401.1Scgd default: 1411.1Scgd usage(); 1421.2Scgd /* NOTREACHED */ 1431.1Scgd } 1441.1Scgd argc -= optind; 1451.1Scgd argv += optind; 1461.1Scgd 1471.1Scgd /* 1481.1Scgd * Space after keywords (while, for, return, switch). No braces are 1491.2Scgd * used for control statements with zero or only a single statement. 1501.1Scgd * 1511.1Scgd * Forever loops are done with for's, not while's. 1521.1Scgd */ 1531.2Scgd for (p = buf; *p != '\0'; ++p); 1541.1Scgd for (;;) 1551.1Scgd stmt; 1561.1Scgd 1571.1Scgd /* 1581.2Scgd * Parts of a for loop may be left empty. Don't put declarations 1591.2Scgd * inside blocks unless the routine is unusually complicated. 1601.1Scgd */ 1611.1Scgd for (; cnt < 15; cnt++) { 1621.1Scgd stmt1; 1631.1Scgd stmt2; 1641.1Scgd } 1651.1Scgd 1661.2Scgd /* Second level indents are four spaces. */ 1671.2Scgd while (cnt < 20) 1681.1Scgd z = a + really + long + statment + that + needs + two lines + 1691.1Scgd gets + indented + four + spaces + on + the + second + 1701.7Senami and + subsequent + lines; 1711.1Scgd 1721.1Scgd /* 1731.2Scgd * Closing and opening braces go on the same line as the else. 1741.2Scgd * Don't add braces that aren't necessary. 1751.1Scgd */ 1761.1Scgd if (test) 1771.1Scgd stmt; 1781.1Scgd else if (bar) { 1791.1Scgd stmt; 1801.1Scgd stmt; 1811.1Scgd } else 1821.1Scgd stmt; 1831.1Scgd 1841.2Scgd /* No spaces after function names. */ 1851.1Scgd if (error = function(a1, a2)) 1861.1Scgd exit(error); 1871.1Scgd 1881.1Scgd /* 1891.2Scgd * Unary operators don't require spaces, binary operators do. Don't 1901.2Scgd * use parenthesis unless they're required for precedence, or the 1911.2Scgd * statement is really confusing without them. 1921.1Scgd */ 1931.1Scgd a = b->c[0] + ~d == (e || f) || g && h ? i : j >> 1; 1941.2Scgd k = !(l & FLAGS); 1951.1Scgd 1961.1Scgd /* 1971.1Scgd * Exits should be 0 on success, and 1 on failure. Don't denote 1981.1Scgd * all the possible exit points, using the integers 1 through 300. 1991.1Scgd */ 2001.1Scgd exit(0); /* Avoid obvious comments such as "Exit 0 on success." */ 2011.1Scgd} 2021.1Scgd 2031.1Scgd/* 2041.1Scgd * If a function type is declared, it should be on a line 2051.1Scgd * by itself preceeding the function. 2061.1Scgd */ 2071.1Scgdstatic char * 2081.2Scgdfunction(a1, a2, fl, a4) 2091.2Scgd int a1, a2, a4; /* Declare ints, too, don't default them. */ 2101.2Scgd float fl; /* List in order declared, as much as possible. */ 2111.1Scgd{ 2121.1Scgd /* 2131.1Scgd * When declaring variables in functions declare them sorted by size, 2141.1Scgd * then in alphabetical order; multiple ones per line are okay. Old 2151.1Scgd * style function declarations can go on the same line. ANSI style 2161.3Scgd * function declarations should go in the include file "extern.h". 2171.1Scgd * If a line overflows reuse the type keyword. 2181.1Scgd * 2191.2Scgd * DO NOT initialize variables in the declarations. 2201.1Scgd */ 2211.1Scgd extern u_char one; 2221.1Scgd extern char two; 2231.1Scgd struct foo three, *four; 2241.1Scgd double five; 2251.1Scgd int *six, seven, eight(); 2261.1Scgd char *nine, ten, eleven, twelve, thirteen, fourteen, fifteen, sixteen; 2271.1Scgd char *overflow __P((void)); 2281.1Scgd void *mymalloc __P((u_int)); 2291.1Scgd 2301.1Scgd /* 2311.1Scgd * Casts and sizeof's are not followed by a space. NULL is any 2321.1Scgd * pointer type, and doesn't need to be cast, so use NULL instead 2331.1Scgd * of (struct foo *)0 or (struct foo *)NULL. Also, test pointers 2341.1Scgd * against NULL, i.e. use: 2351.1Scgd * 2361.1Scgd * (p = f()) == NULL 2371.1Scgd * not: 2381.1Scgd * !(p = f()) 2391.2Scgd * 2401.2Scgd * Don't use '!' for tests unless it's a boolean, e.g. use 2411.2Scgd * "if (*p == '\0')", not "if (!*p)". 2421.1Scgd * 2431.1Scgd * Routines returning void * should not have their return values cast 2441.1Scgd * to any pointer type. 2451.2Scgd * 2461.2Scgd * Use err/warn(3), don't roll your own! 2471.1Scgd */ 2481.1Scgd if ((four = malloc(sizeof(struct foo))) == NULL) 2491.2Scgd err(1, NULL); 2501.1Scgd if ((six = (int *)overflow()) == NULL) 2511.2Scgd errx(1, "Number overflowed."); 2521.1Scgd return (eight); 2531.1Scgd} 2541.1Scgd 2551.2Scgd/* 2561.4Sbriggs * Don't use ANSI function declarations unless you absolutely have to, 2571.2Scgd * i.e. you're declaring functions with variable numbers of arguments. 2581.2Scgd * 2591.2Scgd * ANSI function braces look like regular function braces. 2601.2Scgd */ 2611.1Scgdfunction(int a1, int a2) 2621.1Scgd{ 2631.1Scgd ... 2641.2Scgd} 2651.2Scgd 2661.2Scgd/* Variable numbers of arguments should look like this. */ 2671.2Scgd#if __STDC__ 2681.2Scgd#include <stdarg.h> 2691.2Scgd#else 2701.2Scgd#include <varargs.h> 2711.2Scgd#endif 2721.2Scgd 2731.2Scgdvoid 2741.2Scgd#if __STDC__ 2751.2Scgdvaf(const char *fmt, ...) 2761.2Scgd#else 2771.2Scgdvaf(fmt, va_alist) 2781.2Scgd char *fmt; 2791.2Scgd va_dcl 2801.2Scgd#endif 2811.2Scgd{ 2821.2Scgd va_list ap; 2831.2Scgd#if __STDC__ 2841.2Scgd va_start(ap, fmt); 2851.2Scgd#else 2861.2Scgd va_start(ap); 2871.2Scgd#endif 2881.2Scgd STUFF; 2891.2Scgd 2901.2Scgd va_end(ap); /* No return needed for void functions. */ 2911.1Scgd} 2921.1Scgd 2931.1Scgdstatic void 2941.1Scgdusage() 2951.1Scgd{ /* Insert an empty line if the function has no local variables. */ 2961.1Scgd 2971.1Scgd /* 2981.1Scgd * Use printf(3), not fputs/puts/putchar/whatever, it's faster and 2991.1Scgd * usually cleaner, not to mention avoiding stupid bugs. 3001.1Scgd * 3011.1Scgd * Usage statements should look like the manual pages. Options w/o 3021.1Scgd * operands come first, in alphabetical order inside a single set of 3031.1Scgd * braces. Followed by options with operands, in alphabetical order, 3041.1Scgd * each in braces. Followed by required arguments in the order they 3051.1Scgd * are specified, followed by optional arguments in the order they 3061.1Scgd * are specified. A bar ('|') separates either/or options/arguments, 3071.1Scgd * and multiple options/arguments which are specified together are 3081.1Scgd * placed in a single set of braces. 3091.1Scgd * 3101.1Scgd * "usage: f [-ade] [-b b_arg] [-m m_arg] req1 req2 [opt1 [opt2]]\n" 3111.1Scgd * "usage: f [-a | -b] [-c [-de] [-n number]]\n" 3121.1Scgd */ 3131.1Scgd (void)fprintf(stderr, "usage: f [-ab]\n"); 3141.1Scgd exit(1); 3151.1Scgd} 316