oliverdavies.uk/source/_daily_emails/2022-12-01.md

1.4 KiB
Raw Blame History

title pubDate permalink
Writing readable code 2022-12-01 daily/2022/12/01/writing-readable-code

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:

  1. Use only one level of indentation per method.
  2. Dont use the else keyword.
  3. Wrap all primitives and strings.
  4. Use only one dot per line.
  5. Dont abbreviate.
  6. Keep all entities small.
  7. Dont use any classes with more than two instance variables.
  8. Use first-class collections.
  9. Dont 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?