• About
  • Overused Words/Phrases
  • Projects

Jonathan's Ramblings

Jonathan's thoughts about just about anything

style

Linux Coding Standard

2012-09-21 by Jonathan Leave a Comment

I have been looking at a few coding standards recently and for the most part, I am happy with the Linux kernel coding style (http://www.kernel.org/doc/Documentation/CodingStyle).  It has a very nice description of how to code everything in a terse but clear way which is something a lot of other guides lack.  There are two items with it though that I definitely do not agree with: the indentation for switch statements and the use of braces in if/else statements.

For switch statements, the coding style recommends having the case statements at the same indentation level as the switch statement (see below)

switch (suffix) {
case a:
	break;
default:
	break;
}

In my opinion, this is hard to read and the case statements should all be indented by another level (see below)

switch (suffix) {
	case a:
		break;
	default:
		break;
}

The style also specifies that curly braces should not be used with conditional statements unless one branch consists of multiple statements.  I completely agree with this concept for a single if statement, however, I think it looks a lot cleaner to use braces if there are multiple branches in the if statement.  This does not add a significant amount of white space (only a single line thanks to the inline braces), gives a cleaner separation, and prevents people from stupidly adding an extra statement they think is in a branch but really is not.  The change I am proposing is below.

if (cond)
	action();
else
	other_action();

would become

if (cond) {
	action();
} else {
	other_action();
}

With these two changes (and adaptations for things like classes and other non-C language features), I will be using the Linux Kernel Coding Style as my personal coding style.  There are times when I do things wrong (for example, I often do not put spaces around “*” and “/” just because my Engineering 101 professor instructed us not to) but overall this is a nice sound style guide and one I will be happy trying to follow.

Posted in: Computer Science Tagged: style

Recent Posts

  • The dog I needed
  • Recycling some old electronics
  • Rent and La Boheme
  • I’m using Twitter now!
  • Operating Systems and Food Trucks

Categories

  • Circle K
  • Computer Science
  • Dogs
  • Flying
  • Kiwanis Family
  • Meta
  • Parliamentary Procedure
  • Socializing
  • Technology
  • Theatre
  • Uncategorized
  • Websites

  • Mastodon
  • Twitter
  • Goodreads

Copyright © 2025 Jonathan's Ramblings.

Omega WordPress Theme by ThemeHall