I'm asking: is there scientific proof (i.e., in a study) that testing and maintainability are at least positively correlated in a reasonably universal fashion?
Automated regression tests can be created in may ways, including UI-level tests that don't require any changes in coding practices at all.
If you write client code and don't need to mutate an object, having it "hard-coded" significantly improves readability and makes your program much simpler.
The most trivial example I can come up with. Compare:
public void SomeMethod()
{
string config = File.ReadAllText("bal.conf");
//etc
}
with this: string config;
public Something(string newConfig)
{
config = newConfig;
}
public void SomeMethod()
{
//etc
}
To understand what the first method does, you only need to look at the method. To understand what the second block of code does, you need to know about the method, the property, the constructor, and even with all that knowledge you have no idea where that string really came from. If it always comes from the same file, it's added complexity with no tangible benefit.It's the side of dependency injection that people don't like to speak about. Client code doesn't always need to be perfectly flexible. You can make it flexible when and where it's needed.
// yay, scala
def someMethod(config: String = File.ReadAllText("bal.conf")) = {
//etc
}So, let's see, you've updated tests, code, and documentation all in one. Whereas if you didn't have tests, you'd have to update the code through manually searching and testing everything, and still have to remember to update the documentation.
Unit tests help refactorability in dynamicly typed languages. Whether this is an argument for static typing is left as an exercise for the reader.
Most of my tests are two-liners, like:
There are a number of reasons why this has helped:1. It ensures I'm using dependency injection, etc to write testable, well-factored code. There's huge correlation between testability and maintainability. 2. I don't have to boot the thing all the time to confirm that I didn't mess up in some obvious way. Covering the code paths prevents typos. 3. My test suite runs in under three seconds. I can sanity check what I'm doing, without being tempted to browse HN/reddit/twitter/etc.
I like TDD more in wiring-only code. If I'm writing wiring, I know what my test case will look like ahead of time, and I'll write it first. If I'm writing experimental algorithms, I have no idea what will happen, and I'd rather write code and poke it.