View
92
Download
3
Category
Preview:
Citation preview
Code Smell are certain structures in the code that indicate violation of
fundamental design principles and negatively impact design quality.
Refactoring is a disciplined technique for restructuring an existing body of code,
altering its internal structure without changing its external behavior.
“A code smell is a surface indication that usually corresponds to a deeper problem in the system”
- Martin Flower
SOLID*
● Single Responsibility
● Open Close principle
● Interface Segregation
● Dependency Inversion principle
● Liskov Substitution principle
*This are the principles, not laws.
STUPID
● Singleton
● Tight Coupling
● Untestability
● Premature Optimization
● Indescriptive Naming
● Duplication
Levels of Code smell
There are three levels of
code smell:
1. Method level
2. Class level
3. Application level
Long class
- Single responsibility
- Readability
- Untestability
- Extract Class
- Extract Subclass
- Inline Class
- Collapse Hierarchy
Symptoms
Remedy
Long Method
- Do not have single responsibility
- Readability
- Untestability
- Extract Method
- Replace temp with query
- Split temporary variables
- Separate query from modifiers
Symptoms
Remedy
Long Parameter list
- More than 3 parameter in constructor or method
- No Readability
- Use Builder pattern
- Replace parameter with object
- Preserve whole object
Symptoms
Remedy
Primitive Obsession
- A lots of primitives in code
- Use of constants for sub-type like,
USER_MANAGER = 1
USER_EMPLOYEE = 2
- Replace Type Code with class, subclass or state/strategy pattern
- Replace parameter with object
Symptoms
Remedy
Duplicate Code
- Same code structure in more than one place
- Same expression in two sibling class
- Extract Method or Class
- Pull up field or method
Symptoms
Remedy
Switch statements
- Obviously when you see switch everywhere in your code
- Use enums
- Replace type code with sub class
- Replace type code with state/strategy
- Replace conditional with polymorphism
Symptoms
Remedy
*If you have only few cases that affects single method only, then polymorphism is overkill.
Refused Bequest
- When your subclass has a behaviour inherited from parent which is not
needed.
- Push down a method or field
Symptoms
Remedy
Speculative Generality
- Oh! We might need this feature in future.
- Collapse Hierarchy
- Inline Class
- Remove extra parameter from method
Symptoms
Remedy
Lazy Class
- Each class you create costs money to maintain and understand. A class
that does nothing.
- Collapse Hierarchy
- Inline Class
Symptoms
Remedy
Comments
- Too much comments in code.
- Add comments only where needed
- Class name or method name should be descriptive as we do not need
to add comments for what they are doing.
Symptoms
Remedy
Comments are for Human, not for Machine.
Referenceshttps://blog.codinghorror.com/code-smells/
https://andigeeky.wordpress.com/2017/03/23/code-smell-refactoring/
https://www.csie.ntu.edu.tw/~r95004/Refactoring_improving_the_design_of_existing_c
ode.pdf
http://www.competitivenessc.com/files/hpchp.pdf
Recommended