Maintaining Badly Written Legacy Code

You have been handed a project or partlycase. If you have an editor that automatically
developed application to support. Due to priorindents code based upon language, cut and paste
deadlines, restricted budgets, limited developer skill,the code into your editor, realign the code and
multi-developer miscommunication or just plainpaste back into the application. If you do not have
laziness from some previous developers you havean editor and will be working with this code for
been handed a maintenance nightmare. Whatsome time, perhaps a quickly typed script to
steps can you take to improve this situation.clean the code is in order. This is not as hard as it
It is likely that you will not get the time to rewritesounds. Basically all the script needs to do is count
or refactor what is basically a mess, so smalland add tab spaces whilst reading in each line of
steps and a plan of action is needed:the code. If for example your script finds a While
statement it adds a tab to the front of the next
1. Do you have a prior list of complaints orlines of code it reads in until it finds a Wend
help-desk calls that provide an overview ofstatement.
problems and priorities?If an application's existing code is particularly ugly
2. Does the code have exception or errorand your job is only to add functionality to the
handling?application you may consider either removing the
3. How appropriately named are the variables? Iold code to a library so you will not have to look
have seen code with variables named aThingListat it or build a module as an interface to the old
and aColList.code. The benefit of this is that you will only have
4. Is the code laid out in a way that makes itto modify your interface should you need to
easy to understand the flow of instructions? Imake changes in the future. A added bonus is
have seen code that was completely left-alignedthat you can comment your interface and
making reading to understand it extremely difficult.remove your reputation from the extant disaster
5. Is there an infinite number of exponential loopsbehind your it.
within the code?== INFINITE DO, WHILE, FOR LOOPS ==
6. Have you been left adequately writtenMy pet hate! If the code is not inextricably
documentation about the application and any codeentwined within looping statements then try to
and design decisions that were made.reduce the complexity of a routine by extracting
First you need to tackle any commonly reportedalgorithms from the loop statements and placing
application problems. This often means finding thethe functionality within separate, smaller routines.
algorithmic cause of problems. Ideally you will beDocument your changes via comments within the
able to run a script over an uncompiled version ofcode or preferably use version tracking software
the application and replicate the error. Make ato capture your changes.
copy of any data sources the applications uses so== PROCESS IMPROVEMENT ==
that you can safely manipulate data withoutBefore embarking upon a plan of gradual
affecting the live system the users are using. Ifimprovement you need to do some investigation
you can break into the code during an error or anand find out whether there are any projects
exception then do so and step through the codeunderway that could replace the application you
line by line until you find the error.have been handed. You do not want to waste
If you cannot break into the code whilst thetime and effort patching up an application that will
application is running then you will have to relysoon be replaced or defunct. Perhaps there are
upon tracing code movement via a messageaddins to other applications that can provide the
notification, either using message boxes, writing tosame service to the users. Your goal is to get rid
a file or writing the algorithm results to aof the application if you can or reduce its negative
makeshift debug screen. A notification at theimpact upon workload (due to badly written
entrance of a routine which includes the routinealgorithms) by farming out its tasks to better
name and the time entered would be useful.written and more stable applications. Maintenance
Once you have determined where the error isis an expensive and time-consuming process for
occurring you will need to determine why?badly written applications.
Is the data causing the problem? The best wayIf you cannot rid yourself of the application then
to solve this problem is to improve data quality atwrite down some steps of improvement, for
the datasource. Perhaps an uploaded data file isexample; introduce exception/error-handling where
not up to scratch. Perhaps the user is entering anit is lacking. Break down seriously long routines.
unacceptable value. Even though the latter couldRewrite extremely bad legacy code that fails to
be blamed upon the application's lack of built-inlive up to expectations. Remove ugly code to a
ability to check user input, decisions made duringseparate library and access it's functionality by a
it's previous development phase could have putcleanly written interface. Perhaps you could
the responsibility upon the user to enter correctlyintroduce user input testing or format masks to
formatted data which would make this a trainingforce consistent data entry.
issue not a data issue.Sometimes an application produces reports with
== ERROR-HANDLING ==spurious results due to poorly devised calculations
I have seen numerous applications' code withoutand inadequate testing of SQL string results.
any kind of exception/error-handling. You need toProvide users with data dumps in comma
enter exception/error-handling into routines youseparated value (csv) files so they can build their
know have errors and extraneously long routines.own reports using spreadsheets and charting
Within error notification you will need to displaysoftware (
the routine name, error number and errorDo not be shy in discussing with your users the
message. For more information upon errorfact that you have been handed a lemon of an
handling see my article Exception/Error Handling,application, they will more than likely agree with
An Integral Part of Programming atyou. Inform them on how you intend to
== BADLY NAMED VARIABLES ==implement a plan of process improvement and
Usually there is not much you can do about aask them for their thoughts and ideas. Remind
situation where the variable names are less thanthem that it will be a gradual process and involve
adequately descriptive of their purpose. Searchthem in the development and testing. This is called
and replace functionality may help you but usuallythe winning of hearts and minds! This concepts
will not discriminate between variables sharing theworks because it is an inherently genuine and
same name, in different areas of the cod,e thathonest approach to improving the situation for
are used for different purposes. In fact, theyour users and yourself.
renaming of an ambiguous name to a moreDo not be disheartened by any user's cynicism
purposeful name may complicate thetowards the services and plans you plan to offer
understanding of the code where other variablesthem; When an application produces bad results
of the same name are affected. The bestfor any period of time, prior promises upon which
practice is to write a comment next to thethe previous developer was supposed to deliver
variable explaining it's use for any particular routinehave been broken and it is the user who suffers
within which it is found.the results of those broken promises.
== CRUMMILY LAID OUT CODE ==Lastly! Expedient support to users' problems will
Either the code is all left-aligned or dischevelled:be extremely appreciated even if your plan of
Reading badly laid out code is difficult in eitherimprovement is slowed because of it.