About  Posts  Feed

Start refactoring from the leaves

To reduce duplication in your code, there usually are two ways to approach it:

  1. Start from the top and extract the biggest common block of code to a function
  2. Start from the bottom (leaves) and extract the common expression(s) to a function

It’s best to always aim for doing the latter and start by extracting smaller things rather than bigger things. Here’s why:

Starting small seems counterintuitive because it feels like nothing is getting done. We all like to removing code. It’s better to start small, it always works out better.