Professional Documents
Culture Documents
Milan Vukoje
www.vukoje.net
Soprex
• SkfOffice2
• SkfOffice3
• Big5
• Quality oriented
• We are hiring…
Agenda
• Why is (clean) code important?
• Refactoring (definition and samples)
• Tools
• When to and when not to refactor?
• Summary
Is code important?
• Is construction relatively mechanical process?
• Only activity that’s guaranteed to be done
• 50-65% of overall effort
• 50-75% of overall errors
Coding Horror
• Stress
• Fear
• Cargo cult programming
• “Just in case” coding
• Unusual software bugs (Heisenbug ,
Mandelbug, Schroedinbug… )
Code Example
Technical Debt
– If you can get today's work done today, but you do it
in such a way that you can't possibly get tomorrow's
work done tomorrow, then you lose. – Kent Beck
• 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
Refactoring
• Refactoring - a change made to the internal structure of software to
make it easier to understand and cheaper to modify without changing
its observable behavior.
• Set of rules and techniques for enhancing code while reducing
chances for error
• Refactoring: Improving the Design of Existing Code --Martin
Fowler
Composing Methods
1. Extract Method
void printOwing(double amount) {
printBanner();
//print details
System.out.println ("name:" + _name);
System.out.println ("amount" + amount);
}
int getRating() {
return (_numberOfLateDeliveries > 5) ? 2 : 1;
}
Managing temps
1. Inline Temp
double basePrice = anOrder.basePrice();
return (basePrice > 1000)
if (notSummer(date))
charge = winterCharge(quantity);
else charge = summerCharge (quantity);
Simplifying conditionals
2. Consolidate Duplicate Fragments
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();
Simplifying conditionals
3. Consolidate Conditional Expression
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
Moving Features between objects
• Replace Method with Method Object
• Extract/Inline Class
• Pull Up/Down Field/Method
• Extract Subclass/ Superclass/Interface
• Collapse Hierarchy
Benefits
• Less code
– less bugs
• Readable business logic
– better business domain understanding
• Self explaining code
– Less documentation and faster changes
• Better design
– Higher encapsulation and reusability
– Cleaner concepts and structure
Visual Studio 2008
Need for refactoring
• 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
• Micro design - clear API and logical structures
• Code evolution - Embracing change
• Avoiding coding horror by managing complexity
• Agile methods (XP, TDD, Scrum)
When Should You Refactor?
• Not having enough time usually is a sign that you need to do some
refactoring. – Martin Fowler
Milan Vukoje
www.vukoje.net
vukoje@gmail.com
Molimo vas da popunite ankete!
Please fill out the evaluations!
Svi posetioci koji popune All attendees that fill out the
ankete ulaze u nagradnu evaluations are taking part
igru in drawing of special prizes