Refuctoring
Jason Gorman
© [Link]
The Professional’s Dilemma
expendability
time
© [Link]
What Is Refuctoring?
• Refuctoring is the process of taking a well-
designed 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.
© [Link]
Dilemma Solved – With
Refuctoring!
expendability
Indefinitely
sustainable income
time
© [Link]
Code “Smiles”
• Common sense naming conventions
• Cohesive and loosely coupled modules
• Elegant abstractions
• Lack of duplication
• A close resemblance to the application
domain
© [Link]
The Refuctoring Process
3. Call your
1. Make a single
accountant with
refuctoring
the good news
2. Run all the tests
© [Link]
Common Refuctorings
© [Link]
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;
}
}
}
}
© [Link]
Refuctoring #1 – Pig Latin
class Account { class Accountway {
class Account { class Accountway {
private float balance = 0; private oatflay alancebay = 0;
private float balance = 0; private oatflay alancebay = 0;
public void deposit(float amount) { public void epositday(oatflay amountw ay) {
public void deposit(float amount) { public void epositday(oatflay amountw ay) {
balance += amount; alancebay += amountw ay;
balance += amount; alancebay += amountw ay;
} }
} }
public void w ithdraw(float amount) { refuctored! public void ithdrawway(oatflay amountw ay) {
public void w ithdraw(float amount) { public void ithdrawway(oatflay amountw ay) {
balance -= amount; alancebay -= amountw ay;
balance -= amount; alancebay -= amountw ay;
} }
} }
public float getBalance() { public oatfflay etBalancegay() {
public float getBalance() { public oatfflay etBalancegay() {
return balance; return alancebay;
return balance; return alancebay;
} }
} }
}cv }
}cv }
© [Link]
Code Smile – Simple, Easy-to-
follow Code
public void executeTransaction() {
public void executeTransaction() {
payer.w ithdraw(amount);
payer.w ithdraw(amount);
[Link](amount);
[Link](amount);
}
}
© [Link]
public void executeTransaction() {
public void executeTransaction() {
[Link]();
[Link]();
Refuctoring #2 –
}
}
priv ate void callExecuteTransaction() {
priv ate void callExecuteTransaction() {
Treasure Hunt
[Link]();
[Link]();
}
}
.........
.........
class TransactionExecutionHelper extends ExecutionHelper {
class TransactionExecutionHelper extends ExecutionHelper {
public void doExecute() {
public void doExecute() {
[Link](this);
[Link](this);
}
}
public void execute() {
public void execute() {
ExecuteTxCommand command =
public void executeTransaction() { ExecuteTxCommand command =
public void executeTransaction() { [Link]();
[Link]();
[Link]();
[Link]();
payer.w ithdraw(amount);
payer.w ithdraw(amount); refuctored! // and so on...
// and so on...
}
[Link](amount); }
[Link](amount);
}
}
}
}
...........
...........
abstract class ExecutionHelper {
abstract class ExecutionHelper {
protected v oid doExecute(ExecutionHelper helper) {
protected v oid doExecute(ExecutionHelper helper) {
[Link]();
[Link]();
}
}
public void execute();
public void execute();
} © [Link]
}
Code Smile – Shared Understanding
* 1
Book Library
Member
© [Link]
Refuctoring #3 – Unique Modeling Language
refuctored!
© [Link]
Code Smile – Clutter-free Code
for (int number = 1; number <= 12; number++) {
for (int number = 1; number <= 12; number++) {
[Link](number + " squared is " + (number * number));
[Link](number + " squared is " + (number * number));
}
}
© [Link]
/*
/*
Author: Jason Gorman
Author: Jason Gorman
Refuctoring #4 Date & Time: 13/9/05 12:32:06
Date & Time: 13/9/05 12:32:06
– Stating The Revision History:
Revision History:
Bleeding 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
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:
number + " squared is " + (number * number)
refuctored! 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
*/
*/
[Link](number + " squared is " + (number * number));
[Link](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
} © [Link]
}
Code Smile – No Redundant Code
© [Link]
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++;
}
}
[Link]( luckyString);
[Link]( luckyString);
}
}
}
}
refuctored!
© [Link]
Code Smile – Manageable
Modules
* 1
*
*
* 1 * 1
*
*
© [Link]
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() refuctored!
twelv eMoreMonkeys()
meToo()
meThree()
andThenThereWereNone()
andBaby MakesThree()
captainScarlettDumDeeDumDeeDum()
itAintHalfHotMum()
dontPutYourDaughterOnTheStageMrsWorthington()
foo()
yung()
insert()
remove()
doTheHokeyCokey()
© [Link]
Refuctoring Metrics
Job Security Index = 1 / Maintainability
© [Link]
Further Reading
• Refuctoring To Anti-Patterns. John Q. Clockwatcher
• Mortgage-driven Development. Keith Bank-Account
• Timesheet-oriented Architecture. Jill Tax-Haven
© [Link]
[Link]
© [Link]