Welcome!

Java IoT Authors: Liz McMillan, Elizabeth White, Pat Romanski, Yeshim Deniz, Frank Lupo

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.

@ThingsExpo Stories
Join IBM November 1 at 21st Cloud Expo at the Santa Clara Convention Center in Santa Clara, CA, and learn how IBM Watson can bring cognitive services and AI to intelligent, unmanned systems. Cognitive analysis impacts today’s systems with unparalleled ability that were previously available only to manned, back-end operations. Thanks to cloud processing, IBM Watson can bring cognitive services and AI to intelligent, unmanned systems. Imagine a robot vacuum that becomes your personal assistant tha...
In his Opening Keynote at 21st Cloud Expo, John Considine, General Manager of IBM Cloud Infrastructure, will lead you through the exciting evolution of the cloud. He'll look at this major disruption from the perspective of technology, business models, and what this means for enterprises of all sizes. John Considine is General Manager of Cloud Infrastructure Services at IBM. In that role he is responsible for leading IBM’s public cloud infrastructure including strategy, development, and offering ...
In a recent survey, Sumo Logic surveyed 1,500 customers who employ cloud services such as Amazon Web Services (AWS), Microsoft Azure, and Google Cloud Platform (GCP). According to the survey, a quarter of the respondents have already deployed Docker containers and nearly as many (23 percent) are employing the AWS Lambda serverless computing framework. It’s clear: serverless is here to stay. The adoption does come with some needed changes, within both application development and operations. Tha...
SYS-CON Events announced today that Avere Systems, a leading provider of enterprise storage for the hybrid cloud, will exhibit at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 - Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Avere delivers a more modern architectural approach to storage that doesn't require the overprovisioning of storage capacity to achieve performance, overspending on expensive storage media for inactive data or the overbui...
Widespread fragmentation is stalling the growth of the IIoT and making it difficult for partners to work together. The number of software platforms, apps, hardware and connectivity standards is creating paralysis among businesses that are afraid of being locked into a solution. EdgeX Foundry is unifying the community around a common IoT edge framework and an ecosystem of interoperable components.
SYS-CON Events announced today that TidalScale will exhibit at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. TidalScale is the leading provider of Software-Defined Servers that bring flexibility to modern data centers by right-sizing servers on the fly to fit any data set or workload. TidalScale’s award-winning inverse hypervisor technology combines multiple commodity servers (including their ass...
SYS-CON Events announced today that N3N will exhibit at SYS-CON's @ThingsExpo, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. N3N’s solutions increase the effectiveness of operations and control centers, increase the value of IoT investments, and facilitate real-time operational decision making. N3N enables operations teams with a four dimensional digital “big board” that consolidates real-time live video feeds alongside IoT sensor data a...
As hybrid cloud becomes the de-facto standard mode of operation for most enterprises, new challenges arise on how to efficiently and economically share data across environments. In his session at 21st Cloud Expo, Dr. Allon Cohen, VP of Product at Elastifile, will explore new techniques and best practices that help enterprise IT benefit from the advantages of hybrid cloud environments by enabling data availability for both legacy enterprise and cloud-native mission critical applications. By rev...
With major technology companies and startups seriously embracing Cloud strategies, now is the perfect time to attend 21st Cloud Expo October 31 - November 2, 2017, at the Santa Clara Convention Center, CA, and June 12-14, 2018, at the Javits Center in New York City, NY, and learn what is going on, contribute to the discussions, and ensure that your enterprise is on the right path to Digital Transformation.
Join IBM November 1 at 21st Cloud Expo at the Santa Clara Convention Center in Santa Clara, CA, and learn how IBM Watson can bring cognitive services and AI to intelligent, unmanned systems. Cognitive analysis impacts today’s systems with unparalleled ability that were previously available only to manned, back-end operations. Thanks to cloud processing, IBM Watson can bring cognitive services and AI to intelligent, unmanned systems. Imagine a robot vacuum that becomes your personal assistant th...
Digital transformation is changing the face of business. The IDC predicts that enterprises will commit to a massive new scale of digital transformation, to stake out leadership positions in the "digital transformation economy." Accordingly, attendees at the upcoming Cloud Expo | @ThingsExpo at the Santa Clara Convention Center in Santa Clara, CA, Oct 31-Nov 2, will find fresh new content in a new track called Enterprise Cloud & Digital Transformation.
Most technology leaders, contemporary and from the hardware era, are reshaping their businesses to do software. They hope to capture value from emerging technologies such as IoT, SDN, and AI. Ultimately, irrespective of the vertical, it is about deriving value from independent software applications participating in an ecosystem as one comprehensive solution. In his session at @ThingsExpo, Kausik Sridhar, founder and CTO of Pulzze Systems, will discuss how given the magnitude of today's applicati...
Smart cities have the potential to change our lives at so many levels for citizens: less pollution, reduced parking obstacles, better health, education and more energy savings. Real-time data streaming and the Internet of Things (IoT) possess the power to turn this vision into a reality. However, most organizations today are building their data infrastructure to focus solely on addressing immediate business needs vs. a platform capable of quickly adapting emerging technologies to address future ...
SYS-CON Events announced today that Ryobi Systems will exhibit at the Japan External Trade Organization (JETRO) Pavilion at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Ryobi Systems Co., Ltd., as an information service company, specialized in business support for local governments and medical industry. We are challenging to achive the precision farming with AI. For more information, visit http:...
As popularity of the smart home is growing and continues to go mainstream, technological factors play a greater role. The IoT protocol houses the interoperability battery consumption, security, and configuration of a smart home device, and it can be difficult for companies to choose the right kind for their product. For both DIY and professionally installed smart homes, developers need to consider each of these elements for their product to be successful in the market and current smart homes.
SYS-CON Events announced today that Golden Gate University will exhibit at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Since 1901, non-profit Golden Gate University (GGU) has been helping adults achieve their professional goals by providing high quality, practice-based undergraduate and graduate educational programs in law, taxation, business and related professions. Many of its courses are taug...
What is the best strategy for selecting the right offshore company for your business? In his session at 21st Cloud Expo, Alan Winters, U.S. Head of Business Development at MobiDev, will discuss the things to look for - positive and negative - in evaluating your options. He will also discuss how to maximize productivity with your offshore developers. Before you start your search, clearly understand your business needs and how that impacts software choices.
SYS-CON Events announced today that Avere Systems, a leading provider of hybrid cloud enablement solutions, will exhibit at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Avere Systems was created by file systems experts determined to reinvent storage by changing the way enterprises thought about and bought storage resources. With decades of experience behind the company’s founders, Avere got its ...
High-velocity engineering teams are applying not only continuous delivery processes, but also lessons in experimentation from established leaders like Amazon, Netflix, and Facebook. These companies have made experimentation a foundation for their release processes, allowing them to try out major feature releases and redesigns within smaller groups before making them broadly available. In his session at 21st Cloud Expo, Brian Lucas, Senior Staff Engineer at Optimizely, will discuss how by using...
In this strange new world where more and more power is drawn from business technology, companies are effectively straddling two paths on the road to innovation and transformation into digital enterprises. The first path is the heritage trail – with “legacy” technology forming the background. Here, extant technologies are transformed by core IT teams to provide more API-driven approaches. Legacy systems can restrict companies that are transitioning into digital enterprises. To truly become a lead...