Relax the rule against declaring variables in nested scopes and for

initializations.

Relax some overly perscriptive rules against declarations: they may be at the
start of any blocks, even if things aren't super complicated. Allow more
initializations (those that call simple functions, like accessor functions for
newbus are fine). Allow the common idiom of declaring the loop variable in a for
loop.

This tries to codify what common exceptions are today, as well as give
some guidance on when it's best to do these things.

Reviewed by: tsoome, kp, markm, allanjude, jiles, cem, rpokala
	(earlier versions: seanc, melifaro, bapt, pjd, bz, pstef, arichards,
	 jhibits, vangyzen, jmallet, ian, glebius, jhb, dab, adrian,
	 sef, gnn)
Differential Revision: https://reviews.freebsd.org/D25312
This commit is contained in:
Warner Losh 2020-07-16 14:12:54 +00:00
parent 3ea3fbe685
commit 34a8e7368f

View File

@ -25,7 +25,7 @@
.\" From: @(#)style 1.14 (Berkeley) 4/28/95 .\" From: @(#)style 1.14 (Berkeley) 4/28/95
.\" $FreeBSD$ .\" $FreeBSD$
.\" .\"
.Dd June 30, 2020 .Dd July 16, 2020
.Dt STYLE 9 .Dt STYLE 9
.Os .Os
.Sh NAME .Sh NAME
@ -592,8 +592,6 @@ not
Parts of a Parts of a
.Ic for .Ic for
loop may be left empty. loop may be left empty.
Do not put declarations
inside blocks unless the routine is unusually complicated.
.Bd -literal .Bd -literal
for (; cnt < 15; cnt++) { for (; cnt < 15; cnt++) {
stmt1; stmt1;
@ -601,6 +599,15 @@ inside blocks unless the routine is unusually complicated.
} }
.Ed .Ed
.Pp .Pp
A
.Ic for
loop may declare and initialize its counting variable.
.Bd -literal
for (int i = 0; i < 15; i++) {
stmt1;
}
.Ed
.Pp
Indentation is an 8 character tab. Indentation is an 8 character tab.
Second level indents are four spaces. Second level indents are four spaces.
If you have to wrap a long statement, put the operator at the end of the If you have to wrap a long statement, put the operator at the end of the
@ -676,25 +683,26 @@ The opening brace of the function body should be
on a line by itself. on a line by itself.
.Bd -literal .Bd -literal
static char * static char *
function(int a1, int a2, float fl, int a4) function(int a1, int a2, float fl, int a4, struct bar *bar)
{ {
.Ed .Ed
.Pp .Pp
When declaring variables in functions declare them sorted by size, When declaring variables in functions declare them sorted by size,
then in alphabetical order; multiple ones per line are okay. then in alphabetical order; multiple ones per line are okay.
If a line overflows reuse the type keyword. If a line overflows reuse the type keyword.
.Pp Variables may be initialized where declared especially when they
Be careful to not obfuscate the code by initializing variables in are constant for the rest of the scope.
the declarations. Declarations may be placed before executable lines at the start
Use this feature only thoughtfully. of any block.
DO NOT use function calls in initializers. Calls to complicated functions should be avoided when initializing variables.
.Bd -literal .Bd -literal
struct foo one, *two; struct foo one, *two;
double three; struct baz *three = bar_get_baz(bar);
int *four, five; double four;
char *six, seven, eight, nine, ten, eleven, twelve; int *five, six;
char *seven, eight, nine, ten, eleven, twelve;
four = myfunction(); four = my_complicated_function(a1, f1, a4);
.Ed .Ed
.Pp .Pp
Do not declare functions inside other functions; ANSI C says that Do not declare functions inside other functions; ANSI C says that