34 lines
1.4 KiB
Markdown
34 lines
1.4 KiB
Markdown
|
---
|
|||
|
title: >
|
|||
|
Writing readable code
|
|||
|
pubDate: 2022-12-01
|
|||
|
permalink: >-
|
|||
|
archive/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.
|
|||
|
1. Don’t use the else keyword.
|
|||
|
1. Wrap all primitives and strings.
|
|||
|
1. Use only one dot per line.
|
|||
|
1. Don’t abbreviate.
|
|||
|
1. Keep all entities small.
|
|||
|
1. Don’t use any classes with more than two instance variables.
|
|||
|
1. Use first-class collections.
|
|||
|
1. 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?
|