Lec 3 SREE
Lec 3 SREE
Reengineering
1
Understanding the
challenges of legacy
projects
2
Introduction
The average developer spends much more time working with existing code
than writing new code.
The vast majority of developers have to deal with legacy projects in some
shape or form.
3
What is a Legacy System ?
“legacy”
A sum of money, or a specified article, given to another
by will; anything handed down by an ancestor or
predecessor. — Oxford English Dictionary
5
Characteristics of Legacy
Systems
1. OLD
• Usually a project needs to exist for a few years before it gains
enough entropy to become really difficult to maintain.
• In that time, it will also go through a number of generations of
maintainers.
• With each of these handoffs, knowledge about the original
design of the system and the intentions of the previous
maintainer is also lost.
Characteristics of Legacy
Systems
2. LARGE
• It goes without saying that the larger the project is, the more
difficult it is to maintain.
• There is more code to understand, a larger number of existing
bugs (if we assume a constant defect rate in software, more code
= more bugs), and a higher probability of a new change causing
a regression, because there is more existing code that it can
potentially affect.
• The size of a project also affects decisions about how the project
is maintained.
• Large projects are difficult and risky to replace, so they are more
likely to live on and become legacy.
Characteristics of Legacy
Systems
3. INHERITED
• As is implied by the common meaning of the word legacy, these
projects are usually inherited from a previous developer or team.
• In other words, the people who originally wrote the code and
those who now maintain it are not the same individuals, and they
may even be separated by several intermediate generations of
developers.
• This means that the current maintainers have no way of knowing
why the code works the way it does, and they’re often forced to
guess the intentions and tacit design assumptions of the people
who wrote it.
Characteristics of Legacy
Systems
4. POORLY DOCUMENTED
• Given that the project spans multiple generations of
developers, it would seem that keeping accurate and
thorough documentation is essential to its long-term
survival.
• Unfortunately, if there’s one thing that developers
enjoy less than writing documentation, it’s keeping that
documentation up to date.
Legacy code
10
1.Untested, untestable code
A good test suite can function as the de facto documentation for a
project.
Not only this, but the projects are usually written without testing
in mind,
11
2.Inflexible code
A common problem with legacy code is that implementing new features or
changes to existing behavior is extremely difficult.
Imagine your application defines two types of users: Admins and Normal
users.
It’s a large and complex application, so there are a few hundred of these
checks in total, each one looking like the following.
12
Inflexible code
13
Inflexible code
One day you’re asked to add a new user type called Power User.
These users can do more than Normal users but are not as powerful as
Admins.
So for every action that Power Users are allowed to perform, you’ll have to
search through the codebase, find the corresponding if statement, and
update it to look like this.
14
Inflexible code
15
3.Code encumbered by
technical debt
Every developer is occasionally guilty of writing code that they know isn’t
Until you repay the loan by refactoring and cleaning up the code, you’ll be
burdened with interest payments, meaning a codebase that’s more difficult
to work with.
If you take out too many loans without paying them back, eventually the
interest payments will catch up with you, and useful work will grind to a halt.
16
Legacy infrastructure
Just looking at the code doesn’t show you the whole picture.
17
1.Development environment
Setting up a legacy project often involves a combination of:
18
2. Outdated dependencies
Nearly any software project has some dependencies on third-party
software.
19
Outdated dependencies
While you shouldn’t upgrade dependencies just for the sake of it,
upgrading is usually a Good Thing.
20
3.Heterogeneous
environments
Developers run the software on their local machines.
21
Legacy culture
Fear of change:
Many legacy projects are so complex and poorly documented that
even the teams charged with maintaining them don’t understand everything
about them. For example,
Which features are no longer in use and are thus safe to delete?
Which bugs are safe to fix?
Which users need to be consulted before making changes
Because of this lack of information, many teams come to respect the status
quo as the safest option, and become fearful of making any unnecessary
changes to the software.
22
Legacy culture
Knowledge silos:
Each individual developer becomes a metaphorical silo of information, with all their
valuable knowledge sitting untapped inside their heads instead of being shared for the
benefit of the whole team.
23
24
Finding your starting
point
25
Introduction
After previous lecture, you should have a clear idea of what legacy software
is and why you’d want to improve it.
Now we’ll look at how to formulate a plan for improvement and how to
measure your progress once the plan is in place
26
Overcoming feelings of fear and
frustration
Fear
Frustration.
27
Fear
Fear of code is often fear of the unknown. In a large codebase,
there are likely large bindings of code that you don’t understand
well.
It’s this unexplored territory that scares you the most, because:
28
Fear (Example)
Let’s say you’re tasked with maintaining your company’s employee time-
tracking system, codenamed TimeTrack.
It’s a legacy Java application that was developed in-house years ago,
mostly by just one developer who has now left the company.
Unfortunately he left little in the way of tests or documentation, and the only
useful document you could find was the architecture diagram in figure .
29
Fear (Example)
30
Fear (Example)
So far your only maintenance has been to add a few small features to the
web UI, so
You’d like to clean up some of the code you found in the Core component,
but you know that the payroll batches also depend on Core, and you’re
scared that you might break those.
31
Fear (Solution)
The best way to overcome this fear of the unknown is simply to dive into the
code and start playing around with it.
Open up the Core project in your IDE and try renaming methods, moving
methods between classes, introducing new interfaces, adding
comments—basically anything you can think of that makes the code cleaner
and more readable.
32
BENEFITS OF EXPLORATORY
REFACTORING
Exploratory refactoring increases your understanding of the code.
The more you explore, the more you understand (i.e. Less Fear).
33
Frustration
Working with legacy code can sometimes be very frustrating. Even the
simplest fix can involve touching 20 different instances of the same copy-
pasted code.
Adding a new feature can take days when it should really take minutes, and
trying to follow the logic of excessively complex code can be a real
mental strain.
34
Frustration (Solution)
To show the quality of the software and how that quality is changing over
time.
This may be a part of the code that is quantitatively worse than others
35
Gathering useful data about your
software
We want to gather metrics about legacy software in order to help us
answer the following questions:
What should be your next target for refactoring at any given time?
36
Gathering useful data about your
software
37
1. Bugs and coding standard
violations
Static analysis tools can analyze a codebase and detect possible bugs or
poorly written code.
Static analysis involves looking through the code (either the human-
readable source code or the machine-readable compiled code) and
flagging any pieces of code that match a predefined set of patterns or
rules.
FindBugs may flag any code that fails to close an Input Stream that it has
opened, because this may result in a resource leak and should thus be
considered a bug.
38
2. Performance
One goal of your refactoring may be to improve the performance of a legacy
system.
If you wanted to test the performance of the Audit component, you could
start with the following test:
3. Time how long it takes to process the data and generate an audit report.
39
2. Performance
40
MONITORING PERFORMANCE IN
PRODUCTION
If your software is a web application, it’s easy to collect performance
data from the production system.
Any decent web server will be able to output the processing time of
every request to a log file. You could write a simple script to
aggregate this data to calculate percentile response times per hour,
per day, and so on.
One excellent tool that can be used to measure and visualize the
performance of production systems is Kibana.
41
PMD (static analysis tool)
Another popular static analysis tool for Java is PMD.
Unlike FindBugs, PMD works by analyzing Java source code rather than
the compiled bytecode.
47
CYCLOMATIC COMPLEXITY
48
Summary
Any Questions?
50