If a piece of code is unstable or has some potential problems, then the code will often contain some obvious traces.
Just like before food is going to spoil, it often emits some odors.
We call these traces code smells .
No.1 Duplicated Code
solution:
Two functions within the same one
class
contain the same expression. - If necessaryExtract Method
, extract the duplicate code, and then have both places call the extracted piece of code.Two siblings
subclass
contain the same expression, to avoid this - you needclass
to use bothExtract Method
,Extract
andMethod
push thesuperclass
in.Two unrelated
classes
internals appearDuplicate Code
, and you should consider using one of themExtract Class
, distilling the duplicate code into a standclass
-alone, and thenclass
using the new one in the otherclass
.
No.2 Long Method ( Long Method )
The so-called "benefits"
avoid extra costs
Understand programs without jumping context
solution:
Notes
Conditional expression
cycle
No.3 Large Class
solution:
Variables & Methods
re-abstract
No.4 Long Parameter List
what problems will it cause
useless/switch/sort parameter
incomprehensible
Inconsistent, not easy to use
Modifications are often required
solution:
- abstract into classes
No.5 Divergent Change
Performance: A class is affected by multiple changes
solution:
- Refactoring to keep classes functionally single
No.6 Shotgun Surgery
Performance: A change triggers corresponding modification of multiple classes
solution:
- Extract classes whose changed parts are public
No.7 Attachment Plot ( Feature Envy )
Performance: use a lot of members of other classes
solution:
- If they need to be together, let them be together
No.8 Data Clumps
Performance: a bunch of data that often appears together
solution:
- So if you have a relationship, let's be together and give them a new class.
No.9 Primitive Obsession
solution:
A recurring set of parameters, abstracted into a class
There are multiple arrays associated, abstracted into classes
No.10 Switch Thriller Appears ( Switch Statements )
solution:
state
/strategy
/多态
No.11 Parallel Inheritance Hierarchies
Manifestation: Whenever you
class
add one to onesubclass
, you must alsoclass
add one to the othersubclass
.
solution:
- There should be a class that can remove the inheritance relationship
No.12 redundant class ( Lazy Class )
solution:
- delete
No.13 Talk about the future ( Speculative Generality )
solution:
- delete
No.14 Temporary Field ( Temporary Field )
solution:
- abstract into classes
No.15 Over-coupled Message Chains ( Message Chains )
Manifestation: User asks one object for another object, then asks the latter for another object, then asks for another object... This is
Message Chain
solution:
- dismantle
No.16 Middle Man ( Middle Man )
Performance: Half of the methods of a class interface are delegated to other classes
Essence: Overuse of Delegation
solution:
- Inheritance instead of delegation
No.17 Inappropriate Intimacy
Performance: Two classes use each other's private members or methods
solution:
- Draw the line to break up/merge/one-way link
No.18 Similar classes with different interfaces ( Alternative Classes with Different Interfaces )
solution:
- merge
No.19 Incomplete Library Class ( Incomplete Library Class )
solution:
- Wrap a function or wrap it into a new class
No.20 Naive Data Class ( Data Class )
solution:
- Encapsulate related operations and reduce public member variables
No.21 Refused Bequest
Performance: There are many methods in the parent class, and only a limited number of subclasses are used
Essence: Inheritance System Design Error
solution:
- Replacing inheritance relationships with proxies
No.22 Too many comments ( Comments )
solution:
- Avoid explaining the code with comments, instead explain the purpose, background, etc. of the code, good code speaks for itself
References
"Refactoring - Improving the Design of Existing Code"
https://sourcemaking.com/refactoring/smells