Help, I need to refactor a mega class! Here are 5 tips …

By on 24 September 2022

Somebody asked the other day for tips on how to refactor a mega-class? It was actually one of the first tasks on the new job, ouch!

A single class, several thousands lines of code, no tests available šŸ˜®

You might scratch your head and say WTF?! After all, good developers decouple code into manageable pieces and write a test suite, no? Well, not everywhere, horror stories like this are more common than you might think šŸ˜„

So the person tasked with this was now puzzled šŸ˜µ

– How do you figure out what is going on with a new class?

– Do you go function by function and read the code and make notes?

– Do you step through with a debugger?  

– Maybe call the class methods directly to understand what they do?

Luckily we have an awesome community of developers (join here) which graciously came to the rescue.

Here are some tips I distilled from the conversation:

1. Run it.

You need a way to run the code. See where it is used and how. That should give you hints and hopefully it allows you to write a couple of tests for it so you have a safety net for refactoring.

2. Narrow down the scope.

It would be good to ask why the code needs to be refactored, is it giving any problems?

Then zoom in on specific areas and address those first, not touching the rest yet (ā€œif it ainā€™t broken, donā€™t fix itā€).

3. Apply 80/20 (Pareto principle).

Sometimes work to clean up/ refactor some of the code (most used pieces), and then when a high percentage has been cleaned up, then undergo the full refactor of the remainder – you may find that the full refactor is unnecessary.

There is actually a great book on this topic:Ā Working Effectively with Legacy CodeĀ by Michael Feathers.

Also the following video was shared as a helpful resource providing a step-by-step approach:Ā Brett Slatkin’s Refactoring Python: Why and how to restructure your code (PyCon 2016).

4. Regroup the code by functionality.

One long God class might not be necessary, can it be broken up into multiple functions / smaller classes and more modules?

Maybe you can logically break it out over different areas. A nice side effect is that overall naming and encapsulation (namespacing) will improve as well.

One thing I typically find is once I start breaking it down into smaller classes/components/methods etc I keep going until it is all changed. Makes me feel better, but can take longer though. May want to create some initial tests to confirm the as-is and the to-be work the same for external calls.

5. Improve your code reading skills.

In The Programmer’s Brain, Felienne Hermans offers 7 strategies to use as you are reading code:

  • Activating: Thinking of related things to activate prior knowledge
  • Monitoring: Keeping track of your understanding of a text
  • Determining Important: Deciding what parts of a text are most relevant
  • Inferring: Filling in facts that are not explicitly given the text
  • Visualizing: Drawing diagrams of the read text to deepen understanding
  • Questioning: Asking questions about the text at hand
  • Summarising: Creating a short summary of a text

You can use these steps to create a model of the code. Don’t be afraid of non-digital tools either. White boards and paper and pencil can be used to draw things out. It’s important to createĀ mental modelsĀ of how the different parts of the code relate to each other.

By the way, we spoke with AJ about The Programmer’s Brain book on our podcast, you can listen here.


We hope that these tips help you if you’re ever faced with this situation at your (new) developer job.

Thanks and shoutout to Heather, Thomas, AJ, Richard, Russell and David from our Pybites Community for contributing to this discussion!

Want a career as a Python Developer but not sure where to start?