• 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

  • Recycling some old electronics
  • Rent and La Boheme
  • I’m using Twitter now!
  • Operating Systems and Food Trucks
  • The Meaning of a Nomination

Categories

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

Archives

  • February 2017
  • November 2015
  • September 2014
  • August 2014
  • May 2014
  • April 2014
  • March 2013
  • February 2013
  • January 2013
  • October 2012
  • September 2012
  • July 2012

Recent Comments

  • Jeff Sipek on Recycling some old electronics
  • Jonathan on Recycling some old electronics
  • Jonathan on Operating Systems and Food Trucks
  • Josef "Jeff" Sipek on Operating Systems and Food Trucks
  • Denise aka Mom on Purpose

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org

Copyright © 2022 Jonathan's Ramblings.

Omega WordPress Theme by ThemeHall