Indenting… why not use 3-space indents?

Anyone would think I were interested in indenting. Well, it is the kind of thing many people ignore. I use to be very much a automaton of 4 spaces… but in recent years (maybe the wisdom acquired with age?), I have begun to explore the realms of computer science literature in the 1960s and 70s, and have realized the wealth of knowledge many have forsaken. I recently wrote an article on the origins of indenting. In 1980 Dennis Leinbaugh wrote a paper titled “Indenting for the Compiler“. In the opening sentence of the abstract he wrote:

A simple indentation rule, indent statements from the control statement they belong to, is sufficient to express a program’s block structure without the use of compound statements or closing keywords.

Which really means that indenting wasn’t that well known. It likely all began with formatting Algol programs in the 1960s, yet wasn’t explicitly discussed until Pascal programs in the late 1970s. Leinbaugh cites an indentation rule:

“all statements directly belonging to  control statement are right indented an equal amount from the beginning of the control statement”

A rule which is most commonly used to this day. But one has to wonder if some of the ideas of indenting rules we use today are useful. I have talked about different aspects of writing control structures on single lines before, and 2 space indenting. I’m almost willing to advocate for 3 space indenting as a form of compromise between 2 and 4. So why not combine these concepts?

Here is a piece of C code that prints out 3-digit Armstrong numbers:

#include <stdio.h>

int main(void)
{
    int i, j, k, arm;

    printf("The following are 3-digit Armstrong numbers:\n");
 
    for (i=1; i<=9; i=i+1)
        for (j=0; j<=9; j=j+1)
            for (k=0; k<=9; k=k+1)
            {
                arm = i*i*i + j*j*j + k*k*k;
                if (arm == i*100+j*10+k)
                    printf("%d ", arm);
            }

    return 0;
}

Now here is the code with modified style:

#include <stdio.h>

int main(void)
{
   int i, j, k, arm;

   printf("The following are 3-digit Armstrong numbers:\n");
 
   for (i=1; i<=9; i=i+1)
      for (j=0; j<=9; j=j+1)
         for (k=0; k<=9; k=k+1){
            arm = i*i*i + j*j*j + k*k*k;
            if (arm == i*100+j*10+k) printf("%d ", arm);
         }
   return 0;
}

The changes are subtle, but do they make for a more readable piece of code? It’s amazing what a difference one less space per indent makes. It also makes a single line out of an if clause which only has one statement.

Refs:
Leinbaugh, D.W., “Indenting for the compiler”, ACM SIGPLAN Notices, 15(5), pp.41-48 (1980)
Crider, J.E., “Structured formatting of Pascal programs”, ACM SIGPLAN Notices, 13(11), pp.15-22 (1978)
Gustafson, G.G., “Some practical experiences formatting Pascal programs”, ACM SIGPLAN Notices, 14(9), pp.42-49 (1979)
Peterson, J.L., “On formatting Pascal programs”, ACM SIGPLAN Notices, 12(12), pp.74-75 (1979)

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s