60
Restructuring Tudor Gîrba www.tudorgirba.com

Restructuring (EVO 2008)

Embed Size (px)

DESCRIPTION

I used this set of slides at a lecture I gave at the University of Bern.

Citation preview

Page 1: Restructuring (EVO 2008)

Restructuring

Tudor Gîrbawww.tudorgirba.com

Page 2: Restructuring (EVO 2008)

reve

rse

engin

eerin

gforward engineering

}

{

}

{

}

{}

{

}

{

actual development }

{

}

{

}

{

}

{

Page 3: Restructuring (EVO 2008)

restructuring

reve

rse

engin

eerin

gforward engineering

}

{

}

{

}

{

}

{}

{

}

{

}

{}

{

}

{

Page 4: Restructuring (EVO 2008)

restructuring

reve

rse

engin

eerin

gforward engineering

}

{

}

{

}

{

}

{}

{

}

{

}

{}

{

}

{

Reengineering life cycle

Page 5: Restructuring (EVO 2008)

restructuring }

{

}

{

}

{

}

{}

{

}

{

}

{}

{

}

{

What?

Page 6: Restructuring (EVO 2008)

Restructuring is often taken as a synonym of refactoring

Page 7: Restructuring (EVO 2008)

Refactoring is a disciplined technique for restructuring an existing body of code, altering its internal structure without changing its external behavior

Page 8: Restructuring (EVO 2008)

Refactoring isbehavior-preservingtransformation

Page 9: Restructuring (EVO 2008)

Restructuring is transforminga program to fit current needs

Page 10: Restructuring (EVO 2008)

restructuring }

{

}

{

}

{

}

{}

{

}

{

}

{}

{

}

{

What?Why?

Page 11: Restructuring (EVO 2008)

Software should behabitable

Page 12: Restructuring (EVO 2008)
Page 13: Restructuring (EVO 2008)

Markus Denker

The secret to tidiness is to have aplace for every thing

Page 14: Restructuring (EVO 2008)

Markus Denker

The secret to tidiness is to find the rightplace for every thing

Page 15: Restructuring (EVO 2008)

Grandma Beck

If it stinks, change it

Page 16: Restructuring (EVO 2008)

restructuring }

{

}

{

}

{

}

{}

{

}

{

}

{}

{

}

{

What?Why?How?

Page 18: Restructuring (EVO 2008)

Take a critical look at design

Page 19: Restructuring (EVO 2008)

Lanza, Marinescu 2006

A God Class centralizes too much intelligence

ATFD > FEW

Class uses directly more than a

few attributes of other classes

WMC ! VERY HIGH

Functional complexity of the

class is very high

TCC < ONE THIRD

Class cohesion is low

AND GodClass

Page 20: Restructuring (EVO 2008)

ModelFacadefrom ArgoUML

Page 21: Restructuring (EVO 2008)

ModelFacadefrom ArgoUML

Demeyer etal 2002

Split up God Class

Page 22: Restructuring (EVO 2008)

Demeyer etal 2002

Easier said than done

Page 23: Restructuring (EVO 2008)

Demeyer etal 2002

You can also wrap it

Page 24: Restructuring (EVO 2008)

Ratiu etal, 2004

AND

isGodClass(last)

God Class

in the last version

Stability > 90%

Stable throughout

the history

Harmless God Class

Maybe you do not

need to touch it at all

Page 25: Restructuring (EVO 2008)

Client

service()

Provider

Page 26: Restructuring (EVO 2008)

Client

getProvider()

Intermediary

service()

Provider

Page 27: Restructuring (EVO 2008)

Client

getProvider()

Intermediary

service()

Provider

this.intermediary.provider.service();

Page 28: Restructuring (EVO 2008)

Client

getProvider()

Intermediary

service()

Provider

this.intermediary.provider.service();

this.intermediary.provider2.service2();

this.intermediary.provider3.service3();

service2()

Provider2

service3()

Provider3

Page 29: Restructuring (EVO 2008)

Client

getProvider()

Intermediary

service()

Provider

this.intermediary.provider.service();

this.intermediary.provider2.service2();

this.intermediary.provider3.service3();

service2()

Provider2

service3()

Provider3

The Law of Demeter: Don’t talk to strangers

Page 30: Restructuring (EVO 2008)

Demeyer etal 2002

Client

fullService()

Intermediary

service()

Provider

this.intermediary.fullService();

service2()

Provider2

service3()

Provider3

Eliminate Navigation Code

Page 31: Restructuring (EVO 2008)

Demeyer etal 2002

Client

fullService()

Intermediary

service()

Provider

this.intermediary.fullService();

service2()

Provider2

service3()

Provider3

Caveat: This can lead to huge interfaces

Page 32: Restructuring (EVO 2008)

Fowler 1999

Client

service()

Provider

this.provider.service();

this.provider2.service2();

this.provider3.service3();

service2()

Provider2

service3()

Provider3

Remove Middle Man

Page 33: Restructuring (EVO 2008)

Lanza, Marinescu 2006

An Envious Method is more interestedin data from a handful of other classes

ATFD > FEW

Method uses directly more than

a few attributes of other classes

LAA < ONE THIRD

Method uses far more attributes

of other classes than its own

FDP ! FEW

The used "foreign" attributes

belong to very few other classes

AND Feature Envy

Page 34: Restructuring (EVO 2008)
Page 35: Restructuring (EVO 2008)

Move Behavior Closer to Data

Page 36: Restructuring (EVO 2008)

Lanza, Marinescu 2006

Data Classes are dumb data holders

WOC < ONE THIRD

Interface of class reveals data

rather than offering services

AND Data Class

Class reveals many attributes and is

not complex

Page 37: Restructuring (EVO 2008)
Page 38: Restructuring (EVO 2008)

Move Behavior Closer to Data

Page 39: Restructuring (EVO 2008)

Lanza, Marinescu 2006

Significant Duplication

Page 40: Restructuring (EVO 2008)

Lanza, Marinescu 2006

m2

m1

m1'

m2'

m3

AA

Page 41: Restructuring (EVO 2008)

Lanza, Marinescu 2006

m1

B C

m2

A

m1

B C

m2

A

m3

Page 42: Restructuring (EVO 2008)

Restructuring can also improve performance

Page 43: Restructuring (EVO 2008)

Client

BusinessObject

BusinessSessionBean

BusinessEntityBean

Page 44: Restructuring (EVO 2008)

java.sun.com

Client

BusinessObject

BusinessSessionBean

BusinessEntityBean

SessionFacade

Page 45: Restructuring (EVO 2008)

Not all Facades are good,

some are God :)

ModelFacadefrom ArgoUML

Page 46: Restructuring (EVO 2008)

Anonymous

Trouble never comes alone

Page 48: Restructuring (EVO 2008)

EliminateNavigation Code

Split upGod Class

Move BehaviorCloser to Data

Redistributeresponsibilities

Transform conditionalsto Polymorphism

Transform ClientType Checks

Introduce Null Object

...

Page 49: Restructuring (EVO 2008)

inCode correlates problems

Page 51: Restructuring (EVO 2008)
Page 52: Restructuring (EVO 2008)

restructuring }

{

}

{

}

{

}

{}

{

}

{

}

{}

{

}

{

What?Why?How?When?

Page 53: Restructuring (EVO 2008)

Restructure all the time

Page 54: Restructuring (EVO 2008)

Restructure all the time

… hmm, is this feasible?

Page 55: Restructuring (EVO 2008)
Page 56: Restructuring (EVO 2008)

It is feasible

Page 57: Restructuring (EVO 2008)

Development Maintenance

Page 58: Restructuring (EVO 2008)
Page 59: Restructuring (EVO 2008)

Software evolution