Code smell & Refactoring
Mamata Gelanee
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
“Code as SOLID developer not STUPID one!”
- William Durand
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
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
Long Parameter List
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
Primitive Obsession
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
Duplicate code
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.
Switch statements
Switch statements
Bird
getSpeed()
African
getSpeed()
Asian
getSpeed()
European
getSpeed()
Refused Bequest
- When your subclass has a behaviour inherited from parent which is not
needed.
- Push down a method or field
Symptoms
Remedy
Oddball Solution
Move method or Move class
Black Sheep
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.
80%time of software lifecycle, is a maintenance, only while 20% of time, we
develop a software!
Quiz
Duplicate
Code
Quiz
Feature Envy
Quiz
Long class
Quiz
Long
Parameter list
Quiz
Conditional
Complexity
References
https://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
Thanks!
Software Engineer,
Mastercard Transaction solution pvt. Ltd.,
Vadodara - Gujarat
AndiGeeky
@MamataGelanee
andigeeky.wordpress.com/
mamatagelanee07

Baroda code smell and refactoring

Editor's Notes

  • #2 Add GDG Baroda logo