Blog: The GNU Coding Style (2014-12-01)

Over the past years, I've been involved in various GNU projects. In particular, gfortran, Guile and Octave. (You might have noticed that I'm seemingly interested in programming languages, compilers and interpreters.) While I'm mostly on to other things now, something stayed from that time: The GNU indentation style. I like it, and have even defended it on the Octave mailing list when someone (not affiliated with Octave, of course) was sceptical.

In this post, I would like to make this point again. Before I understood the GNU style, I followed what Wikipedia calls the Allman style. It looks like this:

if (some condition) shortCode (); if (something else) { doSomething (); oneMoreThing (); }

In comparison, the GNU style looks like this:

if (some condition) shortCode (); if (something else) { doSomething (); oneMoreThing (); }

Granted, when you first see this style, it looks weird. However, let me explain a little more: The core thing to understand about its philosophy is that blocks are a special kind of statement in the C (and others) programming language. (Please excuse me if I got the official terms used in language design or the standard wrong, but I hope it is clear what I mean.) In particular, it doesn't really matter if you have a statement like

shortCode ();

or like:

{ doSomething (); oneMoreThing (); }

The former is just a single line, while the latter consists of multiple lines. But both of them are valid as, for instance, the body of a conditional or loop. The GNU style, now, doesn't specify different indentations for single- and multi-line bodies of constructs. Instead, there's just a single rule:

Nested bodies of constructs are indented by a certain amount of white space (two spaces, to be precise).

It is crucial to understand that this applies both to blocks as a construct (which gives the indentation shown above), and to blocks when they are nested bodies themselves! Maybe this needs some time to sink in—but when you fully grasp the concept, it should be clear as day why this single rule leads to the GNU indentation style, and that its simplicity makes it the most consistent and elegant indentation style possible. At least, that's my opinion.

Copyright © 2011–2019 by Daniel KraftHomeContactImprint