Code beautification nightmare

hacker_compProgrammers develop unique styles of coding over years. While some of those might actually be useful, there are some widespread styles around code beautification which range from being redundant to problematic. I will keep the examples in this article limited to C but they may apply to other languages too.

Variable declarations

Here’s a very common example of variable declarations with ridiculous alignment:

int     i;
char    c;
float   f;

The developer who did this believes the indentation increases readability.

Let’s say the following variable is to be added at a later point of time:

static struct my_struct* ptr;

How can this new variable comply to the existing style? If the same developer works on it, he may as well try:

int                             i;
char                            c;
float                           f;
static struct my_struct*        ptr;

pushing stuff to the limit of madness.

To keep things saner, try the following style:

int i;
char c;
float f;
static struct my_struct* ptr;

Yes, a single space after the variable type, nothing more, nothing less. This also applies to the following (less used, but existing) pattern:

var1    = 1;
myvar1  = 2;
count   = 3;

License notice or comments

Devs with a lot of time come up with asterick-bound rectangles to add license notices or somewhat less frequently, comments:

/**************************************
 * This design was done by shitty dev *
 * with a lot of time and zero work   *
 **************************************/

In real life, licenses change or get updated… a single year changes to a range, organizations get acquired. And so do comments. To make sure the next dev doesn’t have to spend time on senseless beautification, try the following 4 comment styles:

// This is a single-line comment.

/* This is also a single-line comment, even better. */

/* This is a double line comment that's too long to
   fit in a single line and may be difficult to read. */
   
/*
 * This is a good style for licenses
 * or detailed function descriptions
 * or very descriptive comments.
 * /

Blank lines

Some devs think blank lines add to readability (or maybe, just looks clean). Let’s check out:

if (var == val) {

    /*
     * This is a complex logic
     * actually roket science
     * and I'm a shitty dev
     * /
    
    var *= 10;
    var++;
    
    val /= 5;
    val--;
    
}

Notice the redundant empty lines and how painful they are? Here’s the right way to do this, assuming:

  • the operations on var and val are unrelated
  • the comment applies only to the operations on var
if (var == val) {
    /*
     * This is a complex logic
     * actually roket science
     * and I'm an adorable dev
     * /
    var *= 10;
    var++;
    
    val /= 5;
    val--;
}

If the operations on var and val are related and the comment applies to both:

/*
 * This is a complex logic
 * actually roket science
 * and I'm an adorable dev
 * /
if (var == val) {
    var *= 10;
    var++;
    val /= 5;
    val--;
}

Seen something similar? Do share your experience on redundant beautification.

2 thoughts on “Code beautification nightmare”

  1. “The developer who did this believes the indentation increases readability.” Not necessarily, it is an ego booster. I stopped doing this kind of things after searching for coding best-practices!

Leave a Reply

Your email address will not be published. Required fields are marked *