Unit Testing Legacy Code: 3 Reasons to Reconsider

3 Reasons Legacy Code Twitter

Teams that have embraced DevOps and begun using the practice of test driven development are familiar with the headaches that accompany testing legacy code.

This is particularly true for companies that have applications out in production that have been working for years, but have no formal tests or testing regiment associated with its codebase.

Most of the time the physical code for the applications is known; many times it is not. In fact, both the code and the developer who wrote the code might have left the building years ago.

So, what do you do?

Unit testing is a fundamental tenet of the practice of Test Driven Development. As a result, the typical reaction by managers pursuing the DevOps way of life is to have development staff go back into the codebase and write unit tests.

Once the units tests are in place, the code coverage number relevant to that portion of the codebase being tested will improve and hopefully achieve the enterprise’s standard, which in most places is around 80%.

Thus, the manager can sleep better at night knowing that the legacy code is now a full-fledged member of the continuous build/continuous deployment process and that his or her organization is one step closer to achieving the technical excellence that the DevOps way of life provides. Right?

Not exactly. While attempting to test legacy code can seem like the best solution, in many cases it turns into one of the costly and time-consuming process teams want to avoid.

Here are a few of the significant challenges to watch out for:

1. Difficulty tracking down source code

In order to test a legacy application, you have to have the code. Just because an application is in force, it does not necessarily mean that the source code is available, particularly when the application is old.

Believe me, there is still a lot of C, C++, Delphi, VB6, and FoxPro out there working as compiled binaries without any source code in sight. Yes, you can do some reverse compilation and get something that looks like source code. But, then you’ll have to find a testing framework compatible with the old code. Odds are you are going to go through a lot of labor just to get to a point where you can write some tests. This might be fine if you are working with a small utility.

But what about large scale applications that potentially might be made up of hundreds upon thousands of lines of code?

As those of us on the terrain have learned, at the enterprise level, things get very expensive very fast. The clock that is ticking while you try to get the source code you need to do the testing you desire is dollars adding up.

But, let’s say you do get the source code. Now you have to write the tests.

2. Untestable code

Just because code is functioning, it does not necessarily mean that the code is testable. Consider the following Java code:

 

public class OldApp{

public static void main(String[] args){

if(args != null && args[0] != null){

String message;

message = args[0];

Stringifier stringifier = new Stringifier(message);

System.out.print(stringifier.toString());

}

}

 

private static class Stringifier{

String message;

public Stringifier(String input)

{

message = input;

}

public String toString()

{

return message.toUpperCase();

}

}

}

Notice the class, Stringifier contains the intelligence that makes the overall application special.

(OK, you got me. How special can it be to make a string uppercase? Well… let’s pretend that it is special.)

Notice that the special behavior is in a private class. Guess what? If you are developer coming to this code after it was written, you are going to be hard pressed to get that code tested because you can’t test the private class directly. Yes, you can do some inferential testing on main(), but you can never get directly at the method, Stringifier.toString(), or the class’s constructor. All you can do is hope that you write some tests that work.

Of course, Stringifier is a trivial class, doing one thing in a trivial manner. But, what if that private class contained behavior that figures out what is the best stock to buy, at the best price, under a certain set of conditions? In such a case, hope just isn’t going to cut it when it comes to doing valid testing. For all intents and purposes, the code is untestable.

Believe me, there is a lot of code out there that was written before the notion of Test First came along. If that code was never designed to be testable, most likely it isn’t. Have fun figuring it out. You have my sympathy.

3. Navigating business rules

Let’s pretend that you have the source code and that the code is testable.

Now you have to write the tests. Let’s say the application is made up of 50 classes, with an average of public 3 methods per class. If these methods do nothing more than behave consistently under a single condition, there is a good argument that you have to write 150 tests.

Sadly, there are these little things called business rules. Business rules tend to make methods behave one way under a certain set of conditions and another way when the conditions change. So much for your 150 tests. Now you could end up writing thousands of tests, with each test subject to a business rule condition. And, this is assuming that the business rules are identified and well documented.

When is the last time you worked in a place where all the business rules were formally identified and well documented? Usually this is not the case. Often times there is a special person named, Harold who works down on the 3rd floor, who worked with a business analyst a few years back. He’s the one who really knows the rules. Me? In my professional life there has always been a Harold.

The nice thing about code that is well tested and reflects very high code coverage is that the tests serve as a concrete way to document business rules. But, that’s not how legacy code tends to work. Legacy code is more a mystery than not. So, figure if it took 10 developers 10 years to write the application that is now considered legacy, it’s going to take two professional lifetimes to go back and write unit tests for that code. Here’s the formula:

 

10 developer x 10 years = 100 developer years
100 developer years /50 years = 2 lifetimes

WHERE 50 is the length of a professional lifetime.

So what do I do?

Nothing.

That’s right, do nothing until you have to do something. If you have legacy code that is out there working and has been working for a long time, leave it alone. The cost of going back into that code is going to cost more than the value of the testing. History has proven the code works.

But what about the possibility of catastrophe? Yes, the possibility of catastrophe is real, but is catastrophe statistically probable? History has shown time and time again that the odds are in our favor that the sun will rise tomorrow. Is there a chance it won’t? Yes. But, it probability will.

The same can be said of legacy code. If it’s been working for you, with no error for a long time, the odds are it will continue to work for you. Leave it alone. Will the TDD Gods be angry with you? Yes, but such gods are forgiving while the sun continues to rise. Will there be a day when the code does not work or the sun does not rise? Yes, and that is the time when you should do something about it, if doing something is possible.

But really, what should I do?

If doing nothing is unacceptable, here’s what you do: take all that money you would have spent going back into the code and laboring away to get unit testing working and code coverage up, and spend that money on a new, replacement system, one that emulates and improves your current system.

Make sure the development process you follow embraces Test Driven Development and the DevOps sensibilities. Make sure you design code that is testable always and that unit testing is part of the way a developer does business. Make sure that that you have strive for 100% code coverage. This way, twenty years down the line, when you are in the autumn of your professional life, you will rest easy knowing that when it comes to your code, nobody will ever have to ask the question, how are we going to test this legacy code?

What you should do with legacy code.

While you may not want to invest time and resources into testing legacy code, there are benefits of reviewing legacy code that your team should be aware of. Reviewing legacy code can help improve future performance and help reduce defects down the road.

The Complete Guide To Approaching Your Development Team When They Write Bad Code

In our newest eBook, The Complete Guide to Approaching Your Development Team When They Write Bad Codeyou will learn:

  • How to approach your developer’s bad code
  • How to build the appropriate strategy before having the conversation
  • How to collectively take responsibility for bad code
  • How to set a coding standard within your team

Get your copy!

bad-code-ebook-cover

Speak Your Mind

*