Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you refactor a God class?

Tags:

Does anyone know the best way to refactor a God-object?

Its not as simple as breaking it into a number of smaller classes, because there is a high method coupling. If I pull out one method, i usually end up pulling every other method out.

like image 434
Oliver Watkins Avatar asked Feb 14 '13 08:02

Oliver Watkins


People also ask

What is God Class and why should we avoid it?

God classes are the literal opposite of the single-responsibility principle, taking on too many responsibilities and being highly unfocused. This causes engineers to reinvent the wheel, duplicate code, and incur technical debt.

What is God Class in Java?

A “God Class” is an object that controls way too many other objects in the system and has grown beyond all logic to become The Class That Does everything. It is a class that centralizes the intelligence in a system given that it is large and complex and uses data from other classes.


2 Answers

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
like image 58
Fabian Schmengler Avatar answered Nov 15 '22 21:11

Fabian Schmengler


I assume "God Object" means a huge class (measured in lines of code).

The basic idea is to extract parts of its functions into other classes.

In order to find those you can look for

  • fields/parameters that often get used together. They might move together into a new class

  • methods (or parts of methods) that use only a small subset of the fields in the class, the might move into a class containing just those field.

  • primitive types (int, String, boolean). They often are really value objects before their coming out. Once they are value object, they often attract methods.

  • look at the usage of the god object. Are there different methods used by different clients? Those might go in separate interfaces. Those intefaces might in turn have separate implementations.

For actually doing these changes you should have some infrastructure and tools at your command:

  • Tests: Have a (possibly generated) exhaustive set of tests ready that you can run often. Be extremely careful with changes you do without tests. I do those, but limit them to things like extract method, which I can do completely with a single IDE action.

  • Version Control: You want to have a version control that allows you to commit every 2 minutes, without really slowing you down. SVN doesn't really work. Git does.

  • Mikado Method: The idea of the Mikado Method is to try a change. If it works great. If not take note what is breaking, add them as dependency to the change you started with. Rollback you changes. In the resulting graph, repeat the process with a node that has no dependencies yet. http://mikadomethod.wordpress.com/book/

like image 37
Jens Schauder Avatar answered Nov 15 '22 20:11

Jens Schauder