Non-Redundancy and Testability
Have you ever inherited a code base full of redundancies? One dev argues for the need to standardize practices such as the Open-Closed Principle.
Join the DZone community and get the full member experience.Join For Free
Redundancy is always a maintenance issue. If you have to change redundant code, you have to do it in multiple places. If you have to extend redundant code, you have to do it in multiple places. When you have to test redundant code, you have to modify tests in multiple places.
You get the idea.
Have you ever heard of Shalloway’s Law? Al Shalloway is a brilliant thought leader, the CEO of Net Objectives, and a good friend. He was trained as a mathematician at MIT and likes to poke fun at himself so he has what he calls Shalloway’s Law—I actually own a t-shirt that has Shalloway’s Law written on it.
Shalloway’s Law says that “When n things need to change and n is greater than one, Shalloway will find at most n-1 of these things.”
In other words, like the rest of us, he’s fallible, and when there’s more than one thing that needs to change in a computer system, it’s easy to miss at least one of them.
As a result, he came up with what he calls Shalloway’s Principle, which states: “Avoid situations where Shalloway’s Law applies.”
This is good advice. Build code that’s maintainable by minimizing the impact of what needs to change when business rules change.
This relates to the Open-Closed Principle, which states that software entities, such as classes and methods, should be open for extension but closed for modification. In other words, I want to limit the code in my system that needs to be changed as I extend the system. I want to build systems in such a way that whenever I extend it I’m having a minimum impact on existing code even if that means I have to write a lot more new code to compensate for or accommodate this new requirement.
Developers don’t mind writing new code. They actually like it. It’s what we do. What developers don’t like is changing existing code, especially if someone else wrote it.
First of all, you have to understand it and invariably when someone else writes software they do it differently than the way we would. We then have to sort of get into their minds because there are currently very few standards and practices in our industry. I do hope and pray that it will change soon. But as of now, and in the foreseeable future, we’ll be dealing with a lot of poorly written and difficult to work with legacy code.
I do have faith that we’ll slowly dig ourselves out and start building software that’s more in alignment with those principles because this, I believe, is the guiding light in software development. We developers should focus on finding ways to support the Open-Closed Principle by building software that can be extended rather than changed to accommodate more features. We do this by centralizing business rules, centralizing instantiation by taking common routines and putting them in a shared place so they can be used by multiple callers rather than having each caller have its own implementation.
How much software in production today would you say is redundant?
Even within a single operating system, I propose that if we pay attention to collapsing redundancy we can reduce the executable footprint by at least 50%. That’s my guess. I have no facts to back that up, but I do have a lot of anecdotal evidence to suggest that we developers reinvent the wheel over and over but with subtle and sometimes not so subtle differences.
Sometimes I don’t see the redundancy in my code but I find myself writing a redundant test and realize that my code is redundant as well. There are lots of techniques for finding redundancy and eliminating it in code, and I’ve written about this here and in my book Beyond Legacy Code: Nine Practices to Extend the Life (and Value) of Your Software.
The bottom line is: the less redundancy in a system, the more straightforward and cost-effective testing and maintenance becomes.
Published at DZone with permission of David Bernstein, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.