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
Answer from ouah on Stack OverflowI would like to know your opinions on the coding guidelines found in the Linux kernel page.
What I really like:
-
"... align the
switchand its subordinatecaselabels in the same column." -
"It’s a mistake to use
typedeffor structures and pointers." (I do prefer repeatingstructeverywhere).
However I'm curious to what you think about:
-
Maximum size of 80 columns.
-
Using braces when single statements are enough:
if (condition)
do_this();
else
do_that();I'm trying to stick to one coding style and be consistent with it.
I always use braces for single-line if, while and for.
In my opinion, it reads more consistently, and gives me a solid visual indication of where the block ends.
Moreover, I'm often doing things like adding ad-hoc printf (or rather printk in kernel-space) for debugging purposes. Leaving the brackets in leaves me less prone to errors like this:
if (foo)
bar();
printf("%s\n", "bar called");
Linus has come to the conclusion that the 80 character limit is obsolete.
http://lkml.iu.edu/hypermail/linux/kernel/2005.3/08168.html
Linux kernel coding style
Linux kernel coding style
I always use braces for single-line if, while and for.
In my opinion, it reads more consistently, and gives me a solid visual indication of where the block ends.
Moreover, I'm often doing things like adding ad-hoc printf (or rather printk in kernel-space) for debugging purposes. Leaving the brackets in leaves me less prone to errors like this:
if (foo)
bar();
printf("%s\n", "bar called"); More on reddit.com