You get a bonus - 1 coin for daily activity. Now you have 1 coin

Refactoring techniques. Simplify method calls

Lecture



These refactorings make method calls easier and clearer to understand. This, in turn, simplifies the interaction interfaces between the classes.

Rename method

Problem: The name of the method does not reveal the essence of what it does.

Solution: Change the name of the method.

Add parameter

Problem: The method does not have enough data to perform some actions.

Solution: Create a new parameter to transfer this data.

Deleting a parameter

Problem: The parameter is not used in the method body.

Solution: Delete the unused parameter.

Separation of request and modifier

Problem: You have a method that returns some value, but it changes something inside the object in the process.

Solution: Divide the method into two different methods. Let one of them return a value, and the second modifies the object.

Parameterization method

Problem: Several methods perform similar actions, which differ only in some internal values, numbers or operations.

Solution: Combine all these methods in one with the parameter to which the different value will be passed.

Replacing a parameter with a set of specialized methods

Problem: The method is divided into parts, each of which is performed depending on the value of a parameter.

Solution: Extract the individual parts of the method into your own methods and call them instead of the original method.

Transfer the entire facility

Problem: You get several values ​​from an object, and then pass them to the method as parameters.

Solution: Pass the entire object instead.

Replace a parameter with a method call

Problem: Calling a method and passing its results as parameters of another method. In this case, the value of the parameters could be obtained inside the called method.

Solution: Instead of passing the value through the method parameters, try moving the code for getting the value inside the method itself.

Replacing parameters with an object

Problem: In your methods there is a repeating group of parameters.

Solution: Replace these parameters with an object.

Remove setter

Problem: The field value should be set only at the moment of creation and never change again.

Solution: Remove the methods that set the value of this field.

Method hiding

Problem: The method is not used by other classes or is used only within its class hierarchy.

Solution: Make the method private or protected.

Replacing the designer with the factory method

Problem: You have a complex constructor that does more than just setting the values ​​of an object's fields.

Solution: Create a factory method and replace constructor calls with it.

Exception error code replacement

Problem: The method returns a specific value that will signal an error.

Solution: Together, an exception should be thrown.

Replace exception by checking condition

Problem: You throw an exception where you can get by with a simple check of the condition.

Solution: Replace throwing an exception by checking this condition.


Comments


To leave a comment
If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Refactoring theory

Terms: Refactoring theory