Welcome!

Java IoT Authors: Pat Romanski, Zakia Bouachraoui, Yeshim Deniz, Elizabeth White, Liz McMillan

Related Topics: @CloudExpo, Java IoT, @DevOpsSummit

@CloudExpo: Blog Feed Post

Unit Testing Legacy Code: Three Reasons to Reconsider By @ReselBob | @CloudExpo #Cloud

Unit testing is a fundamental tenet of the practice of Test Driven Development

Unit Testing Legacy Code: Three Reasons to Reconsider
By Bob Reselman

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.

Here are some additional resources to help you better understand how to handle legacy code:

Read the original blog entry...

More Stories By SmartBear Blog

As the leader in software quality tools for the connected world, SmartBear supports more than two million software professionals and over 25,000 organizations in 90 countries that use its products to build and deliver the world’s greatest applications. With today’s applications deploying on mobile, Web, desktop, Internet of Things (IoT) or even embedded computing platforms, the connected nature of these applications through public and private APIs presents a unique set of challenges for developers, testers and operations teams. SmartBear's software quality tools assist with code review, functional and load testing, API readiness as well as performance monitoring of these modern applications.

IoT & Smart Cities Stories
Digital Transformation and Disruption, Amazon Style - What You Can Learn. Chris Kocher is a co-founder of Grey Heron, a management and strategic marketing consulting firm. He has 25+ years in both strategic and hands-on operating experience helping executives and investors build revenues and shareholder value. He has consulted with over 130 companies on innovating with new business models, product strategies and monetization. Chris has held management positions at HP and Symantec in addition to ...
Dynatrace is an application performance management software company with products for the information technology departments and digital business owners of medium and large businesses. Building the Future of Monitoring with Artificial Intelligence. Today we can collect lots and lots of performance data. We build beautiful dashboards and even have fancy query languages to access and transform the data. Still performance data is a secret language only a couple of people understand. The more busine...
The challenges of aggregating data from consumer-oriented devices, such as wearable technologies and smart thermostats, are fairly well-understood. However, there are a new set of challenges for IoT devices that generate megabytes or gigabytes of data per second. Certainly, the infrastructure will have to change, as those volumes of data will likely overwhelm the available bandwidth for aggregating the data into a central repository. Ochandarena discusses a whole new way to think about your next...
CloudEXPO | DevOpsSUMMIT | DXWorldEXPO are the world's most influential, independent events where Cloud Computing was coined and where technology buyers and vendors meet to experience and discuss the big picture of Digital Transformation and all of the strategies, tactics, and tools they need to realize their goals. Sponsors of DXWorldEXPO | CloudEXPO benefit from unmatched branding, profile building and lead generation opportunities.
All in Mobile is a place where we continually maximize their impact by fostering understanding, empathy, insights, creativity and joy. They believe that a truly useful and desirable mobile app doesn't need the brightest idea or the most advanced technology. A great product begins with understanding people. It's easy to think that customers will love your app, but can you justify it? They make sure your final app is something that users truly want and need. The only way to do this is by ...
DXWorldEXPO LLC announced today that Big Data Federation to Exhibit at the 22nd International CloudEXPO, colocated with DevOpsSUMMIT and DXWorldEXPO, November 12-13, 2018 in New York City. Big Data Federation, Inc. develops and applies artificial intelligence to predict financial and economic events that matter. The company uncovers patterns and precise drivers of performance and outcomes with the aid of machine-learning algorithms, big data, and fundamental analysis. Their products are deployed...
Cell networks have the advantage of long-range communications, reaching an estimated 90% of the world. But cell networks such as 2G, 3G and LTE consume lots of power and were designed for connecting people. They are not optimized for low- or battery-powered devices or for IoT applications with infrequently transmitted data. Cell IoT modules that support narrow-band IoT and 4G cell networks will enable cell connectivity, device management, and app enablement for low-power wide-area network IoT. B...
The hierarchical architecture that distributes "compute" within the network specially at the edge can enable new services by harnessing emerging technologies. But Edge-Compute comes at increased cost that needs to be managed and potentially augmented by creative architecture solutions as there will always a catching-up with the capacity demands. Processing power in smartphones has enhanced YoY and there is increasingly spare compute capacity that can be potentially pooled. Uber has successfully ...
SYS-CON Events announced today that CrowdReviews.com has been named “Media Sponsor” of SYS-CON's 22nd International Cloud Expo, which will take place on June 5–7, 2018, at the Javits Center in New York City, NY. CrowdReviews.com is a transparent online platform for determining which products and services are the best based on the opinion of the crowd. The crowd consists of Internet users that have experienced products and services first-hand and have an interest in letting other potential buye...
When talking IoT we often focus on the devices, the sensors, the hardware itself. The new smart appliances, the new smart or self-driving cars (which are amalgamations of many ‘things'). When we are looking at the world of IoT, we should take a step back, look at the big picture. What value are these devices providing. IoT is not about the devices, its about the data consumed and generated. The devices are tools, mechanisms, conduits. This paper discusses the considerations when dealing with the...