DEV Community

Cover image for Alphabetize your Way to More Readable Code
Todd Eidson
Todd Eidson

Posted on • Originally published at eidson.info

Alphabetize your Way to More Readable Code

Originally posted on my personal blog.

Alphabetize your Way to More Readable Code

Super short and simple post this time, because it's explaining a simple concept. The concept is so simple, that I'm slightly embarrassed, almost shocked, that it took me almost 7 years of professional programming, and 3 and a half years of college to see and use this pattern.

The tip? Any time you are writing code and the order of the lines is irrelevant, such as declaring class variables, creating a JSON object, populating an associative array, adding dependencies, configuring your configuration, ad infinitum, use alphabetical order.

Instead of:

{
  name: "Marshall Mathers",
  palms: "sweaty",
  knees: "weak",
  arms: "heavy",
  nervous: true,
  sweater: "mom's spaghetti"
}

Use this:

{
  arms: "heavy",
  knees: "weak",
  name: "Marshall Mathers",
  nervous: true,
  palms: "sweaty",
  sweater: "mom's spaghetti"
}

Sure, it does not rhyme as well, but if you need to change nervous to false, it is much easier to find in the list. It does take a little extra brain power to add an item to the list, instead of just appending to the list, you may have to sing a phrase of the alphabet (unless you have a tool that sorts for you, see below). However, since code is read at an average of 10 times after it is written, you'll see the pay off when looking through a long JSON object or associative array to alter or fix something.

I used to try and use some sort of hierarchical order with grouping in most places that I use this now, but there is no real hierarchy in most places that this can be implemented. Take for an example a 'person' object or class. Sure, 'name' is pretty important and would always go first (if not a uid), but how do you order the rest of the fields? Is a phone number more or less important than an address? As far as packages and dependencies go, one package might be used a lot more than another package, but I'd argue that one is no more important than the other. If it wasn't important, it wouldn't be a dependency. In fact, it was when I started a new Symfony project a few months ago and noticed that they had added "config": {"sort-packages": true} to composer.json and the profound effect that it had had on the readability of the "require" section as to what packages I was using in a project that I it had dawned on me that I should be using alphabetical order in as many places as I could to improve readability.

It wouldn't really feel like one of my blog posts if I didn't somehow mention my favorite IDE, PhpStorm, so here it is: I've recently discovered a plugin, called Lines Sorter by Sylvain Francois, that will do your sorting for you. After you have the plugin installed, highlight the lines you wish to have sorted and then in the menu go to Edit>Sort Lines or use the keyboard shortcut alt + shift + L (I had to unmap this shortcut from the default 'Load Context' option, first). This plugin will work in any of the JetBrains IDEs.

Is this something you've been doing for years? Do you have any tips that are so simple you can't believe how long it took you to incorporate them? Do you know of a VS Code plugin to accomplish this? Any arguments against using this tip? Please let me know in the comments.

Top comments (0)