This week, I needed to investigate and fix a bug within some existing code.
It's code written some time ago and not by anyone working on the team.
The code wasn't very readable, so before I could fix the bug, I needed to figure out what the code was supposed to be doing.
I started to write a list of things that would make the code easier to read and understand - no single-letter variable names, reduced levels of indentation and splitting some nested ternary operators to use separate return statements.
I also watched a video of a conference talk titled "Writing code you won't hate tomorrow", which re-introduced me to Object Callisthenics.
They are from "The ThoughtWorks Anthology" book and are some steps that include some of the points that I had written:
Use only one level of indentation per method.
Don’t use the else keyword.
Wrap all primitives and strings.
Use only one dot per line.
Don’t abbreviate.
Keep all entities small.
Don’t use any classes with more than two instance variables.
Use first-class collections.
Don’t use any getters/setters/properties
As well as the original book, there are numerous blog posts and videos on this topic.
Why try some of them on the next code you write and see if it's easier to read and understand?
- Oliver
Was this interesting?
About me
I'm an Acquia-certified Drupal Triple Expert with 17 years of experience, an open-source software maintainer and Drupal core contributor, public speaker, live streamer, and host of the Beyond Blocks podcast.