Saturday, September 15, 2007

Of Jargon and Metaphors

The Lessons:
  1. Customer jargon trumps development metaphors- even deeply entrenched ones.
  2. Broken or conflicting metaphors need removed via refactoring- and sooner is better.
  3. Remove/rename old terminology before you reintroduce it with new development. It's far safer that way.

We've had a very successful application library in place since 2004. Core to its design was the metaphor of a team of people on a project, each with a specific role to play. Years of development had peacefully grown up around this simple development metaphor.


It's 2007, and up jumps the devil. The customer now would like to introduce their own team concept to the application suite. Developer discussion ensued. We could:

  • Translate the new requirement into some other metaphor, such as "caucuses"- but that would require the mental translation (and leap for new deveopers) to "teams" that the customer understands. Worse, that would leave "team" still there, waiting to confuse everyone if their wits aren't on high alert. I just don't like digging holes I'm going to fall into later on.
  • Refactor the code and remove the old metaphor. This is an expensive proposition. There is a lot of code that uses the metaphor. Additionally, it has expanded to published service interfaces, table and field names in the database, XML entries... Surely out of the question?


The discussion came around again to what time would be spent in the future jumping needless mental hurdles, going from caucuses to teams. What about time spent just debugging "teams" that aren't teams, and caucuses that are teams? What about developer comfort? How valuable is it for a customer and developer to be able to talk to each other with the same jargon? What about the hotbed of bugs waiting to leap at our throats due to the confusion? Wouldn't we continue to pay indefinitely? Yes, we jolly well would...


Refactoring started in earnest. It took me two and a half days to remove every single instance of the word team from the Service Interfaces, the core application domain, the database schemas, and XML mappings. In the course of the edit, I found two unused properties and an abandoned query. The software base shrank by nearly sixty lines. The changes were behavior-neutral; though keyed to new API methods. This was only possible since we own all of the client code. The other developers kept on course for the iteration. Continuous integration broke on me; but it was fixed by the end of the day.


What made the refactoring a slam-dunk for me was the fact I could remove the team metaphor before the new customer jargon was employed. This simple fact made it almost a search-and-replace operation. I didn't have to burn time inspecting each instance to determine if it was one that needed to go or stay.


Including the refactoring work, the new feature for the customer took all of three and a half days. I can sleep tonight, because I know that a team is now just a team.


What happened to the old "teams" metaphor? It turned out we'd outgrown it, anyway. We'd long since began talking in terms of people who are members of a project. It was easy to carry this through the code while removing team references.


Human intelligence is the weak link in maintainability. Make it as easy on your brain as possible throughout a project.

Tuesday, September 11, 2007

Why Test?

I tutor programming craft, and I now always start with testing. I force testing, even when professors don't even mention it. But why? Why test? As a challenge to myself, I decided to try to list as many reasons as I could off of the top of my head.

I've been using unit testing JUnit (Java) and TestUnit (Ruby) for about three years, now. In that time, my code quality has drastically improved. Moreover, I'm getting other benefits from testing, especially over the past year. Things like...

Documentation

I'm not a huge fan of classic software documentation- the stuff that begins with /*, //, #, or (really old school)... ;

In fact, I find that documentation, if it isn't lying to you outright- is still just laying there. It doesn't do anything, so it never gets fixed. It doesn't get wavy red lines under it when it doesn't fit the unit of code. I almost never find the intent of the code in comments. It usually just parrots what the software was originally supposed to do:

a++; // Increment a

Unit tests don't lie. They run. They demonstrate succinctly how the code is supposed to work... even how it was envisioned to work before it was written. They demonstrate how it is expected to fail. Best of all, it shows you if the unit of code is still working as expected.

Comments, at best, often tell what steps a unit of code is following. Unit tests show its reason to be. If I have software written with supporting tests, I can always look at the tests to see how to use it. In development, the best documentation I can run into is a working example. Tests give that to me with a bow on top.

Shows It Works (now... on this hardware... on this platform... after these changes... with this OS security patch...)

You just can't beat walking away from a project knowing your software is just going to work. Of course there are surprises, but there have been many, many less in the last couple of years.

What value is there in testing? I've moved platforms entirely- Postgresql to Oracle, Linux to Windows (what backwards migrations!)- and every step of the way I can tell within ten minutes if I'm going to run into major problems. How? If each method is covered with tests, and any of them fail, I know about it. I don't have to research. I can do it half-asleep with a single command line. Within minutes, I have an enumerated list of problems I can cite, or a clean bill of health. It's hard to put a price tag on that ability. By paying the price of writing tests, I reap the dividends over the life of the project.

Sets Proper Development Focus

Programmers think in terms of solutions. We're awfully fast to jump into implementation details about how to solve a problem. It is much, much better to consider first what success looks like. Consider how your solution is going to be used. Writing tests first properly focuses your mind on how you're going to use your solution to the problem. It reduces the number of methods you write, since you don't write code you only think you're going to need later. You only write code that meets you immediate, demonstrated need. You begin with the end clearly in mind. Programming focuses a tremendous effort in implementation; testing on intent and usage.

Writing Tests Makes Testable (Read: Better) Code

When you write code without tests, it often becomes tightly bound in multiple concerns and mired in dependencies. It can, because there are no bars it must hurdle over. It will become fat, lazy, slow, broken, and, finally... inoperable. When you write code to meet a test, you write testable code. It's just that simple. Testable code is naturally more independent (and therefore better OOP) because it has to be. It's no coincidence that code that sports multiple concerns and bound to other classes' implementation details is prone to problems. Writing tests is difficult. Writing better code can be a challenge. In the end, though, it's a huge payoff.

There's a more subtle payoff, too. When you use revision control correctly, you always have code in the repository that passes all of its tests. That means you never have to fight with broken code you've hammered on for hours. How? You can always pull the last working code, add your new test, and apply only the select changes from your broken code. Once the tests pass, you commit your good code and delete that nasty, hammered-on, broken junk.

Writing Tests Forces Incremental Development

How far along are you on a project? It's often a difficult question to answer. You can spend weeks wandering in the forest and not know if what you've done is something you want to keep, or not. Or, you can write tests. I develop in a cycle where I start with fresh code from the repository that I know passes its tests before I begin to add or change anything. When I add a test and make it pass, I know I've made a step forward. I have something to commit, and it didn't take too long. It may not be a huge feature, but it's forward progress. Best of all, I know when I'm finished writing the section of code. After all, it's passed its tests. I start with a set of expectations, and I know when I meet them through tests.

Writing Tests Lower Software Maintenance Costs

Option one: Write a lot of code quickly, then spend an inordinate amount of time debugging it later. Option two: Write less code with equal or more functionality more slowly, then be able to quickly zero in on problems that crop up later- either by running the existing test suite or writing new tests that re-create new problems encountered in production. I pick option two. While each code unit may take a little longer to write at first, remember you spend way more time later pouring over source code than you ever spend writing it in the first place. Your project velocity really does accelerate in the medium and long term. Longer term, the software is resistant to the kinds of problems that make development teams and managers cringe for a re-write.

Writing Tests Enables Using More Powerful Languages And Techniques

You can't refactor (in any language) if you don't have tests. Otherwise, you can't guarantee your changes are really behavior-neutral. So tests are a gateway to better things.

You can't use Ruby or any other dynamic language without tests. In fact, I'd go as far to say you can't use them without 100% code test coverage. Why? Unlike compiled languages, each line, condition and branch must be thrown into the execution stream just to know if I've fat-fingered something. Does it take a little longer to test? Yes, but I just wrote a time sheet system following union contract rules in sixteen days. The payback is that your development velocity tends to be (a whole lot) faster in Ruby than in Java.

Writing Tests Helps Guarantee The Computer Does What You Expect, So You Can Focus On What The Customer Expects.

It's hard enough trying to zero in on what the customer expects through the iterative development cycle. (To say nothing of the waterfall Requirements Document. Yuck.) You really want to avoid communication break-downs that form between you and the computer. Tests let you know the software does what you expect. That leaves you better prepared to concentrate on what the customer wants. If a calculation isn't as a customer expects, all I do is go over the problem in a way that is meaningful to the customer. I plunk in the right calculated value into an assert statement and make the code pass the test suite.

It's Ethical To Write Tests

I used to work for a company that did construction testing. Bridges don't get built until the soil is tested to know it will support the piers. Concrete doesn't get poured without a sample taken to know it will support the weight and stress. Even after pouring, radar lets you see inside the concrete to know if there are voids that would weaken the structure. The steel is tested. Before all of that, the calculations are checked by peer review of other engineers. Testing isn't a luxury. It's what makes modern construction possible.

Once upon a time, they sacrificed people and put them under the bridge to make sure it didn't fall down. Sometimes, it worked. A lot of places do that with developers and software today. By burying a programmer in untested code, you can have the same, false sense of security enjoyed by bridge builders of the Middle Ages.

This isn't the Middle Ages. We test because we know it makes better, more reliable software. We're still human; we make mistakes. Tests are necessary to know software works; but it's not yet sufficient. Bridges still fail, too. Just as not testing isn't an ethical option for a civil engineer, it shouldn't be considered by professional software developers, either. It's not a question of development speed, managerial prerogative, or hacker buzz phrase du jour. It's just the right thing to do. Your customers and your craft are worth it.