The Knights of Legacy Code

DZone 's Guide to

The Knights of Legacy Code

Laszio Nardai of LogMein covers the knights (i.e. the reactions of team members) to legacy code, and what you can learn from all of them.

· Java Zone ·
Free Resource

Let's set the context first. Your team is in mid development of a new module, which depends on the obligatory legacy core application that your company spent serious amount of time and money to develop. The x week long iteration is starting to get out of hand, but it's still manageable. You need to check someting in the old codebase, and you stumble upon a function like this:

public String getDomainForProxy(String domain, String localhost, boolean local) {
  int portIndex = domain.indexOf(':');
  if(portIndex >= 0 && (portIndex == domain.length() - 5) && (!domain.startsWith(PRODUCTION_DOMAIN) && !appContext.isDevelopmentEnv()) || local) {
    if(domain.indexOf(PRODUCTION_DOMAIN) > 0) {
      return domain.substring(4, 6) + domain.substring(portIndex + 2);
   return domain.substring(portIndex + 3);
  return null;

What kind of reactions are the most common? How do you yourself react? Let's see the options.

White Knight

He thinks this is unacceptable! Also not a single unit test? Starts to wonder how the application keeps working with code like this under the hood. Immediately starts refactoring this, and uses a couple other methods he finds along this class.

Dark Knight

Has no reaction, other than this is the way in our legacy codebase. He has tried his luck before, and it did not go well. He thinks that any effort put into this part of the code will make no difference, other then the possibility of breaking the app. 

Silver Knight

He gets bugged that this piece of code takes more time to understand than usual. Usually, he takes pride in explaining how things work around this enterprise masterpiece. He looks around in the codebase, takes time to understand what exactly happening, but leaves afterwards.

Golden Knight

The jokster. Raises awarness inside the whole team, makes jokes about it, brags that he creates better quality work even when he has a hangover, but does nothing useful at the end.

Ancient Knight

He remembers when he wrote that code many years ago. Three days before the final deadline, the Devops team just tossed back the application. A half year work of "improvement," and it only served every third request with the new Cluster setup, which was a total showstopper. Remembers the triumph he got after the marvelous quick fix.

Probably every software developer can relate to one or more knights in this story. He can also categorize his fellow team members, how they behave in their hour of need. And believe me, if a codebase where examples like this can be found, it really needs attention. 

The moral of the story here, is that none of the knights are absolutely right nor wrong. You can learn valuable lessons from each.

The White Knight represent how a professional software developer should act, but he's fault is that he's a bit overzealous. You have to take into account the current context. Remember when I said that the iteration is getting out of hand? This means that 90 percent of the time it will. Also, if you take a closer look at the function name, getDomainForProxy, sounds pretty serious, right? You really need to think it through, talk to QA and devOps before diving in.

The Dark Knight is a seasoned veteran. His strength comes from experience and caution. He knows how easy is to break an application, especially if it's legacy and lacks tests. I assure you, he has seen many getters, that does some manipulation in the DB layers deeper. Sadly, with experience also comes hopelessness and bad morale. 

Silver Knight is also valuable. He's a mixture of the dark and white, cautious like the Dark Knight, but still has hope for something better. Also, his lust for knowledge is admirable, and many times he will add invaluable little hints in meetings, code reviews and discussions.

And, yes, even the Golden Knight has some value, who only makes jokes at a certain situations. After all, we are human beings, and when you see a class with more then 6000 lines of code, what else can you do, then swallow your tears, and have a good laugh with your colleagues.

The Ancient Knight teaches us that desperate times call for desperate measures. The code up there can be either a work of a genius who was under pressure or a lazy developer with the lack of knowledge. His fault is that if you commit to a debt like this, you have to make sure it will be cleaned up later.

A healthy team should be heterogeneous. They all have good qualities which may come in handy,  and faults that you have to look out for.  

To sum it up, there is never an ultimate truth or best attitude for every situation. You have to take into account the context you are working  in. You have to rely on your teammates, because they surely have qualities that you lack and can prevent you from making mistakes, help your mood, or learn important lessons.

legacy, refactor, team collaboration

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}