The Shape of Code
I heard Uncle Bob say this at a talk:
Programmers don't hate bad code, They get used to the shape of it.
When I first heard it, I laughed a bit… but now when I look at some old code, I know exactly what Uncle Bob meant and I cry.
Code starts to take on a shape as it grows large and/or old. The code works, but its shape starts to, as I like to call it, ‘snake’. Example from my recent code:
This block of code isn’t hard to read, but it takes some time to read.
The code starts at the top left and gets a little longer, where assignment starts to curve in and out in between variable and value, like a snake.
Code Alignment
I thought snaking code was just a natural process of all code bases over time and will be the eventual shape of code.
When I discovered code alignment, it completely changed my opinion about how the shape of code can become over time.
If the previous code is code aligned, it would look like:
I find this code much easier to read than the previous version, even though both are exactly the same, only difference between them: white space.
At a glance, isn’t this block of code just pretty and approachable? I instantly feel the programmer who worked on this code before really cared, not only about the functionality, but also the code.
By aligning code well, the shape of code can be made better and kill off the code snake!
Improving the Shape of Code
One can improve the shape of their code today by applying the principles of code alignment.
So, instead of just creating code, which its shape will default to:
Start shaping the code so it is like this:
I loved the way the code looks when it is aligned well. Coming back to work on aligned code felt better than unaligned, or ‘snaking’ code.
Code Alignment in the wild:
I’m not the only one who thinks like this, let’s look at some code that’s in the wild:
Rubocop
I have been using Rubocop religiously and even submitted a fix!. Looking at one part:
Even with one variable name longer, KeywordSplatNode, keeping everything aligned makes the whole data structure is easy on the eyes.
Linux
When I look at one of the largest open source code bases out there, I found a great example:
What I find very impressive is the indenting is maintained, even through the if/else statements and this is 12year + 7year old code!
How to
Aligning code can be very mundane, especially doing it by hand. I would recommend hand aligning the first time, but getting editor support will save your sanity.
Emacs
Emacs can align anything based on a regex and it is built in. The
command: M-x align-regex
.
Vim
Has an external plugin: vim-easy-align which looks like it will align anything!
Visual Studio, Sublime, Notepad++, & Blend
For other popular editors, http://codealignment.com has great plugins
Conclusion
I thought the shape of code for an old code bases trended towards becoming a ‘snake’. Getting thick and windy for assignment variable and values.
After learning about code alignment, I see the shape of code can be pretty, even for very old code bases.
Now when I look back at my code that has been aligned, I love it. I just want to get in an code on it again. Present me thanks past me for taking the extra effort to align things, so present me can quickly see what’s going on without winding through it.
Start making code pretty today, future you will appreciate it! :-)