How do you refactor a God class?

It’s like Jenga. You will need patience and a steady hand, otherwise you have to recreate everything from scratch. Which is not bad, per se – sometimes one needs to throw away code.

Other advice:

  • Think before pulling out methods: on what data does this method operate? What responsibility does it have?
  • Try to maintain the interface of the god class at first and delegate calls to the new extracted classes. In the end the god class should be a pure facade without own logic. Then you can keep it for convenience or throw it away and start to use the new classes only
  • Unit Tests help: write tests for each method before extracting it to assure you don’t break functionality

Leave a Comment