Everybody loves a satisfied customer. You already know that the first step in writing great software is making sure it does what the customer wants it to. But how do you figure out what a customer really wants? And how do you make sure that the customer even knows what they really want? That’s where good requirements come in, and in this chapter, you’re going to learn how to satisfy your customer by making sure what you deliver is actually what they asked for. By the time you’re done, all of your projects will be “satisfaction guaranteed,” and you’ll be well on your way to writing great software, every time.
You’ve just been hired as the lead programmer at a new start-up, Doug’s Dog Doors. Doug’s got a pretty high-tech door under development, and he’s decided you’re the programmer that can write all the software to make his killer hardware work.
Todd and Gina want more than a “normal” doggie door. Todd has everything from his plasma TV to his surround sound stereo to his garage door operating off of a remote control, and he wants a dog door that responds to the press of a button. Not satisfied with a little plastic flap letting their dog in and out, they’ve given Doug’s Dog Doors a call... and now Doug wants you to build them the dog door of their dreams.
The first thing we need is a class to represent the dog door. Let’s call this class DogDoor, and add just a few simple methods:
But the door doesn’t work the way Todd and Gina want it to!
Todd and Gina didn’t expect to have to close the dog door, so they pressed the button on the remote only once: to let Fido out.
Even worse, in this case, the way they used the door created new problems. Rats and rabbits started coming into their house through the open door, and you’re taking the blame.
Let’s tackle Todd and Gina’s dog door again, but this time, we’ll do things a little bit differently. Here’s our plan:
When it comes to requirements, the best thing you can do is let the customer talk. And pay attention to what the system needs to do; you can figure out how the system will do those things later.
Gina: And we want the door to automatically close after a few seconds. I don’t want to have to wake back up in the middle of the night to close the door.
You: Do you want a single button on the remote, or both an “Open” and “Close” button?
Todd: Well, if the door always closes automatically, we really don’t need separate “Open” and “Close” buttons, do we? Let’s just stick with a single button on the remote control.
You: Sure. So the button opens the door if it’s closed, and it can also close the door if it’s open, just in case the door gets stuck.
Todd: Perfect. Gina, anything else you can think of?
Gina: No, I think that’s it. That’s the dog door of our dreams.
Now that we know what Todd and Gina want, let’s write down our new set of requirements. We don’t need anything too fancy...
You need to understand how the dog door will be used.
You’ve figured out one of the hardest parts about getting a customer’s requirements—sometimes even the customer doesn’t know what they really want! So you’ve got to ask the customer questions to figure out what they want before you can determine exactly what the system should do. Then, you can begin to think beyond what your customers asked for and anticipate their needs, even before they realize they have a problem.
You know what Todd and Gina want the dog door to do, but it’s your job to make sure that the door actually works. In the process, you may even come across some things that Todd and Gina want, but didn’t think about on their own.
Let’s write down exactly what happens when Fido needs to go outside:
Below is a diagram of how Todd and Gina’s dog door should work; all the numbers match up with the steps in our list in What does the dog door really need to do?. But things aren’t always going to go according to plan, so we’ve written down some things that might go wrong along the way.
Now that you’ve figured out some of the things that can go wrong, you need to update your list of things that needs to happen to make the dog door work. Let’s write down what should happen if the door closes before Fido gets back inside.
Yes! You’ve been writing use cases all along
When you wrote down the steps in getting Fido outside to use the bathroom, you were actually writing a use case.
A use case is what people call the steps that a system takes to make something happen. In Todd and Gina’s case, the “something” that needs to happen is getting Fido outside to do his business, and then back inside.
You’ve been writing a use case for almost 10 pages now, but let’s take a closer look at exactly what that list of steps—the use case for Todd and Gina’s dog door—is really all about:
There are three basic parts to a good use case, and you need all three if your use case is going to get the job done.
So far, you’ve got an initial set of requirements and a good solid use case. But now you need to go back to your requirements and make sure that they’ll cover everything your system has to do. And that’s where the use case comes in:
Now you need to look over the use case and see if everything the system needs to do is covered by the requirements.
With use case and requirements in hand, you’re ready to write code that you know will make Todd and Gina satisfied customers. Let’s check out our requirements and see exactly what we’re going to have to write code for:
The only requirement left to code is taking care of automatically closing the door after it’s been opened. Let’s go back to our Remote class and handle that now:
Our old simulator isn’t that useful anymore... it assumes Todd and Gina are closing the door manually, and not letting the timer do its work. Let’s update our simulator to make it work with the updated Remote class:t
It’s time to see if all our hard work is going to pay off. Let’s test out the new and improved dog door.
Good catch... we need to test alternate paths as well as the main path.
Wouldn’t it be great if things worked just like you expected them to every time? Of course, in the real world, that almost never happens. Before we can show the new door off to Todd and Gina, let’s take a little extra time to make sure the door works when Fido doesn’t come right back inside after doing his business.
Your system must work in the real world...
...so plan and test for when things go wrong.
Let’s make sure we understand exactly what happens on the alternate path, and then we can update DogDoorSimulator to test the new path out. Here’s the original main path diagram from Plan for things going wrong, along with the alternate path we figured out and added to our use case:
Make the changes to your copy of DogDoorSimulator.java, and then recompile your test class. Now you’re ready to test out the alternate path of your use case:
Good use cases, requirements, main paths, alternate paths, and a working simulator; we’re definitely on the road to great software. Let’s take the new dog door to Todd and Gina.
Not only did we turn Todd and Gina into satisfied customers, we made sure their door worked when Fido did something they didn’t expect—like stay outside playing.
Secondary Actor: John
Preconditions: The dog door is open for Tex to go outside.
Goal: Tex uses the bathroom and comes back inside, without getting mud inside the house.
Main Path
Tex goes outside.
The dog door closes automatically.
Tex does his business.
John presses a button.
The dog door opens.
Tex comes back inside.
The door closes automatically.
Extensions
3.1 Tex gets muddy.
3.2 John cleans Tex up.
Answers in Use Case Magnets Solutions.