Coding Style: Vertical Alignment Sucks


No, it doesn’t!
— another opinion

That’s right, vertical alignment sucks. Big time. Call me names and throw stuff at me, but it’s like a plaque, it’s everywhere, and I really, really don’t understand why. Get ready, this is going to get emotional.

I am talking about something like this:

public final int    MODE_ON  = 0x0001;
public final int    MODE_OFF = 0x0002;

private      long   myFoo    = 123;
private      String theirBar = "doh";

Folks who “format” their code like that claim that it is much more beautiful and readable than

public final int MODE_ON = 0x0001;
public final int MODE_OFF = 0x0002;

private long myFoo = 123;
private String theirBar = "doh";

Beautiful? Come on, where are we, on a catwalk? Readable? Why would one need to be able to scan data types or variable names from top to bottom anyway? Isn’t it the left-to-right thing which is actually relevant here?

Now, suppose Bob is a vertical alignment freak working on a project with a bunch of other developers, and he is to add two new entries to his own, previously “beautified” piece of code:

public final int    MODE_ON  = 0x0001;
public final int    MODE_OFF = 0x0002;
public final int    MODE_IDLE = 0x0003;
public final int    MODE_WAITING_FOR_XMAS = 0x0004;

private      long   myFoo    = 123;
private      String theirBar = "doh";

Oops, it’s not “readable” anymore! Let’s correct it:

public final int    MODE_ON               = 0x0001;
public final int    MODE_OFF              = 0x0002;
public final int    MODE_IDLE             = 0x0003;
public final int    MODE_WAITING_FOR_XMAS = 0x0004;

private      long   myFoo    = 123;
private      String theirBar = "doh";

Better. Spacier, pun intended. But hey, what about the second block? It looks really ugly, huh? The values are not aligned!

public final int    MODE_ON               = 0x0001;
public final int    MODE_OFF              = 0x0002;
public final int    MODE_IDLE             = 0x0003;
public final int    MODE_WAITING_FOR_XMAS = 0x0004;

private      long   myFoo                 = 123;
private      String theirBar              = "doh";

No-o-ow it’s perfect! Looks really cool! It doesn’t matter that the values are now behind the horizon off the variables they are assigned to, who needs to know, anyway? The point is to have them ordered in a column! And, as a bonus, we have introduced some extra space, so we can draw dancing lemmings in there when the code is printed out!

Hitting the space key continuously is kinda relaxing and meditative, by the way. With thirty to fifty lines to (re-)align, during the process one could ponder upon what to cook tonight or how to break it down to the boss that you’re not happy with your salary.

To spare time and avoid routine, Bob can use tabs. Why, his IDE is configured to show tabs of four-space width, so 3 tabs and 2 spaces make 14 spaces, right? Oh, well, it only makes it look like 14 spaces, but why would Bob care: it does look aligned on his machine, right? Aren’t all IDEs in this company configired in the same way? (He never had a chance to talk about this to Alice, a fellow developer.)

Almost forgot: searching becomes real fun because regular expressions instead of simple strings must be used now. Let’s find myFoo += 3; — without knowing how many spaces there are, of course:

myFoo *\+\= *3;

One day, Alice will probably drop by in Bob’s cubicle and ask him what changes exactly apply to the ticket for which his code was checked in. Because you see, her diff tool will be showing thirty lines of changes, while there are only two that are relevant to the solution, and the rest is pure alignment. Well, maybe Bob still knows what’s where. But how should the version control system or Alice know? Even if the diff tools in use are smart enough to correctly detect and display adjustments within a single line, Bob’s fellow developers will still stumble upon a large red block of code and waste their time separating the wheat from the chaff. Thank you, Bob.

I don’t have to tell you that files tend to grow over time. And believe me, nobody cares how you (are trying to) align the part of code you wrote, just as nobody will ever follow any vertical alignment convention. Because there is none. Because there is no time. Because nobody gives a damn. In the end, the “beautified” code always end up looking like this:

public final int    MODE_ON           = 0x0001;
public final int    MODE_OFF          = 0x0002;
public final int    MODE_IDLE           = 0x0003;
public final int    MODE_WAITING_FOR_XMAS= 0x0004;
public final int MODE_ALIGNING = 0x0005;
public final int MODE_ERROR = 0x0006;
private      long myFoo                = 123;
private      String theirBar           = "doh";
private      SomeObject beeBop           = null;
private      SomeOtherObject  leeLoo  = new SomeOtherObject();
private  int[] a;
private   int[] b;

You still want to call this readable? I’d rather stick to good ol’ “inreadability”, — and bottoms up to single spaces:

public final int MODE_ON = 0x0001;
public final int MODE_OFF = 0x0002;
public final int MODE_IDLE = 0x0003;
public final int MODE_WAITING_FOR_XMAS = 0x0004;
public final int MODE_ALIGNING = 0x0005;
public final int MODE_ERROR = 0x0006;

private long myFoo = 123;
private String theirBar = "doh";

private SomeObject beeBop = null;
private SomeOtherObject leeLoo = new SomeOtherObject();

private int[] a;
private int[] b;

And now — some sweet dessert, my personal favorite (an extract from some two dozen lines of “perfectly aligned” code):

TinyObject              t          = new TinyObject();
SomeFunnyObject         funnyThing = new SomeFunnyObject(t);
funnyThing.setVolume                (SomeFunnyObject.VERY_LOUD);
funnyThing.setIntensity             (SomeFunnyObject.INSANE);

Do you recognize the method calls at first sight? That’s right, you probably don’t, not even at second or third. Neither do I. But it’s okay. Because it’s much more beautiful than confusing, after all!

Programming languages are derivatives from “normal” languages, remember? Why mess with this concept?

Share

Posted

in

by

Comments

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.