phparchitect-2019-08

(Rick Simeone) #1
http://www.phparch.com \ August 2019 \ 19

How to Deal With Legacy Code

The way to win is to take the sticks from
the top, which are not blocked by others,
and in small steps, we can get to the last
one. The Mikado Method is also a way
to refactor code. It’s not often enough
to easily refactor one, deeply nested
class without digging out half the app.
This case is where the Mikado Method
comes in handy. It’s based on drawing
a tree of dependencies between classes
that starts from the target class on which
we care the most. Then, we can quickly
identify those classes that have no other
dependencies and on which no other
classes depend. We should start refac-
toring them and then remove them
from the codebase. The process repeats
until you reach the target class, which
in this case is the root in a tree. The
whole process can be time-consuming,
but it is certainly a lot safer and more
secure than starting with the root itself.
This makes it all the more effective if we
have to deal with spaghetti-style code.
If, after the changes, something stops
working, we can quickly withdraw
from the last step and try a different
approach. An additional advantage is
the stability of the code long before the
process is completed. We change only
one thing, and each can be committed,
tested, and deployed.
The same approach can be used for
more general purposes, not for specific
classes. We write down the main objec-
tive, and then we add requirements to
achieve this objective, requirements to
those requirements, and so on. Again,
we start with those things that do not
have additional requirements or depen-
dencies.


What’s Good About Legacy?
Most of the things I mentioned, you
don’t experience if you are working
on a new codebase: introducing tests
to untested code, refactoring without
complete rewriting, working with code
that works or not, and in both cases,
you don’t know why.
The fact that I am currently involved
in programming has its roots in the first
computer games I played. The interest
in games evolved into computers,
programming, and then it was all

downhill. I like computer games to
this day. One of my favorite titles is Sid
Meier’s Civilization. After many times
playing the game, I thought I knew the
rules and what to do to win. And then
comes the scenario option. There was
no question of building an empire from
scratch without top-down restrictions.
At the beginning of the scenario, you
have some limitations imposed on you,
and you must find a way out of it still.
Only then is it a situation that tests your
skills and allows you to develop them.
I see legacy applications as a challenge.
I have some knowledge, some experi-
ence, but when I get to the next legacy
project, I can expect anything which is
a fascinating thing for me.
Someone once told me that he had
been writing REST APIs for a year
and a half. “API?” I asked. “Yes, APIs
in various microservices,” he replied. I
guess he had to be good at what he was
doing. I don’t know how many times
you can write the same thing from
scratch in 18 months. After some time
you can do it with your eyes closed.
By then, maybe it is for someone
else’s legacy code anyway. If you keep
repeating projects from scratch time
after time, you can learn a lot. After a
while, you hit a ceiling you can’t easily
break through. Working consciously
with a legacy application is a way

out of your comfort zone. In such a
situation, it makes you look for new
solutions, raises the bar, and forces you
to develop constantly. I think that my
skills have significantly improved after
having deliberately worked with legacy
apps. Refactoring is not the only way
to improve the quality of the code. The
longer you work with a given code, the
more you understand it, and the more
you can justify why a given solution
may work better than others.

“A journey of a thousand miles
begins with a single step.”
Chinese proverb

It sounds cliché, but how simple is it
in practice. Going through successive
classes of dependencies, I try to leave the
whole thing in a slightly better condi-
tion than I found them. I start to work
by improving one thing today, another
thing tomorrow, and so on. After a few
months, you can see how much has
changed. When I talked to my friends
about the advantages of working with
legacy code, they most often indicated
the possibility of improving such code.
I sincerely hope the next time you get
a project with legacy code, you consider
it a challenge. Let it be the moment to
put your existing skills to the test, and
you have a chance to acquire new ones.

Paweł Lewtak is developer with 10 years of experience with
web-based applications. He created them from scratch from
analysis and writing specification up to deployment on
production. Big fan of continuous learning and continuous
improvement. @pawel_lewtak

Related Reading


Free download pdf