Give Me Alignment Or Give Me Death!

I am an Alignment Nazi.

An alignment Nazi is someone who looks at code like this:

int v = 24;
double f = 56.7;

and knows it should be formatted like this:

int    v = 24;
double f = 56.7;

This is my disease. I know my colleagues find this amusing. Just something Gordon does. No need to pay all that much attention.

Except, I think it matters.

Why?

Well, I find it a lot easier to read code where the same thing appears in the same place. In my example above the types appears in a single rectangular block, the variable names are in a different block and the assigned values in yet another block.

It makes sense to me. So why not for others?

I’m not sure but I wonder if it is my preference for images over text. I, and I suspect a lot of other people, get a lot more out of a well crafted graphic. Pictures can be worth a thousand words. And putting pictures into words often doesn’t work.

Here is another weirdo aligment thing I like:

final Map<String, Integer> lookup = new HashMap<String, Integer>();
final List<String>        strings = new ArrayList<String>();
final SyncObject             sync = new SyncObject();

Note how the equals signs line up and how the variable names are right justified rather than left justified. I don’t do this all the time but I often do it when I have a block of code that uses a set of objects within it.

You might also suspect that I deliberately name some variables so that they have the same length so that alignment just works without adding in extra space. You would be right. What do you think of this?

int strt = 23; // yes, 'strt' instead of 'start'
int stop = 47;

And no, I’m not so much of a Nazi that I want you to die for disagreeing with me over my fetish. But please, please, please don’t reformat the code blindly. If you take a moment, and think a bit, you might see a glimmer of merit in my crazy ideas about alignment.

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