Code Refactoring
A systematic process of improving code without creating
new functionality that can transform a mess into clean
code and simple design.
https://refactoring.guru/refactoring
2
Dirty Code
The result of inexperience multiplied by tight deadlines,
mismanagement, and nasty shortcuts taken during the
development process.
3
Clean Code
Easy to read, understand and maintain.
Clean code makes software development predictable and
increases the quality of a resulting product.
4
5
Refactoring process
Performing refactoring step-by-step and running tests
after each change are key elements of refactoring that
make it predictable and safe.
6
The main purpose of refactoring process
• Transforms a mess into clean code and simple design.
• Clean code doesn’t contain duplication.
• Minimal number of classes and other moving parts.
• Clean code passes all tests.
• Easier and cheaper to maintain!
7
Causes of dirty code
• Business pressure
• Lack of tests
• Lack of documentation
• Lack of interaction between team members
• Delayed refactoring
• Lack of compliance monitoring
• Incompetence of coders
8
When to Refactor
• When adding a feature
• When fixing a bug
• During a code review
9
How to Refactor
• A series of small changes
• The code should become cleaner
• New functionality shouldn’t be created during refactoring
• All existing tests must pass after refactoring
10
Large Classes (Monolithic)
Methods and classes that have increased to
such large Lines of Codes that they are hard to
work with.
Long Methods
A method contains too many lines of code.
Generally, any method longer than ten lines should make
you start asking questions.
12
13
Using many primitive parameters
If you have a large variety of primitive fields, it may be possible
to logically group some of them into their own class.
Even better, move the behavior associated with this data into the
class too.
For this task, Replace Data Value with an Object.
14
Using many parameters in a method
More than three or four parameters for a method.
You can merge them into a single parameter object
Instead of passing a group of data received from another object as
parameters, pass the object itself to the method,
15
Alternative Classes with Different Interfaces
Two classes perform identical functions but have different method
names.
You get rid of unnecessary duplicated code, making the resulting
code less bulky.
16
Refused inheritance
If a subclass uses only some of the methods and properties inherited
from its parents.
17
Parallel inheritance hierarchies
Whenever you create a subclass for a class, you find yourself
needing to create a subclass for another class.
18
Divergent change
You find yourself having to change many unrelated methods when
you make changes to a class.
For example, when adding a new product type you have to change
the methods for finding, displaying, and ordering products.
Extracting Classes
When one class does the work of two.
Create a new class and place the fields and methods responsible
for the relevant functionality in it.
20
Lazy Classes
Understanding and maintaining classes always costs time and
money.
So if a class doesn’t do enough to earn your attention, it should be
deleted.
Deleting useless comments
Comments are usually created with the best of intentions,
when the author realizes that his or her code isn’t intuitive or obvious.
In such cases, comments are like a deodorant masking
the smell of fishy code that could be improved.
22
Duplicate code
Two code fragments look almost identical.
Pull Up Field
Subclasses grow and develop separately, causing identical fields
and methods to appear.
23
Data Class
It contains only fields and crude methods for accessing them
(getters and setters).
These are simply containers for data used by other classes.
24
Feature Envy
A method accesses the data of another object more than its own
data.
Determine which class contains most of the data used.
Then place the method in this class along with the other data.
25
Message chains
In code you see a series of calls resembling $a->b()->c()->d()
Hiding delegates
Reduces dependencies between classes of a chain.
26
Hiding delegates
The client gets object B from a field or method of object А. Then the
client calls a method of object B.
Create a new method in class A that delegates the call to object B.
Now the client doesn’t know about, or depend on, class B.
27
Removing the man in the Middle
If a class performs only one action, delegating work to
another class, why does it exist at all?
28
Inappropriate Intimacy
One class uses the internal fields and methods of another class.
Move method or Move Field
A method is used more in another class than in its own class.
29
Replacing delegation with inheritance
A class contains many simple methods that delegate to all methods
of another class.
Make the class a delegate inheritor, which makes the delegating
methods unnecessary.
30
Parameterize Method
Combine these methods by using a parameter that will pass the
necessary special value.
31
Introduce Parameter Object
Replace these parameters with an object.
32
Hide Method (encapsulation)
A method isn’t used by other classes or is used only inside its own
class hierarchy.
33
Rename Methods
The name of a method doesn’t explain what the method does.
34
Separate Query from Modifier
Do you have a method that returns a value but also changes
something inside an object?
35
Decompose Conditional
You have a complex conditional statement
Consolidate Duplicate Conditional Fragments
37
38