🚀 [Cheat Sheet] Refactoring: Improving the Design of Existing Code

Recently, I started reading Refactoring: Improving the Design of Existing Code by Martin Fowler. I thought it would be useful to translate my learnings into bite-size content in Ruby for future references.

Note:

  • As the source is in Java, I have modified the code to make more sense in Ruby
  • As I am trying to keep this concise such that my future self can easily refer to it (hi future self if you are reading this), I will be skipping the implementation steps as I find it to be rather straightforward once we grasped the concept.

Why I recommend this book

1) Refactoring techniques are small

They can take a few minutes to an hour at most (depends on the health of the codebase).

  • Lots of quick wins and instant satisfaction!
  • No need to convince non-engineering management to stop progress to make the changes.
2) Suitable for all
  • Even someone with basic development experience can learn and apply the techniques
  • While the book is in Java, most of the concepts are universal and rather language agnostic.
3) Objectively better

Most approaches are objectively an improvement and do not require any degree of agreement among the engineering team

Composing Methods

Streamline methods, remove code duplication, and pave the way for future improvements

Moving Features Between Objects

Move functionality between classes, create new classes, and hide implementation details from public access

Organizing Data

Untangling of class associations, which makes classes more portable and reusable

Simplifying Conditional Expressions

Simplifying complex conditional expressions, making code more readable and maintainable

Making Method Calls Simpler

Make method calls simpler and easier to understand, thus simplifying the interfaces for interaction between classes

Dealing with Generalization

Move functionality along the class inheritance hierarchy, create new classes and interfaces, and replace inheritance with delegation