C99: Suggestions for style(9)

Christoph Mallon christoph.mallon at gmx.de
Sat May 2 07:27:56 UTC 2009


M. Warner Losh schrieb:
> In message: <20090501.081229.1359784281.imp at bsdimp.com>
>             M. Warner Losh <imp at bsdimp.com> writes:
> : In message: <49FA8E88.1040905 at gmx.de>
> :             Christoph Mallon <christoph.mallon at gmx.de> writes:
> : : M. Warner Losh schrieb:
> : : > In message: <20090430233648.GA95360 at keira.kiwi-computer.com>
> : : >             "Rick C. Petty" <rick-freebsd2008 at kiwi-computer.com> writes:
> : : > : On Thu, Apr 30, 2009 at 09:02:26AM -0600, M. Warner Losh wrote:
> : : > : > 
> : : > : > This is the biggest one, and I think it may be too soon.  Also, we
> : : > : > need to be careful on the initialization side of things because we
> : : > : > currently have a lot of code that looks like:
> : : > : > 
> : : > : > 
> : : > : > 	struct foo *fp;
> : : > : > 	struct bar *bp;
> : : > : > 
> : : > : > 	fp = get_foo();
> : : > : > 	if (!fp) return;
> : : > : > 	bp = fp->bp;
> : : > : > 
> : : > : > this can't easily be translated to the more natural:
> : : > : > 
> : : > : > 	struct foo *fp = get_foo();
> : : > : > 	struct bar *bp = fp->bp;
> : : > : > 
> : : > : > since really you'd want to write:
> : : > : > 
> : : > : > 	struct foo *fp = get_foo();
> : : > : > 	if (!fp) return;
> : : > : > 	struct bar *bp = fp->bp;
> : : > : > 
> : : > : > which isn't legal in 'C'.
> : : > : 
> : : > : I thought we were talking about C99, in which case this is perfectly legal.
> : : > : I certainly use it all the time in my C99 code.
> : : > 
> : : > Hmmm, looks like that was added.  That's ugly as C++...
> : : 
> : : I do not think, this is ugly. On the contrary, it aids maintainability, 
> : : because it reduces the scope of variables. Also quite some variables are 
> : : only initialised and not changed afterwards, so it's nice to have the 
> : : declaration and the only assignment in a single place. IMO this is a 
> : : quite natural style, because you don't have anything in the code, before 
> : : it is needed: Get the first pointer; if something is wrong, bail out; 
> : : get the second pointer - the second pointer does not (textually) exist 
> : : before it is needed.
> : 
> : It is ugly.  Hunting for declarations sucks, and it is one of the
> : things I really like about BSD code is that I don't have to.
> : 
> : This is a religious point, and we're dangerously close to saying my
> : religion is better than your religion.  I don't like this part of the
> : proposal at all.  I can see the value in relaxing it for when you have
> : a series of variables that are initialized, but relaxing it to the
> : point where you intermix code and declarations goes way too far.  It
> : is bad enough to have to deal with inner scopes, but tolerable.  It is
> : intolerable to have to look for it anywhere in a big function.  It
> : tends to encourage spaghetti code, which is one of the things that
> : style(9) tries to discourage in many subtle ways.
> 
> This came off a little more absolute than I wanted.  I should have
> added "in the absence of hard data..."

There is hard data: For example look at r190098 and the following 
discussion, which you took part in. Obviously style(9) is too subtle at 
best or counterproductive at worst to achieve the desired goal. It is 
worrisome that somebody is inclined to obfuscate the code (in this case 
replacing multiple variables with descriptive names by a single variable 
with a generic name) because it is less hassle to conform to style(9) 
this way.
I also have to object, that it leads to hunting for declarations. 
Actually it usually reduces scrolling around in the code: Many variables 
are only assigned once. A typical example is getting the softc of a 
device; especially there the type is important, because 
device_get_softc() returns void*. So it is very convenient to have this 
single assignment and its declaration at the same place. Not only the 
type of a variable is important, but also its value, so this assignment 
needs to be looked up, too. Doing declaration and initialisation at once 
you only have to look at one place instead of two. If you use vim as 
editor, then the current way makes it hard to find this assignment: "gd" 
jumps only to the declaration, the assignment is somewhere else. But if 
the declaration and the only assignment are the same, you get both at 
the same place and time.
Even if there are multiple assignments, the first one often is a point 
of interest, e.g. int found = 0; followed by a search loop. Of course, 
if you abuse a variable in multiple contexts, then there are multiple 
"first" assignments and this coupling of declaration and first 
assignment gets lost. Therefore I am against re-using a variable in 
multiple contexts.
In conclusion, I argue that declaring a variable right at its first (and 
often only) assignment does not lead to "spaghetti code" or "sloppy 
code", but in contrary makes the code more concise and easier to navigate.

	Christoph


More information about the freebsd-hackers mailing list