You are on page 1of 22

Refuctoring

Jason Gorman

© www.parlezuml.com

The Professional’s Dilemma
expendability

time

© www.parlezuml.com

What Is Refuctoring?
• Refuctoring is the process of taking a welldesigned piece of code and, through a series of small, reversible changes, making it completely unmaintainable by anybody except yourself. • Comprehensive regression testing guarantees that nobody will be any the wiser.
© www.parlezuml.com

Dilemma Solved – With Refuctoring!
expendability Indefinitely sustainable income

time

© www.parlezuml.com

Code “Smiles”
• • • • • Common sense naming conventions Cohesive and loosely coupled modules Elegant abstractions Lack of duplication A close resemblance to the application domain

© www.parlezuml.com

The Refuctoring Process

3. Call your accountant with the good news

1. Make a single refuctoring

2. Run all the tests

© www.parlezuml.com

Common Refuctorings

© www.parlezuml.com

Code Smile – Common Sense Naming Conventions
class Account { class Account { private float balance = 0; private float balance = 0; public void deposit(float amount) { public void deposit(float amount) { balance += amount; balance += amount; } } public void w ithdraw(float amount) { public void w ithdraw(float amount) { balance -= amount; balance -= amount; } } public float getBalance() { public float getBalance() { return balance; return balance; } } } }

© www.parlezuml.com

Refuctoring #1 – Pig Latin
class Account { class Account { private float balance = 0; private float balance = 0; public void deposit(float amount) { public void deposit(float amount) { balance += amount; balance += amount; } } public void w ithdraw(float amount) { public void w ithdraw(float amount) { balance -= amount; balance -= amount; } } public float getBalance() { public float getBalance() { return balance; return balance; } } }cv }cv } } class Accountway { class Accountway { private oatflay alancebay = 0; private oatflay alancebay = 0; public void epositday(oatflay amountw ay) { public void epositday(oatflay amountw ay) { alancebay += amountw ay; alancebay += amountw ay; } }

refuctored!

public void ithdrawway(oatflay amountw ay) { public void ithdrawway(oatflay amountw ay) { alancebay -= amountw ay; alancebay -= amountw ay; } } public oatfflay etBalancegay() { public oatfflay etBalancegay() { return alancebay; return alancebay; } }

© www.parlezuml.com

Code Smile – Simple, Easy-tofollow Code
public void executeTransaction() { public void executeTransaction() { payer.w ithdraw(amount); payer.w ithdraw(amount); payee.deposit(amount); payee.deposit(amount); } }

© www.parlezuml.com

public void executeTransaction() { public void executeTransaction() { this.callExecuteTransaction(); this.callExecuteTransaction(); }

Refuctoring #2 – Treasure Hunt

}

priv ate void callExecuteTransaction() { priv ate void callExecuteTransaction() { helper.doExecute(); helper.doExecute(); } } ......... ......... class TransactionExecutionHelper extends ExecutionHelper { class TransactionExecutionHelper extends ExecutionHelper { public void doExecute() { public void doExecute() { base.doExecute(this); base.doExecute(this); } } public void execute() { public void execute() { ExecuteTxCommand command = ExecuteTxCommand command = CommandFactory.createCommand(); CommandFactory.createCommand(); command.execute(); command.execute(); // and so on... // and so on... } }

public void executeTransaction() { public void executeTransaction() { payer.w ithdraw(amount); payer.w ithdraw(amount); payee.deposit(amount); payee.deposit(amount); } }

refuctored!
} }

........... ........... abstract class ExecutionHelper { abstract class ExecutionHelper { protected v oid doExecute(ExecutionHelper helper) { protected v oid doExecute(ExecutionHelper helper) { helper.execute(); helper.execute(); } } public void execute(); public void execute();

}

}

© www.parlezuml.com

Code Smile – Shared Understanding

Book

*

1

Library
* *

Member

© www.parlezuml.com

Refuctoring #3 – Unique Modeling Language

?

refuctored!
© www.parlezuml.com

Code Smile – Clutter-free Code

for (int number = 1; number <= 12; number++) { for (int number = 1; number <= 12; number++) { System.out.println(number + " squared is " + (number * number)); System.out.println(number + " squared is " + (number * number)); } }

© www.parlezuml.com

/* /* Author: Jason Gorman Author: Jason Gorman Date & Time: 13/9/05 12:32:06 Date & Time: 13/9/05 12:32:06 Revision History: Revision History: 13/9/05 13/9/05 12:33:14 12:33:14 Accidentally deleted a line then used "undo" to Accidentally deleted a line then used "undo" to bring it back again bring it back again

Refuctoring #4 – Stating The Bleeding Obvious

Comment Body: Comment Body: Declare an integer called number w ith an initial value of 1, and Declare an integer called number w ith an initial value of 1, and then perform the same block of code 12 times, incrementing the then perform the same block of code 12 times, incrementing the value of number by 1 each time for the purposes of computation value of number by 1 each time for the purposes of computation */ */ for (int number = 1; number <= 12; number++) { for (int number = 1; number <= 12; number++) { /* /* get the System's output stream to print the follow ing text: get the System's output stream to print the follow ing text:

refuctored!

number + " squared is " + (number * number) number + " squared is " + (number * number) for the purposes of seeing w hat the square of number is for the purposes of seeing w hat the square of number is */ */ System.out.println(number + " squared is " + (number * number)); System.out.println(number + " squared is " + (number * number)); // use the { character to let the compiler know where the for loop ends // use the { character to let the compiler know where the for loop ends } © www.parlezuml.com }

Code Smile – No Redundant Code

© www.parlezuml.com

Refuctoring # 5 – Rainy Day Module
class SpareCode { class SpareCode { private int spareInteger; private int spareInteger; private String luckyString; private String luckyString; private bool youNeverKnow ; private bool youNeverKnow ; public void spareLogic() { public void spareLogic() { spareInteger = 1; spareInteger = 1; if( youNeverKnow ) { if( youNeverKnow ) { spareInteger++; spareInteger++; } } } } } System.out.println( luckyString); System.out.println( luckyString); }

refuctored!

© www.parlezuml.com

Code Smile – Manageable Modules
* 1

* * * 1 * 1

* *

© www.parlezuml.com

Refuctoring # 6 – Module Gravity Well
Application
doStuff() openWindows() connectToDatabase() blah() etc() kitchenSink() ev ery Man() andHisDog() inForAPenny() any PortInAStorm() makeHayWhileTheSunShines() aRollingStoneGathersNoMoss() didY ouSeeDoctorWhoLastNight() iAmTheWalrus() areWeThereY et() method() madness() wakaJawaka() aHooliHay liHah() andAPartridgeInAPairTree() twelv eMonkeys() twelv eMoreMonkeys() meToo() meThree() andThenThereWereNone() andBaby MakesThree() captainScarlettDumDeeDumDeeDum() itAintHalfHotMum() dontPutYourDaughterOnTheStageMrsWorthington() foo() yung() insert() remove() doTheHokeyCokey()

refuctored!

© www.parlezuml.com

Refuctoring Metrics

Job Security Index = 1 / Maintainability

© www.parlezuml.com

Further Reading
• Refuctoring To Anti-Patterns. John Q. Clockwatcher • Mortgage-driven Development. Keith Bank-Account • Timesheet-oriented Architecture. Jill Tax-Haven

© www.parlezuml.com

www.parlezuml.com

© www.parlezuml.com