This week's focus: Refactoring

Lucian Ghinda
2 min readJun 13, 2023

Look at your code and identify any areas where you can improve. Refactoring isn’t about fixing bugs but improving your code’s readability, efficiency, and maintainability.

Look for complex methods that could be broken down, repeated code that could be extracted into its method or class, and any design patterns that could be better applied.

But mostly, ask yourself: How much effort will it take a new colleague to understand what this code does?

For inspiration, look at these eloquent words from Yukihiro Matz (from the Beautiful Code book). I get back to reading that chapter from time to time when I contemplate refactoring and how I write Ruby code:

A quote from Yukihiro Matz about what does it mean by beautiful code
Source: Yukihiro Matz in Beautiful Code

Start small

  1. Open a file
  2. Identify a method that you think is complex or hard to understand
  3. Make sure you have a test that covers functionally what that piece of code should do (could also be indirect)
  4. Choose one thing you want to improve: make it smaller, readable, or better naming or anything that you think will make the code better
  5. Refactor it
  6. Ask for a review with at least a focus on readability.

Of course, if you have more time, then there are other strategies to approach refactoring. But in case you don’t have too much dedicated time, let’s focus this week on some refactoring bits and pieces.

Enjoyed this article?

Join my Short Ruby News newsletter for weekly Ruby updates. Also, check out my co-authored book, LintingRuby, for insights on automated code checks. For more Ruby learning resources, visit rubyandrails.info.

--

--