Refactoring and Coding Standard: Milan Vukoje
Refactoring and Coding Standard: Milan Vukoje
Refactoring and Coding Standard: Milan Vukoje
Standard
Milan Vukoje
www.vukoje.net
[email protected]
Themes
Iscode important?
Code Refactoring
Coding Standard
Code Review
Tools
Importance of code
Ward Cunningham
When software organization chooses a
design or construction approach that's
expedient in the short term but that
increases complexity and is more costly in
the long term.
Unintentional and intentional debt
Micro design
Code evolution
Embracing change
Clear API with expected behavior
Avoiding coding horror
Why change something that works?
We want programs that are easy to read, that have
all logic specified in one and only one place, that
do not allow changes to endanger existing
behavior, and that allow conditional logic to be
expressed as simply as possible. Kent Beck
Agile methodologies and
Refactoring
XP – no upfront design
TDD – red, green, refactor
Duplicated Code
Long Method
Large Class
Long Parameter List
Feature Envy
Data Clumps
Primitive Obsession
Code smells[2]
Switch Statements
Lazy Class
Speculative Generality
Temporary Field
Message Chains
Middle Man
Data Class
Refused Bequest
Shotgun Surgery
Refactorings: Composing
Methods
Extract Method
Inline Method
Inline Temp
Replace Temp with Query
Introduce Explaining Variable
Split Temporary Variable
Remove Assignments to Parameters
Replace Method with Method Object
Substitute Algorithm
Extract Method
//print details
System.out.println ("name:" + _name);
System.out.println ("amount" + amount);
}
int getRating() {
return (moreThanFiveLateDeliveries()) ? 2 : 1;
}
boolean moreThanFiveLateDeliveries() {
return _numberOfLateDeliveries > 5;
}
int getRating() {
return (_numberOfLateDeliveries > 5) ? 2 : 1;
}
Inline Temp
You have a temp that is assigned to once with a simple expression, and the
temp is getting in the way of other refactorings.
Replace all references to that temp with the expression.
Substitute Algorithm
You want to replace an algorithm with one that is clearer.
Replace the body of the method with the new algorithm.
Refactorings: Simplifying
Conditional Expressions
Decompose Conditional
Consolidate Conditional Expression
Consolidate Duplicate Conditional Fragments
Remove Control Flag
Replace Nested Conditional with Guard Clauses
Replace Conditional with Polymorphism
Introduce Null Object
Introduce Assertion
Decompose Conditional
You have a complicated conditional (if-then-else) statement.
Extract methods from the condition, then part, and else parts.
if (notSummer(date))
charge = winterCharge(quantity);
else charge = summerCharge (quantity);
Consolidate Conditional
Expression
You have a sequence of conditional tests with the same result.
Combine them into a single conditional expression and extract it.
double disabilityAmount() {
if (_seniority < 2) return 0;
if (_monthsDisabled > 12) return 0;
if (_isPartTime) return 0;
// compute the disability amount
double disabilityAmount() {
if (isNotEligableForDisability()) return 0;
// compute the disability amount
Consolidate Duplicate
Conditional Fragments
The same fragment of code is in all branches of a conditional expression.
Move it outside of the expression.
if (isSpecialDeal()) {
total = price * 0.95;
send();
}
else {
total = price * 0.98;
send();
}
if (isSpecialDeal())
total = price * 0.95;
else
total = price * 0.98;
send();
Refactorings: Making Method
Calls Simpler
Rename Method
Separate Query from Modifier
Parameterize Method
Preserve Whole Object
Hide Method
Replace Error Code with Exception
Replace Exception with Test
Refactorings: Moving Features
between objects
Move Method
Move Field
Extract Class
Inline Class
Hide Delegate
Encapsulate Field
Replace Data Value with Object