Search code examples
clinuxcoding-stylelinux-kernel

Linux Kernel coding style


I am new in the kernel-programming, so I would like to find out what coding style is more acceptable. For example, in case of the error handling which of the following is better?

This one:

/* some stuff */
if(error) {
    /* error handling */
    return -(errorcode);
}
/* normal actions */

or this one:

/* some stuff */
if(!error) {
    /* normal actions */
} else {
    /* error handling */
    return -(errorcode);
}

Where can I find any document, that regards to kernel coding standard?


Solution

  • Linux kernel has a coding style guide:

    https://www.kernel.org/doc/Documentation/process/coding-style.rst

    Nicer Formatted Version

    Regarding your example, I personally prefer the first style. With the second style you will quickly violate this Linux kernel style rule (kernel style has 8-character indentation):

    if you need more than 3 levels of indentation, you're screwed anyway, and should fix your program.

    Writing code from top to bottom (as opposed to horizontally) is sometimes referred as duffing. I can suggest you this excellent reading on the subject:

    Reading Code From Top to Bottom