Está en la página 1de 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 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.

© 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
1. Make a single
accountant with
refuctoring
the good news

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 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 }

© www.parlezuml.com
Code Smile – Simple, Easy-to-
follow 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 –
}
}

priv ate void callExecuteTransaction() {


priv ate void callExecuteTransaction() {

Treasure Hunt
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 =
public void executeTransaction() { ExecuteTxCommand command =
public void executeTransaction() { CommandFactory.createCommand();
CommandFactory.createCommand();
command.execute();
command.execute();
payer.w ithdraw(amount);
payer.w ithdraw(amount); refuctored! // and so on...
// and so on...
}
payee.deposit(amount); }
payee.deposit(amount);
}
}
}
}
...........
...........

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

* 1
Book 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

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
*/
*/
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() refuctored!
twelv eMoreMonkeys()
meToo()
meThree()
andThenThereWereNone()
andBaby MakesThree()
captainScarlettDumDeeDumDeeDum()
itAintHalfHotMum()
dontPutYourDaughterOnTheStageMrsWorthington()
foo()
yung()
insert()
remove()
doTheHokeyCokey()
© 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

También podría gustarte