How to refactor a method that performs several steps (depends on each other)?

What is the best way to reorganize a method that has many steps in it? For example, a method that sets some objects creates several objects (for example, a database table), etc. - basically, one method that performs a set of related steps.

Will this best fit the design pattern of teams?

thanks

+4
source share
3 answers

Well, there is no general answer to this. But about your example of creating and customizing objects, see Builder Pattern and Factory Patten . A command template is useful when you have various possible actions (for example, messages sent through a queue).

The readability of your code is also sometimes useful, just looking for semantic units in your method and reorganizing them into methods, even if you are not reusing them elsewhere. A call to NotifyAllClients tells the reader more than a loop over some collection that calls some method.

+3
source

Builder Template - An appropriate template.

+2
source

Other refactoring methods than those already mentioned can use the template of the template method template, which allows you to extract different parts and, possibly, exchange them, if necessary in the future. You could even use a state template when you would like to change these different parts of the method.

0
source

Source: https://habr.com/ru/post/1305472/


All Articles