Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Legacy Code to Testable Code #6: Add Overload

DZone's Guide to

Legacy Code to Testable Code #6: Add Overload

· DevOps Zone
Free Resource

The DevOps Zone is brought to you in partnership with Sonatype Nexus. The Nexus Suite helps scale your DevOps delivery with continuous component intelligence integrated into development tools, including Eclipse, IntelliJ, Jenkins, Bamboo, SonarQube and more. Schedule a demo today

This post is part of the “Legacy Code to Testable Code” series. In the series we’ll talk about making refactoring steps before writing tests for legacy code, and how they make our life easier. 

In the last post, I’ve talked about Extract Class, and that sometimes in order to do that, we might want to change the signature of a method.

Turns out adding an overload helps in other cases as well.

We used a "setter" to expose and inject internal state before. Another option is to add a controllable overload to bypass the internal state.
Let's look at this code:

public Bool isSameStreet(String newStreet)     {
    return newStreet == this.currentAddress.getStreet();
}

In this example, we compare an external value to an internal state. As we saw before, the option we could use is add a "setter" accessor, to allow injecting the internal value from outside. Instead, we can also add an overload:

public Bool isSameStreet(String newStreet)     {
    return isSameStreet(newStreet, this.currentAddress.getStreet());
}
public Bool isSameStreet(String newStreet, String currentStreet)     {
    return newStreet == currentStreet();
}

We do the actual comparison on the new overload. The new overload should be accessible to the test, depending on language (so it doesn't have to be completely public). In the original method, we delegate the call to the new implementation.

The new overload is more controllable. We can stop there, but if our logic code does not rely on state anymore, why not use Extract Class?

public Bool isSameStreet(String newStreet)     {
    return StreetValidator.areEqual(newStreet, this.currentAddress.getStreet());
}

The StreetValidator class can now be controlled and tested easily.

Time to wrap up the series. So next time, in the last chapter – using dependency injection framework.


The DevOps Zone is brought to you in partnership with Sonatype Nexus. Use the Nexus Suite to automate your software supply chain and ensure you're using the highest quality open source components at every step of the development lifecycle. Get Nexus today

Topics:

Published at DZone with permission of Gil Zilberfeld, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

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

{{ parent.tldr }}

{{ parent.urlSource.name }}