| You have been handed a project or partly | | | | case. If you have an editor that automatically |
| developed application to support. Due to prior | | | | indents 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 plain | | | | paste back into the application. If you do not have |
| laziness from some previous developers you have | | | | an editor and will be working with this code for |
| been handed a maintenance nightmare. What | | | | some 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 rewrite | | | | sounds. Basically all the script needs to do is count |
| or refactor what is basically a mess, so small | | | | and 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 or | | | | lines of code it reads in until it finds a Wend |
| help-desk calls that provide an overview of | | | | statement. |
| problems and priorities? | | | | If an application's existing code is particularly ugly |
| 2. Does the code have exception or error | | | | and your job is only to add functionality to the |
| handling? | | | | application you may consider either removing the |
| 3. How appropriately named are the variables? I | | | | old code to a library so you will not have to look |
| have seen code with variables named aThingList | | | | at 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 it | | | | to modify your interface should you need to |
| easy to understand the flow of instructions? I | | | | make changes in the future. A added bonus is |
| have seen code that was completely left-aligned | | | | that 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 loops | | | | behind your it. |
| within the code? | | | | == INFINITE DO, WHILE, FOR LOOPS == |
| 6. Have you been left adequately written | | | | My pet hate! If the code is not inextricably |
| documentation about the application and any code | | | | entwined 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 reported | | | | algorithms from the loop statements and placing |
| application problems. This often means finding the | | | | the functionality within separate, smaller routines. |
| algorithmic cause of problems. Ideally you will be | | | | Document your changes via comments within the |
| able to run a script over an uncompiled version of | | | | code or preferably use version tracking software |
| the application and replicate the error. Make a | | | | to capture your changes. |
| copy of any data sources the applications uses so | | | | == PROCESS IMPROVEMENT == |
| that you can safely manipulate data without | | | | Before embarking upon a plan of gradual |
| affecting the live system the users are using. If | | | | improvement you need to do some investigation |
| you can break into the code during an error or an | | | | and find out whether there are any projects |
| exception then do so and step through the code | | | | underway 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 the | | | | time and effort patching up an application that will |
| application is running then you will have to rely | | | | soon be replaced or defunct. Perhaps there are |
| upon tracing code movement via a message | | | | addins to other applications that can provide the |
| notification, either using message boxes, writing to | | | | same service to the users. Your goal is to get rid |
| a file or writing the algorithm results to a | | | | of the application if you can or reduce its negative |
| makeshift debug screen. A notification at the | | | | impact upon workload (due to badly written |
| entrance of a routine which includes the routine | | | | algorithms) 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 is | | | | is 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 way | | | | If you cannot rid yourself of the application then |
| to solve this problem is to improve data quality at | | | | write down some steps of improvement, for |
| the datasource. Perhaps an uploaded data file is | | | | example; introduce exception/error-handling where |
| not up to scratch. Perhaps the user is entering an | | | | it is lacking. Break down seriously long routines. |
| unacceptable value. Even though the latter could | | | | Rewrite extremely bad legacy code that fails to |
| be blamed upon the application's lack of built-in | | | | live up to expectations. Remove ugly code to a |
| ability to check user input, decisions made during | | | | separate library and access it's functionality by a |
| it's previous development phase could have put | | | | cleanly written interface. Perhaps you could |
| the responsibility upon the user to enter correctly | | | | introduce user input testing or format masks to |
| formatted data which would make this a training | | | | force 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 without | | | | and inadequate testing of SQL string results. |
| any kind of exception/error-handling. You need to | | | | Provide users with data dumps in comma |
| enter exception/error-handling into routines you | | | | separated 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 display | | | | software ( |
| the routine name, error number and error | | | | Do not be shy in discussing with your users the |
| message. For more information upon error | | | | fact 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 at | | | | you. 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 a | | | | ask them for their thoughts and ideas. Remind |
| situation where the variable names are less than | | | | them that it will be a gradual process and involve |
| adequately descriptive of their purpose. Search | | | | them in the development and testing. This is called |
| and replace functionality may help you but usually | | | | the winning of hearts and minds! This concepts |
| will not discriminate between variables sharing the | | | | works because it is an inherently genuine and |
| same name, in different areas of the cod,e that | | | | honest approach to improving the situation for |
| are used for different purposes. In fact, the | | | | your users and yourself. |
| renaming of an ambiguous name to a more | | | | Do not be disheartened by any user's cynicism |
| purposeful name may complicate the | | | | towards the services and plans you plan to offer |
| understanding of the code where other variables | | | | them; When an application produces bad results |
| of the same name are affected. The best | | | | for any period of time, prior promises upon which |
| practice is to write a comment next to the | | | | the previous developer was supposed to deliver |
| variable explaining it's use for any particular routine | | | | have 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 either | | | | improvement is slowed because of it. |