Welcome!

Java IoT Authors: Elizabeth White, Stackify Blog, APM Blog, Liz McMillan, Yeshim Deniz

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
In his session at 21st Cloud Expo, Carl J. Levine, Senior Technical Evangelist for NS1, will objectively discuss how DNS is used to solve Digital Transformation challenges in large SaaS applications, CDNs, AdTech platforms, and other demanding use cases. Carl J. Levine is the Senior Technical Evangelist for NS1. A veteran of the Internet Infrastructure space, he has over a decade of experience with startups, networking protocols and Internet infrastructure, combined with the unique ability to it...
"There's plenty of bandwidth out there but it's never in the right place. So what Cedexis does is uses data to work out the best pathways to get data from the origin to the person who wants to get it," explained Simon Jones, Evangelist and Head of Marketing at Cedexis, in this SYS-CON.tv interview at 21st Cloud Expo, held Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA.
"Cloud Academy is an enterprise training platform for the cloud, specifically public clouds. We offer guided learning experiences on AWS, Azure, Google Cloud and all the surrounding methodologies and technologies that you need to know and your teams need to know in order to leverage the full benefits of the cloud," explained Alex Brower, VP of Marketing at Cloud Academy, in this SYS-CON.tv interview at 21st Cloud Expo, held Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clar...
Large industrial manufacturing organizations are adopting the agile principles of cloud software companies. The industrial manufacturing development process has not scaled over time. Now that design CAD teams are geographically distributed, centralizing their work is key. With large multi-gigabyte projects, outdated tools have stifled industrial team agility, time-to-market milestones, and impacted P&L stakeholders.
Gemini is Yahoo’s native and search advertising platform. To ensure the quality of a complex distributed system that spans multiple products and components and across various desktop websites and mobile app and web experiences – both Yahoo owned and operated and third-party syndication (supply), with complex interaction with more than a billion users and numerous advertisers globally (demand) – it becomes imperative to automate a set of end-to-end tests 24x7 to detect bugs and regression. In th...
"Akvelon is a software development company and we also provide consultancy services to folks who are looking to scale or accelerate their engineering roadmaps," explained Jeremiah Mothersell, Marketing Manager at Akvelon, in this SYS-CON.tv interview at 21st Cloud Expo, held Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA.
"MobiDev is a software development company and we do complex, custom software development for everybody from entrepreneurs to large enterprises," explained Alan Winters, U.S. Head of Business Development at MobiDev, in this SYS-CON.tv interview at 21st Cloud Expo, held Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA.
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...
"IBM is really all in on blockchain. We take a look at sort of the history of blockchain ledger technologies. It started out with bitcoin, Ethereum, and IBM evaluated these particular blockchain technologies and found they were anonymous and permissionless and that many companies were looking for permissioned blockchain," stated René Bostic, Technical VP of the IBM Cloud Unit in North America, in this SYS-CON.tv interview at 21st Cloud Expo, held Oct 31 – Nov 2, 2017, at the Santa Clara Conventi...
SYS-CON Events announced today that Telecom Reseller 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, NY. Telecom Reseller reports on Unified Communications, UCaaS, BPaaS for enterprise and SMBs. They report extensively on both customer premises based solutions such as IP-PBX as well as cloud based and hosted platforms.
"Space Monkey by Vivent Smart Home is a product that is a distributed cloud-based edge storage network. Vivent Smart Home, our parent company, is a smart home provider that places a lot of hard drives across homes in North America," explained JT Olds, Director of Engineering, and Brandon Crowfeather, Product Manager, at Vivint Smart Home, in this SYS-CON.tv interview at @ThingsExpo, held Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA.
Coca-Cola’s Google powered digital signage system lays the groundwork for a more valuable connection between Coke and its customers. Digital signs pair software with high-resolution displays so that a message can be changed instantly based on what the operator wants to communicate or sell. In their Day 3 Keynote at 21st Cloud Expo, Greg Chambers, Global Group Director, Digital Innovation, Coca-Cola, and Vidya Nagarajan, a Senior Product Manager at Google, discussed how from store operations and ...
It is of utmost importance for the future success of WebRTC to ensure that interoperability is operational between web browsers and any WebRTC-compliant client. To be guaranteed as operational and effective, interoperability must be tested extensively by establishing WebRTC data and media connections between different web browsers running on different devices and operating systems. In his session at WebRTC Summit at @ThingsExpo, Dr. Alex Gouaillard, CEO and Founder of CoSMo Software, presented ...
WebRTC is great technology to build your own communication tools. It will be even more exciting experience it with advanced devices, such as a 360 Camera, 360 microphone, and a depth sensor camera. In his session at @ThingsExpo, Masashi Ganeko, a manager at INFOCOM Corporation, introduced two experimental projects from his team and what they learned from them. "Shotoku Tamago" uses the robot audition software HARK to track speakers in 360 video of a remote party. "Virtual Teleport" uses a multip...
A strange thing is happening along the way to the Internet of Things, namely far too many devices to work with and manage. It has become clear that we'll need much higher efficiency user experiences that can allow us to more easily and scalably work with the thousands of devices that will soon be in each of our lives. Enter the conversational interface revolution, combining bots we can literally talk with, gesture to, and even direct with our thoughts, with embedded artificial intelligence, whic...
SYS-CON Events announced today that Evatronix 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. Evatronix SA offers comprehensive solutions in the design and implementation of electronic systems, in CAD / CAM deployment, and also is a designer and manufacturer of advanced 3D scanners for professional applications.
Leading companies, from the Global Fortune 500 to the smallest companies, are adopting hybrid cloud as the path to business advantage. Hybrid cloud depends on cloud services and on-premises infrastructure working in unison. Successful implementations require new levels of data mobility, enabled by an automated and seamless flow across on-premises and cloud resources. In his general session at 21st Cloud Expo, Greg Tevis, an IBM Storage Software Technical Strategist and Customer Solution Architec...
To get the most out of their data, successful companies are not focusing on queries and data lakes, they are actively integrating analytics into their operations with a data-first application development approach. Real-time adjustments to improve revenues, reduce costs, or mitigate risk rely on applications that minimize latency on a variety of data sources. In his session at @BigDataExpo, Jack Norris, Senior Vice President, Data and Applications at MapR Technologies, reviewed best practices to ...
An increasing number of companies are creating products that combine data with analytical capabilities. Running interactive queries on Big Data requires complex architectures to store and query data effectively, typically involving data streams, an choosing efficient file format/database and multiple independent systems that are tied together through custom-engineered pipelines. In his session at @BigDataExpo at @ThingsExpo, Tomer Levi, a senior software engineer at Intel’s Advanced Analytics gr...
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, it’s about the data consumed and generated. The devices are tools, mechanisms, conduits. In his session at Internet of Things at Cloud Expo | DXWor...