Old Post - A dance of K&R and ANSI

Old Post - A dance of K&R and ANSI

A question: Style and control blocks

Hello everyone! Today I just wanted to put up a short post about something I was thinking about in work. So in my day to day life I work with embedded C, but previously have worked in Java and at home I'll work with all sorts. So going between different languages you'll see different opinions on which way your braces should be laid out. in C++ for example the norm for a function would be:

void functionName ()
{
    if (something)
    {
        printf("Hello World!\n");
    }
}

So you will newline your braces. This style will usually be called the ANSI indentation style, although a quick search will show you that this isn't technically correct. Often it is called ANSI as it was thought the ANSI C standard (later to become ISO C) was written with examples using this style of indentation, however the standard was actually written with K&R (explained later). To be technically correct this should be referred to as Allman style, as it was popularized by Eric Allman, who was the creator of sendmail, as well as other BSD utilities. Sendmail was a simple email delivery system used in the ARPANET (the predecessor to the internet!). Anyway, its pretty common to see this in larger projects and often will be used in C language variants, it can often be considered advantageous in large scale projects where mapping functions/control blocks can become difficult.

A bit of a tangent there, but back to the discussion of brace/indentation style! In Java you will generally see braces laid out something like below:

// This is pure K&R!
public void functionName () 
{
    if (something) {
      System.out.println("Hello World!");
      System.out.println("Statements!"):
    }
}
    
/* 
 * It should be noted functions in Java should be defined using 
 * the Sun variant of K&R, which does not newline function 
 * braces like the below example.
 */
public void functionNameTwo () {
    ...
}

This style is called K&R (Kernighan and Ritchie) indent style, you can find out more about this on wikipedia, essentially K&R had a very deep impact on the C programming language, Dennis Ritchie being an extremely important figure in computing, having implemented C as well as co-designing the unix operating system (pretty awesome guy!). Anyway, the book had a profound effect on coding style, here's a little quote from wikipedia:

The influence of The C Programming Language on programmers (book), a generation of whom first worked with C in universities and industry, has led many to accept the authors' programming style and conventions as recommended practice, if not normative practice. For example, the coding and formatting style of the programs presented in both editions of the book is often referred to as "K&R style" or the "One True Brace Style" and, significantly, became the coding style used by convention in the source code for the Unix and Linux kernels.

Pretty interesting!
its common in most languages based on the JVM or that are Javascript like. The K&R style is actually quite important in Javascript, as some functions don't act as expected when using Allman, essentially automatic semicolon insertion will mess you up, just two quick examples:

// Allman Style, newline everthing
function functionNameBreaks ()
{
    return // at this point a perceived semicolon will be added
    {   
        javascript : "oh dear!"
    };
}
  
// K&R (well the Sun variant, with same line brace functions)
function functionNameWorks () {
    return {  // this brace will mean no semicolon inserted, therefore it works!
        javascript : "this works!"
    };
}

The comments I've added explain this, but essentially using K&R will protect you from automatic semicolon insertion! So in JS theres definitely a reason to use K&R over Allman.

Quick note on variants

I realise there are many variants available out there and I haven't covered anything like BSD KNF, OTBS, Whitesmiths, Horstmann or GNU indentation/brace styles, but I feel that all of these draw from the two major styles, Allman and K&R. However I'm willing to accept i'm wrong, thats just my experience of codebases being heavily Allman or K&R!

A conclusion on styling

So just to conclude that bit on styling, I'd generally go for K&R over Allman, although there's definitely very strong points for Allman, but I like how clean K&R looks. If you're interested in the K&R vs Allman argument (there are other styles, but they are mostly variants) then google about and you'll find a fair bit. Just before I move on I should say I like Allman for large projects (especially in C) as I think it can make large functions/control structures easier to read, as well as separating multi-line function parameter definitions from the actual function implementation.

Oh also, before I pose my question, I should say I don't really mind what style is used throughout a code base, I think K&R's book 'The C Programming Language' describes this best:

The position of braces is less important, although people hold passionate beliefs. We have chosen one of several popular styles. Pick a style that suits you, then use it consistently.

The only thing that matters is that the style is defined and EVERYONE uses it. This is important, if you're ever working on a project and it hasn't been defined check with someone! If there isn't a standard I would definitely put forward using a standard style or putting a style guide together for the project. This can be really helpful in the future, or when people join the project. Its also useful when using linux tools (like grep or sed), standard styling can make the process a whole lot easier.

The question!

So we've gotten to here... This wasn't as short a post as I thought it would be but anyway! I'm not going to say too much here as I'm hoping for user interaction! Brace style is important, but there's a subject that often isn't taken into account in style guides, this is single line control structures!

Here's three examples, I realise that many of these fall into the parameters of the above styles, such as OTBS setting how control structures work, but i'm just interested to get a discussion going. It'd be cool to hear your comments on each one and you're reasoning for favouring that style! I'm using if statements, but any other simple control structure with one statement within would follow the same style. Anyway, here they are (I'm going to do them in Java because why not):

// Example 1: No brace control structure (K&R)
if (something)
    functionOne();
else 
    functionTwo();
        
// Example 2: braces around all control structures (OTBS)
if (something) {
    functionOne();
} else {
    functionTwo();
}
    
// Example 3: I've never seen this one in practice, but threw it in anyway!
if (something) { functionOne(); } else { functionTwo(); }

I'm interested to hear your thoughts on each! I understand that a lot of this is specific to the language the project is being written in, and whether K&R style or Allman (or a variant) is already being used, but i'm more interested as to where you might use example 1 or 3. I would tend to be against example 1, as you can introduce dangers (see this blog post on Apple's mistake due to this Apple's SSL bug). However I think that the OTBS (example 2) version of simple control structures with braces can take up far more space than is necessary, and tend to look quite messy.

So please leave a comment! I'm interested to hear your comments on simple control structures, and if there is a style i've missed! So comment away, I like to have my thoughts on coding style challenged!

Michael Drury

View Comments
Navigation