Transcript Document

Object-Oriented Reengineering
Patterns — an Overview
Oscar Nierstrasz
Software Composition Group
www.iam.unibe.ch/~scg/
What is wrong with this picture?
JUGS — Object-Oriented Reengineering Patterns
Roadmap
Object-Oriented Legacy Systems
> Reengineering patterns
> Reengineering tools and techniques
> Focus on Change
>
© Oscar Nierstrasz
3
JUGS — Object-Oriented Reengineering Patterns
Roadmap
>
Object-Oriented Legacy Systems
— Software Evolution
— The cost of change
— The Reengineering lifecycle
Reengineering patterns
> Reengineering tools and techniques
> Focus on Change
>
© Oscar Nierstrasz
4
JUGS — Object-Oriented Reengineering Patterns
Lehman’s Laws
Continuing change
— A program that is used in a real-world environment must
change, or become progressively less useful in that
environment.
Increasing complexity
— As a program evolves, it becomes more complex, and extra
resources are needed to preserve and simplify its structure.
— Lehman and Belady, 1985
© Oscar Nierstrasz
5
JUGS — Object-Oriented Reengineering Patterns
The Dilemma of Legacy Software
A legacy system is a piece of software that:
— you have inherited, and
— is valuable to you.
You can’t afford to throw it
out, but it is too expensive
to change
Symptoms
—
—
—
—
Loss of knowledge
Architecture & design drift
Hard to make changes
…
© Oscar Nierstrasz
6
JUGS — Object-Oriented Reengineering Patterns
OO Legacy
>
Object-oriented legacy systems are successful OO
systems whose architecture and design no longer
respond to changing requirements
© Oscar Nierstrasz
7
JUGS — Object-Oriented Reengineering Patterns
The cost of change
We need to reduce the cost of change over time …
x 200
cost
cost
— cf., XP Explained
time
© Oscar Nierstrasz
time
8
JUGS — Object-Oriented Reengineering Patterns
FAMOOS Case Studies
Domain
LOC
Reengineering Goal
pipeline planning
user interface
embedded switching
mail sorting
network management
space mission
55,000
60,000
180,000
350,000
2,000,000
2,500,000
extract design
increase flexibility
improve modularity
portability & scalability
unbundle application
identify components
Different reengineering goals … but
common themes and problems !
© Oscar Nierstrasz
9
JUGS — Object-Oriented Reengineering Patterns
The Reengineering Life-Cycle
Requirements
Designs
Need appropriate
methods and tools
Code
© Oscar Nierstrasz
10
JUGS — Object-Oriented Reengineering Patterns
Roadmap
Object-Oriented Legacy Systems
> Reengineering patterns
>
— Design patterns vs. process patterns
— A Map of Reengineering Patterns
— A whirlwind tour!
Reengineering tools and techniques
> Focus on Change
>
© Oscar Nierstrasz
11
JUGS — Object-Oriented Reengineering Patterns
Reengineering Patterns
In software engineering, a design pattern is a
general solution to a common problem in software
design. A design pattern isn't a finished design that
can be transformed directly into code; it is a
description or template for how to solve a problem
that can be used in many different situations.
Reverse engineering patterns encode expertise and trade-offs in
extracting design from source code, running systems and people.
—Even if design documents exist, they are typically out of sync with
reality.
Reengineering patterns encode expertise and trade-offs in
transforming legacy code to resolve problems that have emerged.
—These problems are typically not apparent in original design but are
due to architectural drift as requirements evolve
© Oscar Nierstrasz
12
JUGS — Object-Oriented Reengineering Patterns
A Map of Reengineering Patterns
Tests: Your Life Insurance
Detailed Model Capture
Initial Understanding
First Contact
Setting Direction
© Oscar Nierstrasz
Migration Strategies
Detecting Duplicated Code
Redistribute
Responsibilities
Transform Conditionals
to Polymorphism
13
JUGS — Object-Oriented Reengineering Patterns
Setting Direction
Set direction
Agree on Maxims
Maintain
direction
Appoint a
Navigator
Coordinate
direction
Speak to the
Round Table
Where to start
Most Valuable First
What to do
Fix Problems,
Not Symptoms
Principles & Guidelines
for Software project
management especially
relevant for reengineering
projects
© Oscar Nierstrasz
What not to do
If It Ain't Broke
Don't Fix It
How to do it
Keep it Simple
14
JUGS — Object-Oriented Reengineering Patterns
Most Valuable First
Problem: Which problems should you focus on first?
Solution: Work on aspects that are most valuable to your
customer
> Maximize commitment, early results; build confidence
> Difficulties and hints:
—
—
—
—
—
Which stakeholder do you listen to?
Vs. Fix the
What measurable goal to aim for?
Buggiest First?
Consult change logs for high activity
Play the Planning Game
Wrap, refactor or rewrite? — Fix Problems, not Symptoms
© Oscar Nierstrasz
15
JUGS — Object-Oriented Reengineering Patterns
First Contact
Feasibility assessment
(one week time)
System experts
Talk with
end users
Talk with
developers
Chat with the
Maintainers
Verify what
you hear
Talk about it
Interview
during Demo
Software System
Read it
Read
about it
Compile it
Read All the Code
Skim the
Do a Mock
in One Hour
Documentation Installation
© Oscar Nierstrasz
16
JUGS — Object-Oriented Reengineering Patterns
Initial Understanding
Top down
Recover
design
Speculate about Design
understand 
Obtain a higher-level model
Analyze the
Persistent Data
Study the
Exceptional Entities
Compile it
Read it
Bottom up
© Oscar Nierstrasz
17
JUGS — Object-Oriented Reengineering Patterns
Pattern: Study the Exceptional Entities
Problem
— How can you quickly gain insight into complex software?
Solution
— Measure software entities and study the anomalous ones
Steps
— Use simple metrics
— Visualize metrics to get an overview
— Browse the code to get insight into the anomalies
© Oscar Nierstrasz
18
JUGS — Object-Oriented Reengineering Patterns
System Complexity View
System Complexity View
Nodes = Classes
Edges = Inheritance Relationships
Width = Number of Attributes
Height = Number of Methods
Color = Number of Lines of Code
© Oscar Nierstrasz
Width Metric
Height
Metric
Position
Metrics
Color
Metric
19
JUGS — Object-Oriented Reengineering Patterns
Detailed Model Capture
Tie Code and Questions
Keep track of
your understanding
Expose design
Expose the design & make
sure it remains exposed
Refactor to Understand
Expose collaborations
Step through the Execution
Expose contracts
Write Tests
to Understand
Look for the Contracts
•
•
•
•
•
Use Your Tools
Look for Key Methods
Look for Constructor Calls
Look for Template/Hook Methods
Look for Super Calls
© Oscar Nierstrasz
Expose evolution
Learn from the Past
20
JUGS — Object-Oriented Reengineering Patterns
Tests: Your Life Insurance
Write Tests to Enable Evolution
Use a Testing
Framework
Managing tests
Grow Your Test
Base Incrementally
Write Tests
to Understand
Designing
tests
Record Business
Test the Interface,
Rules as Tests
Not the Implementation
• Test Fuzzy features
• Test Old Bugs
• Retest Persistent Problems
Regression Test
after Every Change
© Oscar Nierstrasz
Migration Strategies
21
JUGS — Object-Oriented Reengineering Patterns
Migration
Build Confidence
Involve the Users
How
Why
Why
Where to
Prototype the
Target Solution
Always Have a
Running Version
Migrate Systems
Incrementally
Conserve
Familiarity
How
How
Make a Bridge
to the New Town
Use Profiler
before Optimizing
Present the
Right Interface
Deprecate
Obsolete Interfaces
Regression Test
after Every Change
© Oscar Nierstrasz
Tests, your
Life-Insurance
Distinguish Public
from Published Interfaces
22
JUGS — Object-Oriented Reengineering Patterns
Detecting Duplicated Code
Detect
Compare Code Mechanically
Understand
Visualize Code as Dotplots
Redistribute
Responsibilities
© Oscar Nierstrasz
Transform
Conditionals to
Polymorphism
23
JUGS — Object-Oriented Reengineering Patterns
Pattern: Visualize Code as Dotplots
Problem
— How can you effectively identify significant duplication in a
complex software system?
Solution
— Visualize the code as a dotplot, where dots represent
duplication.
Steps
— Normalize the source files
— Compare files line-by-line
— Visualize and interpret the dotplots
© Oscar Nierstrasz
24
JUGS — Object-Oriented Reengineering Patterns
Clone detection by string-matching
File A
Solid diagonals
indicate significant
duplication between
or within source files.
File B
File A
File B
© Oscar Nierstrasz
25
JUGS — Object-Oriented Reengineering Patterns
Dotplot Visualization
Sample Dot Configurations:
abc defa bcdef
a b c d e fa b x y e f
a bcd e a b x yc de
a x bc xd e x f g xh
Exact Copies
Copies with
Variations
Inserts/Deletes
Repetitive
Code Elements
(Helfman, 1995)
© Oscar Nierstrasz
26
JUGS — Object-Oriented Reengineering Patterns
Redistribute Responsibilities
Chains of data
containers
Monster client of
data containers
Split Up God Class
Eliminate Navigation Code
Data containers
Move Behaviour Close to Data
© Oscar Nierstrasz
27
JUGS — Object-Oriented Reengineering Patterns
High-level refactorings
Qu ickTime™ and a
No ne decompressor
are nee ded to see this pic ture.
High-level refactorings
make use of many lowlevel refactorings
© Oscar Nierstrasz
28
JUGS — Object-Oriented Reengineering Patterns
Transform Conditionals to
Polymorphism
Test provider
type
Test external
attribute
Test self type
Transform
Client Type Checks
Transform
Self Type Checks
Transform Conditionals
into Registration
Test object state
Test
null values
© Oscar Nierstrasz
Introduce
Null Object
Factor Out Strategy
Factor Out State
29
JUGS — Object-Oriented Reengineering Patterns
Roadmap
Object-Oriented Legacy Systems
> Reengineering patterns
> Reengineering tools and techniques
>
— MOOSE — A reengineering platform
— History as an entity
>
Focus on Change
© Oscar Nierstrasz
30
JUGS — Object-Oriented Reengineering Patterns
The Components of Moose
Dyna
Van
Mondrian
Hapax
...
CodeCrawler
SVN
Extensible meta model
CVS
Model repository
Smalltalk
Java
C++
…
© Oscar Nierstrasz
External
Parser
MSE
Browsing
Metrics
Querying
Grouping
Smalltalk (VW)
31
JUGS — Object-Oriented Reengineering Patterns
Moose Browser
© Oscar Nierstrasz
32
JUGS — Object-Oriented Reengineering Patterns
Moose visualizations
© Oscar Nierstrasz
33
JUGS — Object-Oriented Reengineering Patterns
Moose offers metrics and navigation
© Oscar Nierstrasz
34
JUGS — Object-Oriented Reengineering Patterns
Class Blueprint — Example
> Root Class:
—Template
—Inconsistent accessor definition
—Unused accessors
—Direct attribute access
—Template Method (DP)
> Subclasses:
—Siamese twins
—Pure overriders
© Oscar Nierstrasz
35
JUGS — Object-Oriented Reengineering Patterns
Moose offers many visualizations
© Oscar Nierstrasz
36
JUGS — Object-Oriented Reengineering Patterns
Moose is an environment
With a cast of thousands …
>
>
>
>
>
>
>
Refactoring engine
Static and dynamic
architecture recovery
Trace-based feature recovery
Concept mining
Duplication detection
Evolution analysis
…
© Oscar Nierstrasz
Tobias Aebi
Gabriela Arévalo
Mihai Balint
Frank Buchli
Thomas Bühler
Philipp Bunge
Calogero Butera
Marco D’Ambros
Serge Demeyer
Stéphane Ducasse
Fredi Frank
Michael Freidig
Tudor Gîrba
Georges Golomingi
Orla Greevy
David Gurtner
Matthias Junker
Markus Hofstetter
Marc-Philippe Horvath
Markus Kobe
Adrian Kuhn
Michele Lanza
Adrian Lienhard
Mircea Lungu
Pietro Malorgio
Michael Meyer
Laura Ponisio
Daniel Ratiu
Tamar Richner
Matthias Rieger
Daniel Schweizer
Mauricio Seeberger
Lukas Steiger
Marc Stettler
Daniele Talerico
Sander Tichelaar
Chistoph Wysseier
Ricky Wettel
41
JUGS — Object-Oriented Reengineering Patterns
Roadmap
Object-Oriented Legacy Systems
> Reengineering patterns
> Reengineering tools and techniques
> Focus on Change
>
— Concluding remarks
© Oscar Nierstrasz
42
JUGS — Object-Oriented Reengineering Patterns
Object-Oriented Reengineering
> Yes, there are object-oriented legacy
systems too!
— … which is a sign of health!
> Reverse engineering and reengineering are
essential activities in the lifecycle of any
successful software system.
— … consequently, do not consider it second class
work
> There is a large set of lightweight tools and
techniques to help you with reengineering.
— … the list is growing
> Nevertheless, people must do the job!
— … so pick them carefully and reward them
appropriately!
© Oscar Nierstrasz
43
JUGS — Object-Oriented Reengineering Patterns
Trends
>
Software is evolving ever more rapidly
— Faster, smaller hardware
–
leads to new application domains
— Globalization
–
leads to new opportunities and requirements
— The internet and mobile computing
–
raise the bar for interoperability
We cannot afford to continue to
develop software in the same old way!
© Oscar Nierstrasz
44
JUGS — Object-Oriented Reengineering Patterns
Conclusion
We need to place software evolution at the centre of our
software processes and tools.
>
Methods
— Empirical research into “best practices” to support change
–
>
Tie forward, reverse and re-engineering
Tools
— Model, analyse and transform evolving systems
–
>
Co-evolution of requirements, design and code
Programming Languages
— Software as living systems
–
© Oscar Nierstrasz
Context-aware programs
Questions?
Comments?
45