refactoring
DESCRIPTION
This is my presentation in Tabriz open space meeting about Refactoring.TRANSCRIPT
Clean CodeRefactoring
1
Refactoring
Any fool can write code that a computer can understand. Good programmers write code that humans can understand.
2
Refactoring
What is Refactoring?
Martin Fowler : "a change made to the internal structure of software to make it easier to understand and cheaper to modify without changing its observable behavior“.
William C. Wake : "Refactoring is the art of safely improving the design of existing code“.
3
Refactoring
The goal of refactoring is NOT to add new functionality
The goal is refactoring is to make code easier to maintain in the future
The process of refactoring involves the removal of duplication, the simplification of complex logic, and the clarification of unclear code.
4
Refactoring
When Should You Refactor?
• Refactor When You Add Function
• Refactor When You Need to Fix a Bug
• Refactor As You Do a Code Review
5
Refactoring
The Refactoring Cycle
Choose the worst smell
Select a refactoring that will address the smell
Apply the refactoring
6
RefactoringBad Smells in Code
• Duplicated Code• Long Method• Large Class• Long Parameter List• Divergent Change• Shotgun Surgery• Feature Envy• Data Clumps• Primitive Obsession• Switch Statements• Parallel Inheritance Hierarchies• Lazy Class• Speculative Generality• Temporary Field• Message Chains• Middle Man
7
RefactoringBad Smells in Code
• Inappropriate Intimacy• Alternative Classes with Different Interfaces• Incomplete Library Class• Data Class• Refused Bequest• Comments
8
RefactoringExtract Method
void printOwing() { printBanner(); //print details System.out.println ("name: " + _name); System.out.println ("amount " + getOutstanding()); }
void printOwing() { printBanner(); //print details System.out.println ("name: " + _name); System.out.println ("amount " + getOutstanding()); }
void printOwing() { printBanner(); printDetails(getOutstanding()); } Void printDetails (double outstanding) { System.out.println ("name: " + _name); System.out.println ("amount " + outstanding); }
Reusabilit
y
Readability
High Cohesion
Polymorphism
9
RefactoringMove Method
10
RefactoringMove Method
low CouplingHigh Cohesion
11
RefactoringEncapsulate Field
you should never make your data public. When you make data public, other objects can change and access data values without the owning object's knowing about it.
12
RefactoringExtract Interface
13
RefactoringRemove Parameter
A parameter is no longer used by the method body. Remove it.
14
RefactoringReplace Nested Conditional with Guard Clauses
A method has conditional behavior that does not make clear the normal path of execution.Use guard clauses for all the special cases.
Readability
15
Refactoring
Visit our Page on Facebook!
16