How do you refactor a God class?

匿名 (未验证) 提交于 2019-12-03 01:06:02

问题:

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.

回答1:

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


回答2:

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/



回答3:

Before you start: how did you know a God class is bad?

or at least how you measure God class?

Try to find a bound and differences between good-big-class and bad-god-class.

For example: class with some extra helpers, properties, and built-in util-methods, is a candidate to be God class in future, but it can be also a very good piece of code.

I believe that God class is just a big class body with forgotten or postponed refactoring, but a reason for that refactoring growing by time.

General advice here is a usage of:

  • inheritance and mixins,
  • delegation (ex: to utils files, external classes, API/facade),
  • splitting class: adding subclasses or related classes,
  • using good pattern, like S.O.L.I.D,
  • developing in yourself the art of refactoring,


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!