SlideShare a Scribd company logo
Refactoring
Improving the design of existing code …
…. without changing the behavior!

agile42 | The Agile Coaching Company

www.agile42.com |

All rights reserved. Copyright © 2007 - 2014.
Why do good developers write bad software?
• Project and budget pressure lead to taking shortcuts
• You realize there are better ways to do something
• Requirements change over time
!

• When it’s hard to change update your code this lead to less optimal design

e fix our
w
How do
?
oftware
s

agile42 | The Agile Coaching Company

How do w

e know ou
r
software
is “bad”
… when it
works fin
e!

www.agile42.com |

All rights reserved. Copyright © 2007 - 2014.
Software development is not a Jenga game
• Single Responsibility Principle
• Just because you can, doesn’t mean you should.

• Open Closed Principle
• Open chest surgery is not needed when putting on a coat.

• Liskov Substitution Principle
• If it looks like a duck, quacks like a duck,

but needs batteries – you probably have the wrong abstraction

• Interface Segregation Principle
• You want me to plug this in, where?

• Dependency Inversion Principle
• Would you solder a lamp directly to the electrical wiring in a wall?

agile42 | The Agile Coaching Company

www.agile42.com |

All rights reserved. Copyright © 2007 - 2014.
Single Responsibility Principle
• A responsibility can be defined as a reason to change
!
!
!
!
!
!
!

• Just because you can, doesn’t mean you should.

agile42 | The Agile Coaching Company

www.agile42.com |

All rights reserved. Copyright © 2007 - 2014.
Open Closed Principle
• open for extension, but closed for modification
!
!
!
!
!
!
!

• Open chest surgery is not needed when putting on a coat.

agile42 | The Agile Coaching Company

www.agile42.com |

All rights reserved. Copyright © 2007 - 2014.
Liskov Substitution Principle
• use a subtype without changing the correctness of the program
!
!
!
!
!
!
!

• If it looks like a duck, quacks like a duck,

but needs batteries – you probably have the wrong abstraction
agile42 | The Agile Coaching Company

www.agile42.com |

All rights reserved. Copyright © 2007 - 2014.
Interface Segregation Principle
• states that no client should be forced to depend on methods it does not use
!
!
!
!
!
!
!

• You want me to plug this in, where?

agile42 | The Agile Coaching Company

www.agile42.com |

All rights reserved. Copyright © 2007 - 2014.
Dependency Inversion Principle
• no dependencies on implementations only on abstractions
!
!
!
!
!
!
!

• Would you solder a lamp directly to the electrical wiring in a wall?

agile42 | The Agile Coaching Company

www.agile42.com |

All rights reserved. Copyright © 2007 - 2014.
Dependency Inversion Example
class EventLogWriter
{
public void Write(string message)
{
//Write to event log here
}
}

!

class AppPoolWatcher
{
// Handle to EventLog writer to write to the logs
EventLogWriter writer = null;

!

// This function will be called when the app pool has problem
public void Notify(string message)
{
if (writer == null)
{
writer = new EventLogWriter();
}
writer.Write(message);
}
}
agile42 | The Agile Coaching Company

www.agile42.com |

All rights reserved. Copyright © 2007 - 2014.
Dependency Inversion Example

EventLogWriter
AppPoolWatcher
// This can be
watcher.Action

!

writer = new EventLogWriter();
watcher = new AppPoolWatcher();
done in some class
= writer;

// This can be done in some other class
watcher.Notify("Sample message to log");

agile42 | The Agile Coaching Company

www.agile42.com |

All rights reserved. Copyright © 2007 - 2014.
Dependency Inversion Example
class AppPoolWatcher
{
// Handle to EventLog writer to write to the logs
INofificationAction action = null;

!

!

public INofificationAction Action
{
get
{
return action;
}
set
{
action = value;
}
}
// This function will be called when the app pool has problem
public void Notify(string message)
{
action.ActOnNotification(message);
}

}
agile42 | The Agile Coaching Company

www.agile42.com |

All rights reserved. Copyright © 2007 - 2014.
Refactoring
Improving the design of existing code
Without changing the behavior

agile42 | The Agile Coaching Company

www.agile42.com |

All rights reserved. Copyright © 2007 - 2014.
Refactoring
Released in 2000, already a
golden oldie, a book I recommend
everyone to read.
!
The book defines the technique
of refactoring and is a reference
for refactoring types.

agile42 | The Agile Coaching Company

www.agile42.com |

All rights reserved. Copyright © 2007 - 2014.
Code Smells
Refactoring Cheat Codes

agile42 | The Agile Coaching Company

www.agile42.com |

All rights reserved. Copyright © 2007 - 2014.
Code Smells
• The term code smells was introduced by Kent Beck, based on an important
lesson he learned from his Grandmother about children and diapers.



„If it stinks, change it”—Grandma Beck

agile42 | The Agile Coaching Company

www.agile42.com |

All rights reserved. Copyright © 2007 - 2014.
Code Smells
• Duplicated Code

• Primitive Obsession

• Inappropriate Intimacy

• Long Method

• Switch Statements

• Large Class

• Parallel Inheritance
Hierarchies

• Alternative Classes with
Different Interfaces

• Long Parameter List
• Divergent Change
• Shotgun Surgery
• Feature Envy
• Data Clumps

agile42 | The Agile Coaching Company

• Incomplete Library Class

• Lazy Class

• Data Class

• Speculative Generality

• Refused Bequest

• Temporary Field

• Comments

• Middle Man

• Dead Code

www.agile42.com |

All rights reserved. Copyright © 2007 - 2014.
Comments and Naming
• Comments

• Rename Method

• Add comments only to explain purpose

• Extract Method

• If comments need to explain how code work, it
smells.

!

• Instead use meaningful names, e.g.:

schedule.add(Course course)

!
!

!

!

!

!

• Type Embedded in Name

• Rename Method

• Uncommunicative Name
• Inconsistent Names

agile42 | The Agile Coaching Company

www.agile42.com |

All rights reserved. Copyright © 2007 - 2014.
Excess and Duplication
• Excess
• Long Method
• Long Parameter List
• Large Class
• Long methods, large classes and endless
parameters make code unreadable.
!

• Duplicated Code

• Extract Method
• Replace Temp with Query
• Introduce Parameter Object
• Extract Class
• Extract Subclass

!
!
!
• Replace Parameter with Method
• Pull Up Field
• Form Template Method
• Substitute Algorithm

agile42 | The Agile Coaching Company

www.agile42.com |

All rights reserved. Copyright © 2007 - 2014.
The Past and the Future
• Dead Code

• Delete Code

• or worse, commented code

!

• Delete it, we have version control for that!

!

!
!

• Speculative Generality

• Collapse Hierarchy

• Don't attempt to predict future needs with
unnecessary generalization.

• Inline Class
• Remove Parameter
• Rename Method

YAGNI!

agile42 | The Agile Coaching Company

KISS

www.agile42.com |

All rights reserved. Copyright © 2007 - 2014.
Rename Method
// this method will return the maximum

void getMaximumCreditLimit() {

// credit limit.

return maximumCreditLimit;

void getIt() {

}

return _mCL;

void getInvoiceCreditLimit() {

}

int maximumCredit;

void getivcdlmt() {

float usedCredit;

int m;
float u;

=>

. .
}

. .
}

agile42 | The Agile Coaching Company

www.agile42.com |

All rights reserved. Copyright © 2007 - 2014.
Extract Method
void printOwing(double amount) {

void printOwing(double amount) {

printBanner();

printBanner();
printDetails(amount);

//print details

}

System.out.println ("name:" + _name);
System.out.println ("amount" + amount);
}

void printDetails (double amount) {

=>

System.out.println ("name:" + _name);
System.out.println ("amount" + amount);
}

agile42 | The Agile Coaching Company

www.agile42.com |

All rights reserved. Copyright © 2007 - 2014.
Replace Temp With Query
double basePrice = _quantity * _itemPrice;

if (basePrice() > 1000)

if (basePrice > 1000)

return basePrice() * 0.95;

return basePrice * 0.95;

else

else

return basePrice() * 0.98;

return basePrice * 0.98;

…
double basePrice() {

=>

return _quantity * _itemPrice;
}

agile42 | The Agile Coaching Company

www.agile42.com |

All rights reserved. Copyright © 2007 - 2014.
Introduce Parameter Object
double invoicedAmount(Date start, Date end) {

double invoicedAmount(Period range) {

double result;

double result;

for (Invoice invoice: invoices) {

for (Invoice invoice: invoices) {
if (range.contains(invoice.getDate()) {

if (invoice.getDate().after(start) ||

result += invoice.getAmount();

invoice.getDate().before(end)) {
}

result += invoice.getAmount();
}

}

return result;

}
return result;
}

=> !

}

class Period (Date start, Date end) {
. . .
boolean contains(Date date) {
return date.after(start) || date.before(end);
}
}

agile42 | The Agile Coaching Company

www.agile42.com |

All rights reserved. Copyright © 2007 - 2014.
Extract Class
class Person...
public String getName() {

class Person...
public String getName() {

return _name;
}
public String getTelephoneNumber() {
return ("(" + _officeAreaCode + ")"
+ _officeNumber);
}
String getOfficeAreaCode() {
return _officeAreaCode;
}
String getOfficeNumber() {
return _officeNumber;
}
private String _name;
private String _officeAreaCode;
private String _officeNumber;

agile42 | The Agile Coaching Company

return _name;

=> !

}
public String getTelephoneNumber(){
return _officeTelephone.getTelephoneNumber();
}
TelephoneNumber getOfficeTelephone() {
return _officeTelephone;
}
private String _name;
private TelephoneNumber _officeTelephone =
new TelephoneNumber();
class TelephoneNumber...
public String getTelephoneNumber() {
return ("(" + _areaCode + ") " + _number);
}
String getAreaCode() {
return _areaCode;
String getNumber() {
return _number;
}
private String _number;
private String _areaCode;

www.agile42.com |

All rights reserved. Copyright © 2007 - 2014.
Extract Subclass
class JobItem ...
public JobItem (int unitPrice,

class JobItem ...
public JobItem (int unitPrice) {

boolean isLabor,
Employee employee) {
_unitPrice = unitPrice;
_isLabor = isLabor;
_employee = employee;

!

}
public int getUnitPrice(){
return (_isLabor) ?
_employee.getRate():
_unitPrice;
}
...
class Employee...
public Employee (int rate) {
_rate = rate;
}
public int getRate() {
return _rate;
}
private int _rate;

_unitPrice = unitPrice;

!

=>

}
public int getUnitPrice(){
return _unitPrice;
}
...

class LaborItem ...
public LaborItem (Employee employee) {
super(0);
_employee = employee;
}
public int getUnitPrice(){
return_employee.getRate():
}
...

!

class Employee...
public Employee (int rate) {
_rate = rate;
}
public int getRate() {
return _rate;
}
private int _rate;

agile42 | The Agile Coaching Company

www.agile42.com |

All rights reserved. Copyright © 2007 - 2014.
Replace Parameter with Method
public double getPrice() {
int basePrice = _quantity * _itemPrice;
int discountLevel;
if (_quantity > 100) discountLevel = 2;
else discountLevel = 1;
double finalPrice =
discountedPrice (basePrice,discountLevel);
return finalPrice;
}

!

private double discountedPrice
(int basePrice, int discountLevel) {
if (discountLevel == 2)
return basePrice * 0.1;
else
return basePrice * 0.05;
}

agile42 | The Agile Coaching Company

public double getPrice() {
if (getDiscountLevel() == 2)
return getBasePrice() * 0.1;
else
return getBasePrice() * 0.05;
}

!

=>

private double getBasePrice() {
return _quantity * _itemPrice;
}

!

private int getDiscountLevel() {
if (_quantity > 100) return 2;
else return 1;
}

www.agile42.com |

All rights reserved. Copyright © 2007 - 2014.
Substitute Algorithm
String foundPerson(String[] people){
for (int i = 0; i < people.length; i++) {
if (people[i].equals ("Don")){
return "Don";
}
if (people[i].equals ("John")){
return "John";
}
if (people[i].equals ("Kent")){
return "Kent";
}
}
return "";
}

=>
String foundPerson(String[] people){
List candidates = Arrays.asList(new String[] {"Don", "John", "Kent"});
for (int i=0; i &lt; people.length; i++)
if (candidates.contains(people[i]))
return people[i];
return "";
}

agile42 | The Agile Coaching Company

www.agile42.com |

All rights reserved. Copyright © 2007 - 2014.
References
• SOLID:


http://en.wikipedia.org/wiki/SOLID_(object-oriented_design)
!
• SmellsToRefactorings:

http://users.csc.calpoly.edu/~jdalbey/305/Lectures/SmellsToRefactorings
!
• Martin Fowler refactoring site: http://refactoring.com/
!
• Dependency Injection: http://martinfowler.com/articles/injection.html

agile42 | The Agile Coaching Company

www.agile42.com |

All rights reserved. Copyright © 2007 - 2014.

More Related Content

KEY
JavaScript: Operators and Expressions
KEY
Effective Testing with Ruby
PDF
Why Scrum Cant Fail (Andrea Tomasini, agile42)
PDF
Agile Embedded Software Development, what's wrong with it?
PDF
Application Lifecycle Management and Agile, friends... or foes? (Andrea Toma...
PDF
OCM Java 開發人員認證與設計模式
PPTX
Advanced angular
PDF
Agility and Compliance (Andrea Tomasini, agile42)
JavaScript: Operators and Expressions
Effective Testing with Ruby
Why Scrum Cant Fail (Andrea Tomasini, agile42)
Agile Embedded Software Development, what's wrong with it?
Application Lifecycle Management and Agile, friends... or foes? (Andrea Toma...
OCM Java 開發人員認證與設計模式
Advanced angular
Agility and Compliance (Andrea Tomasini, agile42)

What's hot (6)

PDF
Crafting ColdFusion Applications like an Architect
PDF
How to get everything right... by doing everything wrong? (Andrea Tomasini, a...
PPT
Subconsultas
PDF
Commonly used design patterns
POTX
Thoughts on Component Resuse
PDF
Introduction to React Native Workshop
Crafting ColdFusion Applications like an Architect
How to get everything right... by doing everything wrong? (Andrea Tomasini, a...
Subconsultas
Commonly used design patterns
Thoughts on Component Resuse
Introduction to React Native Workshop
Ad

Viewers also liked (14)

PDF
Riding the Agile Wave
PDF
Workshop empowering teams
PDF
Be curious without judging - building a coaching structure
PDF
Understanding Scrum
ODP
Refactoring Techniques
PPTX
The Value of Refactoring on an Agile Team
PDF
Scrum Roles : Scrum Master | Product Owner |Team
PPTX
Room to Breathe: The BA's role in project estimation
PDF
Product Owner Super Powers
PPT
Agile Development Ultimate Slides
PPTX
Estimating and planning Agile projects
PDF
The Role of a BA on a Scrum Team IIBA Presentation 2010
PPTX
Release Management: Successful Software Releases Start with a Plan
PPTX
Agile Release Planning
Riding the Agile Wave
Workshop empowering teams
Be curious without judging - building a coaching structure
Understanding Scrum
Refactoring Techniques
The Value of Refactoring on an Agile Team
Scrum Roles : Scrum Master | Product Owner |Team
Room to Breathe: The BA's role in project estimation
Product Owner Super Powers
Agile Development Ultimate Slides
Estimating and planning Agile projects
The Role of a BA on a Scrum Team IIBA Presentation 2010
Release Management: Successful Software Releases Start with a Plan
Agile Release Planning
Ad

Similar to Introduction to Refactoring (20)

PPTX
Code Refactoring
PDF
Patterns, Code Smells, and The Pragmattic Programmer
PDF
Keeping code clean
PPTX
Clean Code
PDF
Refactoring: Improve the design of existing code
PPTX
Refactoring workshop
PPTX
Improving the Quality of Existing Software
PDF
[DevDay2018] Let’s all get along. Clean Code please! - By: Christophe K. Ngo,...
PPTX
Improving the Design of Existing Software
PDF
Refactoring
PDF
Agileee Developers Toolkit In The Agile World
PPTX
Improving The Quality of Existing Software
PPTX
Improving the Quality of Existing Software - DevIntersection April 2016
PPTX
Improving the Quality of Existing Software
PPTX
Improving the Quality of Existing Software
PDF
Bade Smells in Code
PDF
Guidelines to clean coding
PPT
Refactoring Tips by Martin Fowler
PPTX
Code Smells and Refactoring - Satyajit Dey & Ashif Iqbal
PPTX
Agile Development Practices - Productivity
Code Refactoring
Patterns, Code Smells, and The Pragmattic Programmer
Keeping code clean
Clean Code
Refactoring: Improve the design of existing code
Refactoring workshop
Improving the Quality of Existing Software
[DevDay2018] Let’s all get along. Clean Code please! - By: Christophe K. Ngo,...
Improving the Design of Existing Software
Refactoring
Agileee Developers Toolkit In The Agile World
Improving The Quality of Existing Software
Improving the Quality of Existing Software - DevIntersection April 2016
Improving the Quality of Existing Software
Improving the Quality of Existing Software
Bade Smells in Code
Guidelines to clean coding
Refactoring Tips by Martin Fowler
Code Smells and Refactoring - Satyajit Dey & Ashif Iqbal
Agile Development Practices - Productivity

Recently uploaded (20)

PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Electronic commerce courselecture one. Pdf
PPTX
Big Data Technologies - Introduction.pptx
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Modernizing your data center with Dell and AMD
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Empathic Computing: Creating Shared Understanding
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
cuic standard and advanced reporting.pdf
PDF
Machine learning based COVID-19 study performance prediction
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Network Security Unit 5.pdf for BCA BBA.
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Advanced methodologies resolving dimensionality complications for autism neur...
Electronic commerce courselecture one. Pdf
Big Data Technologies - Introduction.pptx
CIFDAQ's Market Insight: SEC Turns Pro Crypto
Chapter 3 Spatial Domain Image Processing.pdf
Modernizing your data center with Dell and AMD
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Encapsulation_ Review paper, used for researhc scholars
Empathic Computing: Creating Shared Understanding
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
“AI and Expert System Decision Support & Business Intelligence Systems”
cuic standard and advanced reporting.pdf
Machine learning based COVID-19 study performance prediction
Digital-Transformation-Roadmap-for-Companies.pptx
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
How UI/UX Design Impacts User Retention in Mobile Apps.pdf

Introduction to Refactoring

  • 1. Refactoring Improving the design of existing code … …. without changing the behavior! agile42 | The Agile Coaching Company www.agile42.com | All rights reserved. Copyright © 2007 - 2014.
  • 2. Why do good developers write bad software? • Project and budget pressure lead to taking shortcuts • You realize there are better ways to do something • Requirements change over time ! • When it’s hard to change update your code this lead to less optimal design e fix our w How do ? oftware s agile42 | The Agile Coaching Company How do w e know ou r software is “bad” … when it works fin e! www.agile42.com | All rights reserved. Copyright © 2007 - 2014.
  • 3. Software development is not a Jenga game • Single Responsibility Principle • Just because you can, doesn’t mean you should. • Open Closed Principle • Open chest surgery is not needed when putting on a coat. • Liskov Substitution Principle • If it looks like a duck, quacks like a duck,
 but needs batteries – you probably have the wrong abstraction • Interface Segregation Principle • You want me to plug this in, where? • Dependency Inversion Principle • Would you solder a lamp directly to the electrical wiring in a wall? agile42 | The Agile Coaching Company www.agile42.com | All rights reserved. Copyright © 2007 - 2014.
  • 4. Single Responsibility Principle • A responsibility can be defined as a reason to change ! ! ! ! ! ! ! • Just because you can, doesn’t mean you should. agile42 | The Agile Coaching Company www.agile42.com | All rights reserved. Copyright © 2007 - 2014.
  • 5. Open Closed Principle • open for extension, but closed for modification ! ! ! ! ! ! ! • Open chest surgery is not needed when putting on a coat. agile42 | The Agile Coaching Company www.agile42.com | All rights reserved. Copyright © 2007 - 2014.
  • 6. Liskov Substitution Principle • use a subtype without changing the correctness of the program ! ! ! ! ! ! ! • If it looks like a duck, quacks like a duck,
 but needs batteries – you probably have the wrong abstraction agile42 | The Agile Coaching Company www.agile42.com | All rights reserved. Copyright © 2007 - 2014.
  • 7. Interface Segregation Principle • states that no client should be forced to depend on methods it does not use ! ! ! ! ! ! ! • You want me to plug this in, where? agile42 | The Agile Coaching Company www.agile42.com | All rights reserved. Copyright © 2007 - 2014.
  • 8. Dependency Inversion Principle • no dependencies on implementations only on abstractions ! ! ! ! ! ! ! • Would you solder a lamp directly to the electrical wiring in a wall? agile42 | The Agile Coaching Company www.agile42.com | All rights reserved. Copyright © 2007 - 2014.
  • 9. Dependency Inversion Example class EventLogWriter { public void Write(string message) { //Write to event log here } } ! class AppPoolWatcher { // Handle to EventLog writer to write to the logs EventLogWriter writer = null; ! // This function will be called when the app pool has problem public void Notify(string message) { if (writer == null) { writer = new EventLogWriter(); } writer.Write(message); } } agile42 | The Agile Coaching Company www.agile42.com | All rights reserved. Copyright © 2007 - 2014.
  • 10. Dependency Inversion Example EventLogWriter AppPoolWatcher // This can be watcher.Action ! writer = new EventLogWriter(); watcher = new AppPoolWatcher(); done in some class = writer; // This can be done in some other class watcher.Notify("Sample message to log"); agile42 | The Agile Coaching Company www.agile42.com | All rights reserved. Copyright © 2007 - 2014.
  • 11. Dependency Inversion Example class AppPoolWatcher { // Handle to EventLog writer to write to the logs INofificationAction action = null; ! ! public INofificationAction Action { get { return action; } set { action = value; } } // This function will be called when the app pool has problem public void Notify(string message) { action.ActOnNotification(message); } } agile42 | The Agile Coaching Company www.agile42.com | All rights reserved. Copyright © 2007 - 2014.
  • 12. Refactoring Improving the design of existing code Without changing the behavior agile42 | The Agile Coaching Company www.agile42.com | All rights reserved. Copyright © 2007 - 2014.
  • 13. Refactoring Released in 2000, already a golden oldie, a book I recommend everyone to read. ! The book defines the technique of refactoring and is a reference for refactoring types. agile42 | The Agile Coaching Company www.agile42.com | All rights reserved. Copyright © 2007 - 2014.
  • 14. Code Smells Refactoring Cheat Codes agile42 | The Agile Coaching Company www.agile42.com | All rights reserved. Copyright © 2007 - 2014.
  • 15. Code Smells • The term code smells was introduced by Kent Beck, based on an important lesson he learned from his Grandmother about children and diapers.
 
 „If it stinks, change it”—Grandma Beck agile42 | The Agile Coaching Company www.agile42.com | All rights reserved. Copyright © 2007 - 2014.
  • 16. Code Smells • Duplicated Code • Primitive Obsession • Inappropriate Intimacy • Long Method • Switch Statements • Large Class • Parallel Inheritance Hierarchies • Alternative Classes with Different Interfaces • Long Parameter List • Divergent Change • Shotgun Surgery • Feature Envy • Data Clumps agile42 | The Agile Coaching Company • Incomplete Library Class • Lazy Class • Data Class • Speculative Generality • Refused Bequest • Temporary Field • Comments • Middle Man • Dead Code www.agile42.com | All rights reserved. Copyright © 2007 - 2014.
  • 17. Comments and Naming • Comments • Rename Method • Add comments only to explain purpose • Extract Method • If comments need to explain how code work, it smells. ! • Instead use meaningful names, e.g.:
 schedule.add(Course course) ! ! ! ! ! ! • Type Embedded in Name • Rename Method • Uncommunicative Name • Inconsistent Names agile42 | The Agile Coaching Company www.agile42.com | All rights reserved. Copyright © 2007 - 2014.
  • 18. Excess and Duplication • Excess • Long Method • Long Parameter List • Large Class • Long methods, large classes and endless parameters make code unreadable. ! • Duplicated Code • Extract Method • Replace Temp with Query • Introduce Parameter Object • Extract Class • Extract Subclass ! ! ! • Replace Parameter with Method • Pull Up Field • Form Template Method • Substitute Algorithm agile42 | The Agile Coaching Company www.agile42.com | All rights reserved. Copyright © 2007 - 2014.
  • 19. The Past and the Future • Dead Code • Delete Code • or worse, commented code ! • Delete it, we have version control for that! ! ! ! • Speculative Generality • Collapse Hierarchy • Don't attempt to predict future needs with unnecessary generalization. • Inline Class • Remove Parameter • Rename Method YAGNI! agile42 | The Agile Coaching Company KISS www.agile42.com | All rights reserved. Copyright © 2007 - 2014.
  • 20. Rename Method // this method will return the maximum void getMaximumCreditLimit() { // credit limit. return maximumCreditLimit; void getIt() { } return _mCL; void getInvoiceCreditLimit() { } int maximumCredit; void getivcdlmt() { float usedCredit; int m; float u; => . . } . . } agile42 | The Agile Coaching Company www.agile42.com | All rights reserved. Copyright © 2007 - 2014.
  • 21. Extract Method void printOwing(double amount) { void printOwing(double amount) { printBanner(); printBanner(); printDetails(amount); //print details } System.out.println ("name:" + _name); System.out.println ("amount" + amount); } void printDetails (double amount) { => System.out.println ("name:" + _name); System.out.println ("amount" + amount); } agile42 | The Agile Coaching Company www.agile42.com | All rights reserved. Copyright © 2007 - 2014.
  • 22. Replace Temp With Query double basePrice = _quantity * _itemPrice; if (basePrice() > 1000) if (basePrice > 1000) return basePrice() * 0.95; return basePrice * 0.95; else else return basePrice() * 0.98; return basePrice * 0.98; … double basePrice() { => return _quantity * _itemPrice; } agile42 | The Agile Coaching Company www.agile42.com | All rights reserved. Copyright © 2007 - 2014.
  • 23. Introduce Parameter Object double invoicedAmount(Date start, Date end) { double invoicedAmount(Period range) { double result; double result; for (Invoice invoice: invoices) { for (Invoice invoice: invoices) { if (range.contains(invoice.getDate()) { if (invoice.getDate().after(start) || result += invoice.getAmount(); invoice.getDate().before(end)) { } result += invoice.getAmount(); } } return result; } return result; } => ! } class Period (Date start, Date end) { . . . boolean contains(Date date) { return date.after(start) || date.before(end); } } agile42 | The Agile Coaching Company www.agile42.com | All rights reserved. Copyright © 2007 - 2014.
  • 24. Extract Class class Person... public String getName() { class Person... public String getName() { return _name; } public String getTelephoneNumber() { return ("(" + _officeAreaCode + ")" + _officeNumber); } String getOfficeAreaCode() { return _officeAreaCode; } String getOfficeNumber() { return _officeNumber; } private String _name; private String _officeAreaCode; private String _officeNumber; agile42 | The Agile Coaching Company return _name; => ! } public String getTelephoneNumber(){ return _officeTelephone.getTelephoneNumber(); } TelephoneNumber getOfficeTelephone() { return _officeTelephone; } private String _name; private TelephoneNumber _officeTelephone = new TelephoneNumber(); class TelephoneNumber... public String getTelephoneNumber() { return ("(" + _areaCode + ") " + _number); } String getAreaCode() { return _areaCode; String getNumber() { return _number; } private String _number; private String _areaCode; www.agile42.com | All rights reserved. Copyright © 2007 - 2014.
  • 25. Extract Subclass class JobItem ... public JobItem (int unitPrice, class JobItem ... public JobItem (int unitPrice) { boolean isLabor, Employee employee) { _unitPrice = unitPrice; _isLabor = isLabor; _employee = employee; ! } public int getUnitPrice(){ return (_isLabor) ? _employee.getRate(): _unitPrice; } ... class Employee... public Employee (int rate) { _rate = rate; } public int getRate() { return _rate; } private int _rate; _unitPrice = unitPrice; ! => } public int getUnitPrice(){ return _unitPrice; } ... class LaborItem ... public LaborItem (Employee employee) { super(0); _employee = employee; } public int getUnitPrice(){ return_employee.getRate(): } ... ! class Employee... public Employee (int rate) { _rate = rate; } public int getRate() { return _rate; } private int _rate; agile42 | The Agile Coaching Company www.agile42.com | All rights reserved. Copyright © 2007 - 2014.
  • 26. Replace Parameter with Method public double getPrice() { int basePrice = _quantity * _itemPrice; int discountLevel; if (_quantity > 100) discountLevel = 2; else discountLevel = 1; double finalPrice = discountedPrice (basePrice,discountLevel); return finalPrice; } ! private double discountedPrice (int basePrice, int discountLevel) { if (discountLevel == 2) return basePrice * 0.1; else return basePrice * 0.05; } agile42 | The Agile Coaching Company public double getPrice() { if (getDiscountLevel() == 2) return getBasePrice() * 0.1; else return getBasePrice() * 0.05; } ! => private double getBasePrice() { return _quantity * _itemPrice; } ! private int getDiscountLevel() { if (_quantity > 100) return 2; else return 1; } www.agile42.com | All rights reserved. Copyright © 2007 - 2014.
  • 27. Substitute Algorithm String foundPerson(String[] people){ for (int i = 0; i < people.length; i++) { if (people[i].equals ("Don")){ return "Don"; } if (people[i].equals ("John")){ return "John"; } if (people[i].equals ("Kent")){ return "Kent"; } } return ""; } => String foundPerson(String[] people){ List candidates = Arrays.asList(new String[] {"Don", "John", "Kent"}); for (int i=0; i &lt; people.length; i++) if (candidates.contains(people[i])) return people[i]; return ""; } agile42 | The Agile Coaching Company www.agile42.com | All rights reserved. Copyright © 2007 - 2014.
  • 28. References • SOLID:
 http://en.wikipedia.org/wiki/SOLID_(object-oriented_design) ! • SmellsToRefactorings:
 http://users.csc.calpoly.edu/~jdalbey/305/Lectures/SmellsToRefactorings ! • Martin Fowler refactoring site: http://refactoring.com/ ! • Dependency Injection: http://martinfowler.com/articles/injection.html agile42 | The Agile Coaching Company www.agile42.com | All rights reserved. Copyright © 2007 - 2014.