top | item 8662542

OpenBSD kernel source file style guide

112 points| zytek | 11 years ago |openbsd.org | reply

46 comments

order
[+] bjackman|11 years ago|reply
I've never written code with it, but the BSDs use these macros to implement rudimentary generic types in C: http://www.openbsd.org/cgi-bin/man.cgi/OpenBSD-current/man3/.... Nice.

However, I've only had horrible experiences trying to read the BSDs' kernel code. There are way too many statements like "mst_fqd->f_do_skb((struct mfq_t *) q);"

[+] jpfr|11 years ago|reply
Yes! That's a really good implementations of linked lists using only preprocessor macros.

sys/queue.h is also provided in all Linux systems.

Easy to use, type-checked by the compiler and a lot faster than a "generic" linked list with pointers to the data. Here, the next/previous pointers get embedded in the payload struct itself.

[+] feld|11 years ago|reply
Linux's is very long in comparison to the BSD's. It seems to have weird edge-cases and possibly unnecessary explanations.

https://www.kernel.org/doc/Documentation/CodingStyle

Example: Why is the comment style different in net/? It seems to serve no obvious purpose.

Too many cooks :-)

[+] Alupis|11 years ago|reply
Likely because /net was done before the coding standard and by some 3rd party contributor -- and doing non-functional whitespace/formatting commits is a no-no in most large projects.
[+] gcb0|11 years ago|reply
openbsd is the only project which manpages are not mostly useless. You have even a starting guide there.
[+] dredmorbius|11 years ago|reply
FreeBSD's manpages are also quite excellent. I haven't had to refer to NetBSD's but I'd suspect it's also good.

On the Linux side, Debian tends to support manpages better than others (they're required by Policy, though they're not release-critical).

[+] ja30278|11 years ago|reply
I never understand the desire to omit braces in single line blocks.

sure

   for ()
       foo
saves you a line, but it's a bug waiting to happen.
[+] oneeyedpigeon|11 years ago|reply
Are there not examples where a statement is pretty much guaranteed to live on its own within a block? Having said that, my own style is to always include braces, for aesthetic reasons if nothing else.
[+] ams6110|11 years ago|reply
When I omit braces I also omit the indented line. I would write:

    for () foo;
Makes it clear it's ONE statement. At least for me...
[+] lmm|11 years ago|reply
A function or class that doesn't fit on a single screen is a bug waiting to happen.
[+] DrJokepu|11 years ago|reply
People keep saying that, but do bugs like that really happen? In my entire career as a professional software developer I've never seen it happening. Not once.
[+] mheiler|11 years ago|reply
I like that it's short.
[+] bigfoot|11 years ago|reply
Archaic and impractical. Example: Instead of using Linux' pragmatic approach to function prototypes:

"In function prototypes, include parameter names with their data types. Although this is not required by the C language, it is preferred in Linux because it is a simple way to add valuable information for the reader."

OpenBSD enforces this:

"Prototypes should not have variable names associated with the types; i.e., void function(int); not: void function(int a);"

Instead of letting the code tell the parameters' purposes, this now has to be deduced from informal descriptions, or the function definition in some .c file.

[+] imanaccount247|11 years ago|reply
I love how frequently people on HN decry actual real working things people actually work on and use as "impractical". I suspect the combined experience of the entire openbsd team is greater than yours.
[+] 0x0|11 years ago|reply
What would be a reason for leaving out the parameter names? Genuinely curious.
[+] JelteF|11 years ago|reply
This is the first time I've heard of a style guide that demands a mixture of tabs and spaces. I thought this was very much frowned upon.
[+] Negitivefrags|11 years ago|reply
Tabs for indentation, spaces for alignment. This is the only sane standard.
[+] fredmorcos|11 years ago|reply
Here's a question, since the man page mentions splint. Has anyone here ever managed to reach a splint warning-free project?