Extreme Programming Installed
Extreme Programming Installed
7
Chapter 1 Extreme Programming . . . . . . . . . . . . . . . . 9
Extreme Programming is a discipline of software develop-
ment with values of simplicity, communication, feedback
and courage. We focus on the roles of customer, manager,
and programer and accord key rights and responsibilities to
those in those roles.
Forward . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Chapter 2 Circle of Life . . . . . . . . . . . . . . . . . . . . . . 27
An XP project succeeds when the customers select business
value to be implemented, based on the team’s measured
ability to deliver functionality over time.
1
Sense of Completion . . . . . . . . . . . . . . . . . . . . . . . . .61
XP’s nested planning and programming cycles keep the
project on track, and provide a healthy sense of accomplish-
ment at frequent intervals.
2
On an Extreme Programming team, two programmers sit-
ting together at the same machine write all production
code.
4
stick together, and break the story down into small parts.
You’ll be surprised what you can do.
5
Chapter 33 Estimates and Promises . . . . . . . . . . . . .261
We estimate how long the project will take. We promise to
tell the truth about how we’re doing.
6
Preface
How much would you pay for a software development team that would do
what you want? Wait, don’t answer yet — what if they could also tell you
how much it would cost, so that you could decide what to do and what to
defer, on your way to your deadline? You also get quality software, a
robust array of tests that support the project through its entire lifecycle,
and an up to date, clear view of project status. Best of all, you get the abil-
ity to change your mind about what you want, at any time.
7
Preface
8
Chapter 1
Extreme Programming
Extreme Programming is a discipline of software develop-
ment with values of simplicity, communication, feedback
and courage. We focus on the roles of customer, manager,
and programer and accord key rights and responsibilities
to those in those roles.
9
Extreme Programming
Managers — those who control the project resources — will learn how
to measure project progress, how to measure quality, and how to
answer the all-important question “When will you be done?”. You will
learn an important truth of management - to use the programmers’
actual performance to predict completion.
10
Extreme Programming
Note that we say “the customer”, not “the customers”. Whether they
are one or many people, the XP customer always speaks with one voice.
The determination of what will have business value, and the order of
building that value, rests solely with the customer. (Don’t worry, you
get lots of help and advice. But ultimately, you get to make the call.)
You probably have a delivery date in mind, though some projects have
a fixed feature list rather than a fixed date. We are not content to imag-
ine that everything that you can think of will be done by a given date
Neither should you be. Instead, the XP process lets the team predict,
more and more accurately, how much work can be done in any given
time period. Using this information, you manage project scope —
choosing what to do now and what to defer until later — to ensure
successful delivery.
You, the customer, have the critical responsibility to choose the stories
that will provide the most valuable features, the highest business value,
by the desired delivery date. The XP development process lets you
11
Extreme Programming
choose among the stories with great flexibility. There’s not much limi-
tation on what can be done first and what second. This is by design: if
you are to choose the stories for successful on-time release, you must
have the flexibility to make the choice as independently as possible.
Read more about this process in Customer Defines Release (page 71)
and Iteration Planning (page 79).
Finally, you specify tests that show whether the stories have been cor-
rectly implemented. These Acceptance Tests (page 45), whether built
by the programmers, by an independent tester, or by you yourselves,
provide confidence that the system really does what it needs to do.
Define business value, decide what to do and what to defer, and define
the tests to show that the system works. These are your key responsi-
bilities as the XP customer.
Build the system in small releases, so that the customer benefit is maxi-
mized and you get the best possible feedback on how you’re doing.
We talk about this in Small Releases (page 65), Customer Defines
Release (page 71), and Iteration Planning (page 79).
12
Extreme Programming
Base the program on simple, clear design. This lets you produce qual-
ity software quickly. There’s more discussion of this in Code Quality
(page 103), and A True Story (page 257). As you learn more about
what the design wants to be, improve the design using Refactoring
(page 95).
XP isn’t slash and burn programming, not code and fix, not at all.
Extreme Programming is about careful and continuous design, rapid
feedback from extensive testing, and the maintenance of relentlessly
clear and high-quality code.
Keep the system integrated at all times, so there’s always a good ver-
sion to look at. Keeping integrated lets you go rapidly without
stepping on each others’ toes. See Continuous Integration (page 96).
Share the ownership of all the code, so no one has to wait and every-
one feels able to make everything better. See Collective Code Ownership
(page 93), andReleasing Changes (page 145). Share a single Coding
Standard (page 97) as well, whether you evolve your own or adopt
one from elsewhere. Make everyone’s code look alike—it helps with
communication and team focus. Express your individuality in the way
you wear your XP ball cap.
Make sure that the system always works, using comprehensive unit
tests of your own making, as well as the customer’s acceptance tests.
These tests allow rapid change, and support collective code ownership
by keeping change from introducing mistakes. See Unit Tests
(page 113), Acceptance Tests (page 45), Everything that could possibly
break (page 265), and Test-first, by Intention (page 129).
Write all production code in pairs, for maximum speed and cross-train-
ing, in support of shared code ownership and rapid progress, as
described in Pair Programming (page 107).
13
Extreme Programming
The first and last job of a good manager is to get things out of the way
of the people who are doing the work. Look for things that are slowing
the team, and use your special powers to resolve them. Expedite pur-
chase, make sure the workspace is arranged effectively, keep the
computers up to date, lean on the LAN guys to fix problems, and so
on. You’ll be most successful if you remove everything from the team’s
path that doesn’t contribute to the objective of delivering good soft-
ware on time.
It may seem that the entire team just magically appears at the planning
table when it’s time for the next release plan. If the manager does a
good job, that is.
As manager, you cause that meeting, and you coordinate it into exist-
ence. At a stand-up meeting a bit before release planning time,
mention the need for the meeting and suggest a date. If there’s general
agreement, go ahead. If there are scheduling conflicts, go around to
the team members and find a suitable date and time. You might even
have to encourage someone to change a conflicting appointment.
14
Extreme Programming
When the date is chosen, prepare the ground. Arrange a room, send
out the invitations, order the refreshments — or cause these things to
be done if you have administrative help.
During the iteration, it’s the same: cause the right things to happen,
coordinate the activities, report results — and always remove obstacles.
The project manager usually has the personnel responsibility, and this
is a very important one. Even on the best teams, there are differences
between individuals, and sometimes there can be temporary or perma-
nent people problems.
When people get in conflict, you need to fix it. If someone’s behavior
is harming the team, you have to address the problem. If the individual
cannot or will not correct the behavior, you must remove them from
the team. This should not be done lightly or precipitously, but some-
times it must be done, and it is the project manager’s responsibility.
15
Extreme Programming
On the good side, the project manager gets to give rewards. There is
the annual rating and salary adjustment ritual. We can’t tell you how to
do this — extreme teams are all over the map on compensation policy.
It’s the manager’s responsibility to have fair and consistent assessments
of the staff, and to have compensation fairly reflect those assessments.
This only scratches the surface. The project manager’s role is very
important to the project. If done creatively and effectively, it can
greatly ensure the team’s success.
Rights / Responsibilities
Extreme Programming tries to provide certain benefits to
the managers, customers, and developers involved in a
project. We express these as rights because they are very
important to the success of the project, and to the team
members.
Editor’s note: we need these two rights tables in little boxes, like cards.
If they aren’t, it’s because I didn’t figure out how to do it yet. Please
help.
16
Extreme Programming
Programmer Rights
1. You have the right to know what is needed, with clear declarations
of priority.
2. You have the right to produce quality work at all times.
3. You have the right to ask for and receive help from peers, superiors,
and customers.
4. You have the right to make and update your own estimates.
5. You have the right to accept your responsibilities instead of having
them assigned to you.
This book is about helping your project deliver these rights. Here’s a
bit of discussion about why each of these proposed rights is beneficial
to a successful software project, and how XP helps to provide that
benefit.
17
Extreme Programming
You have the right to get the most possible value out of
every programming week.
Having a plan isn’t everything, but planning is. Project time is short,
and you need to be sure that the right things are happening every
week. Iteration Planning, page 79, describes XP’s short-range plan-
ning component.
18
Extreme Programming
You have the right to ask for and receive help from
peers, superiors, and customers.
Sometimes programmers get buried in the complexity of their work.
Help from colleagues and managers can speed things up and get things
back on track. In XP, we recommend an On-site Customer (page 31) to
be sure that requirements are understood. We use Pair Programming
to provide constant help. We offer some additional support techniques
when we discuss the The manager role (page 14). But the fundamental
rule is this: no one can refuse to help a team member who needs it,
ever.
You have the right to make and update your own esti-
19
Extreme Programming
mates.
The most critical factor in the success of your project is knowing when
you are going to be done. By knowing how long it will really take —
not just how long you hope it will take — you can guide the project to
success by managing what is worked on. XP’s Customer Defines Release
and Iteration Planning allow you to do that management. Having the
programmers do Story Estimation (page 51) gives you the information
you need to steer the planning.
Project flow
The rest of the book follows the chronological flow of an XP project.
We’ve pointed to many of the chapters earlier in this introduction. An
XP project begins with an on-site customer, who provides the stories
that define the system and the acceptance tests that prove the system
works. We focus on small releases, each one defined by the customer.
We work in short iterations, again working on the customer’s stories of
highest business value.
20
Extreme Programming
Based on the early iterations, the team uses Experience improves esti-
mates (page 155) to predict future performance, which sets the project
up for success through constant informed Steering (page 171).
There will be a few defects (no one is perfect) and we’ll tell you how to
deal with them in Handling Defects (page 183).
Finally, we include some Bonus Tracks (page 205), essays and ideas we
couldn’t bear to leave out even though they don’t fit exactly in the
chronological flow. We paid a lot to learn those things, and wanted to
share them with you. So dive in and check out Extreme Programming
Installed!
21
Extreme Programming
22
Forward
--Dan Rawsthorne
I have been looking at XP for a while, and have talked to a lot of peo-
ple (including Ron Jeffries and Kent Beck) about it. And I think that a
lot of us are missing the point of why XP works. Quite simply, I think
that XP works because it is Validation-centric rather than Product-cen-
tric. "Huh?" you say.
We also know that the first question is much more important than the
second, as developing the wrong software right is useless. So, because
XP does everything to extremes, we would expect it to focus on the
first question to the exclusion of the second. Almost, but not quite...
23
Forward
As far as I can tell, the main thing about XP that is not all about either
easing communications or developing the right system is Refactoring
(although Refactoring is partly about clearer communications of what
the code does). It is clear that Refactoring is a good thing to do and,
since XP is extreme, if a little Refactoring is good, XP insists on total
Refactoring. Refactoring is the thing that allows Architecture to
emerge from the code, and this emergent architecture is what allows
ease of maintenance, extensibility, and so on - that is, it allows us to
answer the second question.
So, I hear you ask "so what, Dan... is this a useful message you're giv-
ing me, or just random ramblings?"
24
Forward
My belief is that XP is the first popular process with this focus on Vali-
dation built in. It won't be last... I hope.
25
Forward
26
Chapter 2
Circle of Life
An XP project succeeds when the customers select business
value to be implemented, based on the team’s measured
ability to deliver functionality over time.
Steering a project to success all comes down to what we call the “circle
of life”. The customer’s basic job is to define what has value, and the
programmer’s job is to build it.
Customer
Programmer
Figure 2.1 Customer defines, programmer builds.
27
Circle of Life
Business value depends on what you get, but also on when you get it
and how much it costs. To decide what to do, and when, the custom-
ers need to know the cost of what they ask for. The programmers,
based on experience, provide this information. Then the customers
choose what they want, and the programmers build it. Now the pic-
ture looks like this.
Customer
build value define value
Programmer Programmer
choose value estimate cost
Customer
Figure 2.2 Programmer estimates, customer chooses.
28
Circle of Life
Customer
build value define value
learn
Programmer Programmer
choose value estimate cost
Customer
Figure 2.3 Improve estimates of value and cost.
We hate to be the one to break this to you, but some things are proba-
bly going to take longer than you’d like. The good news is that you
can inform yourself about how fast you’re going, and you can use that
information to choose the best mix of features for each release. By
managing scope, you can schedule good, solid releases for the dates
you need.
The best way to get a good release by a given date is to know how fast
the programmers are delivering features, and to use that knowledge to
choose the best mix of features possible by your chosen date. You must
manage the scope of your project.
29
Circle of Life
It’s tempting to believe your own wishes, or your own press releases,
and schedule more work than measurement says you will do. If you
want to ship late, do this. If you want to ship on time, but with a ran-
dom collection of buggy features, do this. For a predictable delivery,
with the quality you need, you must manage scope.
30
Chapter 3
On-site Customer
An XP project needs a full-time customer to provide guid-
ance. Here’s a summary of why ...
Not every project can afford this, and other projects’ members fear
that they can’t. But the plain truth is that the more time customer and
programmers spend together, the better things go. That doesn’t mean
that the customer is holding the programmers’ hands, but it does
mean that being immediately available to answer questions is of
immense value.
31
On-site Customer
A simple story may need backup details that can be provided on paper.
But for the programmers to implement it quickly and well, they need
to build up a sense of how it fits in. That sense is provided by the cus-
tomer, in many discussions, over the course of the project. Here’s an
example:
Union dues vary by union, and are taken only in the first pay
period of the month. The system computes the deduction automati-
cally. The amount is shown in the attached table.
“What’s the deal on plant guards, tier one and tier two?”
“The agreement with the union divides plant guards into two
groups”, the customer replies, “called Tier One and Tier Two.
The two tiers get different dues rates and benefits.”
“How do I tell them apart?”
“It’s all by date of hire. All plant guards hired after September
1995 are Tier Two.”
32
On-site Customer
“OK, got it. By the way, are those dues figures you gave us solid
for all time?”
“No way”, laughs the customer. “Next time there’s a union
negotiation, those figures will all change. There’s a story com-
ing up giving us the ability to change them. For now, just put
them in that way.”
“OK, thanks”, says the programmer, and returns to her desk in
less time than it took to read this.
Now yes, that same exchange could have been held via email, or writ-
ten down in a detailed spec. But that would have taken more time, and
quite likely wouldn’t have completely quelled the programmer’s con-
cern the way the conversation did. You might also be concerned that
this important information about the requirements will get lost some-
where, but remember that you will have specified acceptance tests, and
they will surely cover the two tiers of plant guards.
You could perhaps get this much certainty by writing more down. But
you would have to write lots more, which would slow the project and
still not obviate the need for conversation. With an on-site customer,
you save time and trees, and build a rapport between customer and
programmers that serves to help you over any tough times that may
come along.
One day on Ron’s last visit, the customers had to go to another work
site for machine access. They told the programmers where they were
(two floors down and a few offices over), left their phone number and
beeper number. Sure enough, in the afternoon, someone had one of
33
On-site Customer
How many guesses do you want in your code? How many times do
you want programmers working on lower priority work while they wait
for an answer on what you have set as the highest priority? Where pos-
sible, keep a customer with the programmers. You’ll be glad you did.
First, try hard to get someone to represent the customer locally. This
could be a non-programming project manager, a trainer, a tracker, or
just someone in the company who is expert in the area. Let them han-
dle the bulk of the interruptions, then get with the real customer
offline to double-check. If the customer and this pseudo-customer can
get on the same wavelength, this works quite well.
Second, at least try to get the real customer on site for planning meet-
ings. In those meetings there is lots of discussion about priorities, and
it’s really best if the real customer makes those trade-offs. This is also a
34
On-site Customer
Third, visit the customer. Send the programmers — all of them, if you
can — to visit the customer as often as possible. Ask questions, discuss
the application, build rapport.
Fourth, release code very frequently to the real customer. If they can’t
see you, make sure that they see the system. You’ll be releasing rapidly
anyway, being an XP team, but the need for this is higher if your cus-
tomer isn’t around, and the frequency should go up.
Fifth, expect misunderstandings and plan for them. The written and
email communications you’ll fall back on are error-prone, not because
you are bad people but because that’s how it is with the written word.
Plan frequent phone conferences and meetings, both to settle issues
and to remind everyone that even though they aren’t together, they’re
all part of the same team.
Summary
Having your customer with you is really valuable. It’s possible to sur-
vive without it – many projects do – but you’ll go faster and more
smoothly by being together. It’s the difference between being in the
car when it’s time to turn or just writing a note that says “Don’t forget
to turn at 34th Street.” When 34th is blocked and you’re in the car,
you can tell the driver how to recover. The note can’t help.
Somewhere along the way in your project, 34th street will be blocked,
and having the customer there will bring the project back on line so
smoothly it’ll be as if it never happened. And every day, the small con-
versations will add up to a lot of trust and communication between
customers and programmers. That, of course, leads to success.
35
On-site Customer
36
Chapter 4
User Stories
Define requirements with stories, written on cards.
Customers have the right to get the most possible value out of every
programming moment, by asking for small atomic bits of functionality.
Programmers have the right to know what is needed. These two rights
come together in the User Story.
Analysis can be loosely described as the process of finding out what the
customer wants. Analysis can be done very formally, with objects and
diagrams, or it can be done very informally. XP, no surprise, chooses
informal analysis: we ask the customer.
37
User Stories
Unwrap the cards. They’re hard to use in the plastic wrap. And the
unwrapping is the hardest part of the process. Whew, glad that’s over.
Now, hand everyone some cards, and let’s all take one card to test our
pen on. We each scribble something on our card. Now — we each tear
up our card!1 This gets us all in the frame of mind to touch the cards,
to write on them, and to be flexible, ripping up and replacing cards
whenever they need it.
The first few story cards are a bit difficult. But don't worry, you've got
lots of blank cards to work with. Customer, tell a story about how the
system will be used. Programmers, listen, and ask questions for under-
standing. Try to stay away from implementation questions. Now,
customer, write the story, in your own words, on a single card. If the
whole story is too big, write the essential core.
1.
Thanks to Erik Meade for this technique! Sometimes you’ll have a player who resists tear-
ing up his card. Watch for other signs of inflexibility, and find ways to help him relax into
the process. But don’t touch his cards!
38
User Stories
Sample Stories
Here are some sample stories. Each one is backed up with conversation
(see Stories are promises for conversation (page 41)), and perhaps with
some related detailed information. Each one is simple enough that the
programmers could understand it, and could see how to implement it
in a week or so.
Union dues vary by union, and are taken only in the first pay
period of the month. The system computes the deduction automati-
cally. The amount is shown in the attached table.
For each account, compute the balance by adding up all the depos-
its and subtracting all the deductions.
39
User Stories
When the GPS has contact with two or fewer satellites for more
than 60 seconds, it should display the message “Poor satellite con-
tact”, and wait for confirmation from the user. If contact
improves before confirmation, clear the message automatically.
Allow the user to add new service types to the system’s initial list.
For example, he may wish to add a special entry for getting the car
washed at the high school’s “free” wash. Include the standard fields
amount and date, plus allow the user to add any additional text
or numeric fields. Reports should automatically sum any numeric
fields. (Programmer note: story needs splitting. Please separate
text and numeric fields into two stories, plus one for the sum-
ming.)
(Split 1) Allow the user to add new service types, including the
standard fields plus any additional text fields desired.
(Split 2) Allow the user to add numeric fields to user defined ser-
vice types.
(Split 3) In all reports, show totals of all numeric fields, not just
the standard gallons and dollar amount fields.
40
User Stories
For most of the stories above, the programmers (your authors) could
see how to implement them in a week or less. As such, we felt that we
understood the stories and could implement them. In the case of the
last story, we felt that adding special numeric fields might be difficult,
and that updating all the reports to include them was a separate effort
and might take a long time. We asked that the customer split the story,
and the customer agreed.
Note that we’d really get the story split in a conversation with the cus-
tomer explaining what was up — not just with a note on the card. We
put the note in the text because we can’t have a conversation with you.
In a real XP project, use conversation wherever you can.
Each story card is carried through the project, serving as a token for
planning and implementing whatever it requests. As such, the pro-
grammers need to be able to make a decent estimate of how difficult
each story is, usually in small numbers of weeks of effort. Just how big
a story can be varies from project to project. The best way to begin is
just to write a few stories and ask the programmers if they are the right
size and detail.
2.
Alistair Cockburn gave us this elegant description of story as promise for conversation.
Buy his book!
41
User Stories How many stories do you need?
It’s better if programmers don’t write stories. We want all the stories to
belong to the customer, not just in name but in their heart. There are
some times when the programmers may help the customer write the
stories. For example, if the customer doesn’t know how to write sto-
ries, the programmers may need to work with her to find out what the
system needs to do. These conversations become the stories. It may
seem that you know something the customer will want. Talk to them
about it, but resist the temptation to write the stories. XP depends on
the circle of life, with customer defining and programmer building.
Don’t break the circle.
It may seem that there is some large technical development that needs
to be scheduled and managed. Some teams do write these as “technical
stories”, but this too threatens the circle. Customers can’t prioritize
what they don’t understand, and all too often the technical bite is too
big to fit into a single iteration. Where possible — and it usually is —
you should solve this by relating the technical task back to a real cus-
tomer need.
42
How many stories do you need? User Stories
It’s usually easy to break a big story down into two or more smaller
ones. Often the story has a very important part and a less important
part: that’s a good place to split. Other times the story covers several
related cases: consider making each one a story. Customers, don’t
worry about not getting everything. You can select all of these stories
for implementation if you want to. We just need to break them down
into bite-sized chunks for estimation.
Other times, stories will be too small. When stories get estimates of
just a couple of days or less, they can gum up the planning process. It’s
best to clip related stories together and estimate them as a group. Use
your own judgment here, but if the planning seems to slow down, or
becomes very rote, it may be time to put some stories into a group.
43
User Stories How many stories do you need?
44
Chapter 5
Acceptance Tests
Surely you aren’t going to assume you’re getting what you
need. Prove that it works! Acceptance tests allow the cus-
tomer to know when the system works, and tell the program-
mers what needs to be done.
Customers, remember that you have the right to see progress in the
form of a working system, passing repeatable tests that you specify.
Well, here’s the responsibility part of that: specifying the acceptance
tests.
Some systems put off overall testing until right before release. They
often eat into the testing time with schedule overruns, but even if they
allocate the full testing period, this slows things down. The reason is
simple: programmers forget what they did. If I write a program today
and test it a week from now, my recollection of how I wrote it will have
faded, in favor of whatever I’m working on then. So when I get the
defect indication, I won’t be able to guess quickly where the problem
is. This leads to long debugging sessions, and slows the project down.
Don’t do that — test right away.
45
Acceptance Tests
to have the computer beep at the programmer one second after she
made a mistake, there’d be a lot fewer mistakes in the world today. We
can’t usually test the system’s function every second (but see Unit
Tests, page 113, for a discussion of how hard we try). With acceptance
tests, the sooner we get them, the sooner we can make them work.
The customer responsibility is to provide those acceptance tests as part
of each iteration. If you can get them to the programmers by the mid-
dle of each iteration, the project will go faster. You will get more
business value by the deadline. That’s a promise.
What must I test, you’re probably asking. The official XP answer is,
you only have to test the things that you want to have work. Let’s be
clear about that: if it’s worth having the programmers build it, it’s
worth knowing that they got it right.
Some projects have a legacy system they are replacing, and they can get
their test data from the legacy. In this case, your job will be to select
the legacy inputs and outputs you want tested. Some projects use
spreadsheets from the customer that provide the inputs and expected
outputs. Smart XP programmers will extract the information from the
spreadsheet automatically and read it into the system. Some projects
use manually-calculated values that are typed in by someone on the
team.
One more thing. The rights refer to repeatable tests. All tests in an XP
project must be automated. We give you the ability to move very rap-
46
Acceptance Tests
idly, and to change your requirements any time you need to. This
means that the code will be changing rapidly. The only way to move
rapidly with confidence is to have a strong network of tests, both unit
and acceptance, that ensure that changes to the system don’t break
things that already work. The acceptance tests must be automated:
insist on it as your right.
We aren’t much into dire warnings and predictions, but here’s one
that’s a sure thing: the defects in your system will occur where you
don’t test. Push your acceptance tests to the limit, in breadth and in
depth. You’ll be glad you did.
47
Acceptance Tests
it once to define a test. Spill output to files and check them auto-
matically.
• Use simple file-based tools like grep and diff and Perl to check re-
sults. You can get a lot of testing automated very quickly with these
tools.
Always build your acceptance tests to be automatic, but build the auto-
mation simply and incrementally as you actually need it. It’s easy to get
sucked into investing in test automation instead of business value. Get
them automated, but don’t go overboard.
Timeliness
Acceptance tests really need to be available in the same iteration as the
story is scheduled. Think about it — you want to score the develop-
ment based on getting stories done, and the only way to know if they
are really done is to run the tests.
48
Sidebar - Chapter 5
Acceptance Test Samples
At first it can be difficult figuring out how to do accep-
tance tests. With a little practice it becomes easy.
Here are some of the sample stories from the story chapter, with sug-
gestions for how they might be tested.
Union dues vary by union, and are taken only in the first pay
period of the month. The system computes the deduction automati-
cally. The amount is shown in the attached table.
This is an easy one, of course: the test pays some employees from vari-
ous unions and checks whether they are charged the right dues, in the
first pay period. Another test checks the subsequent pay periods to
make sure dues are not taken.
Also easy. Some sample customers, with and without overdraft protec-
tion. Test what happens if there isn’t enough money in the overdraft
account — note that the story is probably incomplete. If the customer
writes the test correctly, the programmers will see and deal with the
problem quickly enough.
49
Acceptance Test Samples
Same as previous test, except that emails should be sent. Send them all
to a fixed account: have the programmers write code that reads them
and checks their formats, so the test can be automatic.
It’s tempting to look at the report manually. That way lies the dark
side. Check the report once, very carefully, then have the programmers
machine check it against the good one thereafter.
When the GPS has contact with two or fewer satellites for more
than 60 seconds, it should display the message “Poor satellite con-
tact”, and wait for confirmation from the user. If contact
improves before confirmation, clear the message automatically.
Allow the user to add new service types to the system’s initial list.
For example, he may wish to add a special entry for getting the car
washed at the high school’s “free” wash. Include the standard fields
amount and date, plus allow the user to add any additional text
or numeric fields. Reports should automatically sum any numeric
fields. (Programmer note: story needs splitting. Please separate
text and numeric fields into two stories, plus one for the sum-
ming.)
50
Chapter 6 ‘
Story Estimation
Customers need to know how much stories will cost, in order
to choose which ones to do and which to defer. Programmers
evaluate stories to provide that information. Here’s how.
If all stories were the same size, it would be easy to steer the project.
We’d just do some of the most important stories, and make note of
how many we could do during an iteration. Then we’d just use that
number to set our expectations for next time, and to select what to do
by our preferred delivery date.
“Well, we’re doing about five stories per week, and there are
ten weeks till Comdex. We’ll probably get about fifty done.
Right now, it looks like these are the ones we’ll pick. To make
sure we’re always in the best possible shape, we’ll do the most
valuable ones first. Here are the best ten, let’s do those over the
next two weeks.”
After each iteration, we’d see how fast we were going, and adjust
accordingly. And if our priorities changed and we brought in ten
entirely different stories next time, that would be great. It would just
make the product even better.
51
Story Estimation
Well, all stories aren’t usually quite the same size, but as the team goes
along, you’ll get better and better at knowing how big they are. Some
may be worth one point, some may be worth two or three, some only
one half. If the team is doing five per week, it’s just as easy to pick the
most important ten points’ worth as the most important ten stories. As
the project progresses, estimates will get better and better, and your
ability to steer gets better too.
It’s not easy to know exactly how fast you will go at first, but it’s easy
to observe how fast you do go. Then, except for obvious adjustments
like vacations and holidays, it’s best to trust your actual speed and base
your planning on how fast you’re actually going.
There are a number of ways to handle the comparison once you have
it. The most accurate would seem to be to record how long each story
actually takes to implement, and then to estimate the new one the
52
Story Estimation
same way. But, it turns out, that doesn’t work. The actual time to
implement a story is made up of two components — how difficult the
story is, and how fast the team is going.
Back when you implemented the first story, perhaps support require-
ments were low and there weren’t other projects coming down. The
programmers spent most of their time programming, and they got the
story done in a week. But now, there are support requests coming in,
new projects to plan, and lots of meetings going on. That same story,
today, might take two weeks or more. Knowing that our new story is
the same as one that took a week is good, but it isn’t enough.
Here’s what to do. We’ll estimate story difficulty, using a simple point
system. Local naming rules for these points apply. Some teams call
them perfect engineering weeks, but other teams find this causes too
much pressure trying to fit perfect weeks back into the real weeks that
wind up being issued to us. Some projects call them Gummi Bears. No,
really. You might prefer Story Estimation Units, or Bucks. Ron’s favor-
ite name, this week, is just to call them points.
Jill says, “I think this is just like that one a couple of iterations ago, the
historical report, where we had to connect to the database, but also
had to output those files.”
Jack replies, “Yes, except this one we have a lot more files. I think this
one is harder.”
“You’re right. Probably about fifty percent harder. How many points
was the historical report?”
53
Story Estimation
Jack, flipping through cards, finding the historical report, “It was a one
pointer. Let’s make this story one and a half.”
It really becomes that simple. A quick discussion about what old story
the new one is like, maybe a check of the old card to see how many
points the old one was, and you’ve got an estimate. The team is doing
many stories over many iterations, and gets very good at this.
Try to begin with a story that you can imagine how to do. If the story
is so new to you that you can’t imagine how to do it, then you need to
start with a Spike Solution, described on page 55. Basically a spike is
just an experiment, where you do enough work to get a good feeling
for how to implement the story.
If you feel that you can do a story in one perfect week, give it one
point. If you feel you can do it in two, give it two. If it feels like three,
54
Story Estimation
OK, give it three points, but consider asking the customer to split the
story down into two or more. And our advice is that if you start think-
ing you could do it in a month, your intuition has become too fuzzy. If
your best estimate is four weeks or more, please have the customer
break the story down.
As soon as you have estimated the points for a few stories, start doing
it both ways. Ask your intuition about its difficulty, get the points that
way. Then compare the story to the others, asking yourselves which
ones it’s like in size. Check the points, see if you get the same answer.
For a new project, with fairly experienced programmers with reason-
able intuition about the application, a team can go over a hundred or
more stories in a day or two, and rate them by difficulty.
It usually turns out that your initial ratings will be pretty accurate. But
what’s more important, as you remember from your rights on page 17,
is that you can change those assessments at any time. When you learn
that something is easier or harder than you thought, you get to update
the ratings on cards that are similar. The majority of times, you’ll be
rating stories as easier than you feared — after all, the whole team is
learning, and for lots of stories, you’ll even have built little tools.
Sometimes, though, you may need to upgrade the story’s difficulty.
That’s not a bad thing, because the truth is never a bad thing. Know-
ing the cost of things is how an XP team steers the project to success.
Spike Solution
It really is easy to estimate the difficulty of stories compared to other
stories. Brainstorm what the tasks are a bit, then see what you’ve
already done that is about that size, or twice the size, or half, or
one-and-a-half, as we described above.
But what if you haven’t done anything like this story before? What if
it’s the beginning of the project and you haven’t done anything at all?
55
Story Estimation
through the entire problem in one blow, not to craft the perfect solu-
tion first time out. You need to know how long it will take to do
something. To know that, you need to know how you’ll go about it,
but you don’t really need to have done it all.
Here are some examples. None of these are made up — they are actual
descriptions of spike solutions that people have used to learn how hard
something was.
Balancing an account.
We are going to have accounts, like bank accounts, with trans-
actions in them. We will need to compute the account balance.
This example was actually done live, by Ward Cunningham and
Kent Beck, at an OOPSLA conference a few years ago. The first
spike solution was to take a collection of numbers and add them
up. Sounds dumb, doesn’t it? But that’s the essence of getting
the account balance: take the numbers and add them up.
We mention this one first because it is so primal and simple. By
doing this experiment you realize the fundamental simplicity of
getting the account balance: just add ’em up. It pulls away all
the separate issues of the Transaction class and the MonetaryA-
mount class and the ACID properties of the database, and lets
us see balancing for what it is: get all the stuff and add it up.
Naturally the spike doesn’t stop here. We might build a little
Transaction object with an amount in it, make a vector of them,
loop, add ’em up. We might even make a little Monetary
object, put it in the Transaction, make a vector, add ’em up. By
this time, we’re convinced: balancing an existing account will
take less time to code than this description took to write up.
Formatted reports
We are going to have a bunch of formatted reports. They will
have different columns, and they all have to be totalled and sub-
totalled. Maybe we don’t have a reporting tool — maybe we
do. In either case, we need to estimate these reports. So we
56
Story Estimation
spike.
The spike? Make a collection of record objects — objects with
data in them but no behavior. Give them a key field and a cou-
ple of values. Code the report, using whatever tools you have.
First just print ’em, to get a feel for the shape of the loop. Then
print a total at the bottom. Then print a subtotal on each
sequence break. This whole experience will probably take an
hour or two. So the basics of reporting are easy.
Now look at formatting. Use your tools to format some num-
bers and dates in a few quaint and curious ways. See how hard it
is to do.
Based on the report descriptions, number of fields and keys,
and the hassle of formatting, estimate doing a report. Multiply
by the number you have to do. (I’d assume the customer won’t
like any of the reports the first time and will change them an
average of once each.)
If there are a lot of reports and the cost seems too high, do an
experiment with a report generator or some table-driven thing,
so you can see whether the customers can do their own reports.
Keep it simple, no more than a day.
Database Access
While it runs, the system needs to access your SQL database to
find the details for each item it processes. You want to know
how long it will take to code this, and you also want to know
how it will perform, in case you have to do something special.
You need a sample database to access. Anything will do. Write a
simple SQL query that will return a few records. Test it in inter-
active mode. Code it into a program and send it across through
your database connection. Note what goes wrong. Probably
you won’t get it across for a while, then probably you won’t be
able to deal with what comes back. Might take a day or so to
get the first one working. If you’ve got experience and good
DB connection software, it might just be an hour. After an hour
or a day, you know what the code looks like to talk to the DB.
57
Story Estimation
Automatic Email
The system has to email the support people when certain things
happen, so as to beep their beepers. How hard will this be? No
biggie to detect the error or format the message. We can esti-
mate that. But it could be awful to send the email, we’ve never
done that.
I just surfed the web, looking for “email protocol”. Item 22 on
my list is a Java applet that tells me about the SMTP protocol
and lets me type in example lines. Looks like it is all done with
text messages. Should be easy.
Write a little program to connect to the email API on your
machine, and send a simple email to yourself. It probably will
barf. Debug it. After an hour or so, or a day or so, you’ll get it
to work. Now you have the sketch of the code for doing email,
and you can estimate the story.
58
Story Estimation
In this case, you are not here to drain the swamp, you’re just here to
kill one alligator. As soon as that gator is dead, and you can estimate
how long the solution will take, move on
Estimation Summary
We programmers estimate stories in points, based on perfect engineer-
ing time, and we keep track of how many points we can do in each
iteration. That lets our customer steer us to success by choosing the
best combination of stories within the time and money available.
At the beginning of the project, use your group intuition to get that “I
could do that in a week” feeling, then set the initial points based on
that intuition. We recommend giving the points a neutral term, rather
than weeks, just because it keeps the tension down a bit as you track
your implementation velocity. As soon as you begin to get experience
with the relative difficulty of the stories, you can, and should, update
the points for the remaining ones. This gives the customer the best
opportunity to steer you to success.
59
Story Estimation
60
Interlude
Sense of Completion
XP’s nested planning and programming cycles keep the
project on track, and provide a healthy sense of accomplish-
ment at frequent intervals.
61
Sense of Completion
Writing the tests one at a time, test and code on the task until all the
necessary tests run. Take a moment. Task done! Let yourself feel that
little triumph. Cross the task off your list. Give it a little flourish. Task
done!
Is the code in good shape for release - not necessarily finished, but able
to run all the tests? Maybe it's a good time to run the tests and release
some code. Code released! Life is good.
Work on one story at a time. When all the tests for that whole story
run, take a moment. A whole story is done! That's very good. The cus-
tomer will get that card back marked complete. Life is very good.
When a story is done, it's definitely time to run all the tests and release
your code.
Have a little ritual that goes with code release. Maybe get one of those
little bells that you whack on the top when you want service at the
counter. Give it one whack whenever you successfully release. When
someone else gives the bell a whack, give them a little appreciation -
soft applause, quiet cheers of “Yaaay”.
62
Sense of Completion
Customers, you should feel this moment as well. Some of what you
asked for is complete. A bite has been taken out of your problem.
Enough bites and you'll have something of value, something you can
use.
63
Sense of Completion
64
Chapter 7
Small Releases
The outermost XP cycle is the release. Small and frequent
releases provide early benefit to the customer while provid-
ing early feedback to the programmers. Here are some
thoughts on how to make it happen.
If your customer knows now just exactly what she wants, and if by the
time you’re done, she’s still going to want the same thing, it may be
the first time in software history that this has happened. Probably there
will be a prize for you somewhere. It’s much more likely that as you
steer the project, you’ll be changing direction, a little or a lot. As you
find out what’s easy to do and what’s more costly, you’ll be making
better and better decisions about what to do and when to do it.
Even though you’d like to, we know that small releases ares impossible
in your situation. We accept that you just couldn’t do it. But just imag-
65
Small Releases
ine for a moment, that you could actually get some useful subset of the
software into the customers’ hands. Not something they’d demo and
comment on and set aside, but something they’d use every day. They
would learn a lot about what they really wanted, and you’d be that
much more capable of steering the project to success.
Even better, if the releases are actually useful, you’re delivering real
business value to the customer, which will keep them happy and keep
them coming back. It will build confidence and keep them supporting
the effort, even if once in a while things get rough.
OK, you’re convinced that it would be nice, but it just couldn’t hap-
pen with your product. Your product is all-or-nothing, just couldn’t
really be useful except when it’s really pretty done. Well, believe us, it’s
worth thinking about it. Of all the projects we’ve been involved with
that failed, almost all of them would have been successful had they
delivered a little functionality over time, instead of waiting and waiting
and waiting. So let’s take some examples of projects that couldn’t pos-
sibly deliver something early, and try to think of things we could
deliver early after all. You probably won’t be so lucky that we’ll pick
yours and solve it – fact is, you know more about your project than we
do – but practicing on ours may help you find your own small releases.
Well, setting aside the obvious value of a payroll that didn’t take out
your taxes, let’s think about this a bit.
66
Small Releases
Is there some complex calculation that the old payroll can’t do quite
right, or that needs updating soon? How about replacing that part of
the legacy program with transactions generated by a little chunk of the
new program. The new program has to have that part written anyway,
and now you can save the cost of updating the legacy program.
Or, how about diverting employees from the legacy stream who are
capable of being paid by the new program and paying them there? The
new program would get exercised, and at least those individuals would
get the benefit of being in the new system. If a few pay cycles down,
more features let you divert more people in, great. And if someone’s
pay situation gets too complex for the evolving new program, just
divert them back to the legacy.
Now each of these schemes has a little extra cost associated with it, in
order to bring it on line. But the benefits would be immense in the
delivery of improved value, the feeling of accomplishment, the better
feedback and the better focus of the whole team on delivering value.
Personnel System
You’re replacing your personnel system with a new one. Obviously you
need an integrated database and can’t release the system until it does
everything the old one does, plus all the new things you’ve thought of.
There’s just no way you can release this incrementally.
67
Small Releases
This could turn into one of those horrible tail-chasing exercises where
there’s always just one more feature that you have to put into the new
system before it’s ready. Just don’t go there.
Instead, what if we made our database compatible with the old one,
but with new relational tables for new features that are needed, and
delivered some of those new features. We could produce new reports
that summarized the use of the new features, and we could produce
new versions of some of the old reports to show combined values.
Maybe we could do the GUI trick like from the payroll example, where
we gave the users a better way to input certain transactions. Maybe we
could even put up a little web interface and let employees enter their
own address changes and telephone numbers.
Tax Package
Here’s one we wish we had thought of a few years ago. A professional
tax package isn’t useful unless it does some minimum number of
forms, and the minimum is very large. So building a new Win-
dows-based professional tax package is a multi-year investment. And
there’s just no way to release just part of it.
How about using individual forms calculations from the DOS program
and replacing them incrementally with the new engine?
How about using the existing forms print capability of the DOS ver-
sion, together with its non-WYSIWYG output display, until the screens
that look just like tax forms are done?
68
Small Releases
Well, how about releasing a single module that controls some specific
machine, and that plugs into the legacy system, emulating an old-fash-
ioned controller. Surely your micro-based system could raise and lower
a few electrical levels to make it look like the old modules, and even
respond to centralized signals telling it what to do.
It might be a bit tricky, and it would be wasteful in that when the last
distributed module went in, all that centralized control code would be
unnecessary. But in the years between the ability to release one little
controller, and the release of the last bit of the fully distributed system,
you could make so much money and learn so much!
OK, we admit it, we don’t know anything about air traffic control. But
we do know that a current theory is that the planes should control
themselves, communicating with each other and adjusting their paths
the way that birds do, maintaining certain preferred distances, increas-
ing their desire to move away when they get too close, and so on. One
69
Small Releases
of the problems with this system is that not all planes would be smart
enough to do this, and what would happen if a plane lost its ability to
sense and be sensed by the others.
Summary
We hope you’re getting the idea. Inside almost every large program
there are lots of little programs trying to get out. You might have to do
a little extra work around the edges to get these pieces useful on their
own, but it can be well worth your while. You’ll learn lots more, and
you will actually be delivering value. Would that be cool, or what?
So put all your thoughts behind this simple idea: release little bits, to
be used by real customers, very frequently. It will allow you to deliver
the most value in the shortest time, with great confidence and low
stress. We also think it will grow new hair on your head and help you
lose weight, but we’re not sure about those two. The rest – we’re sure.
70
Chapter 8
71
Customer Defines Release
You’ll hand your story cards over to the programmers for estimation.
Then you sit back and listen, and wait for questions. If the program-
mers want to know whether you want a Windows-specific GUI or one
that is web-based, you’ll answer them. (Sometimes you’ll have to get
them to explain their question first. That’s OK, keep after them until
they do.)
Sometimes the programmers will find one of your stories too hard to
estimate. That doesn’t mean you can’t have it, it just means that it’s
got too much in it for them to make a valid single estimate. This is
important to you: you want the estimates to be as good as possible, so
that your selection of what to do and what to defer is as good as
possible.
Sometimes you just have to explain what you meant in the story. When
this happens, consider ripping that story up and writing a new one. It
feels good and shows the programmers how flexible you are.
Sometimes, the story is just too big. In XP, the programmers aren’t
allowed to estimate things in months of effort. We’ve learned that
72
Customer Defines Release
those estimates are too often wild guesses. So even if the programmers
are pretty sure about a big story, we don’t let them estimate it that way.
Instead, they’ll ask you to split a story. Often this will be obvious and
easy. You asked for a lot of things in one story. Just split the things out,
tearing up the old card (feels good, doesn’t it?) and writing new ones.
If, while you’re at it, you can keep things of like priority together, that
will help later. An easy way to do that is to choose the most important
thing, write it on a card, then the next, and so on. Don’t worry about
getting too many card. Too few is a problem, but if there are too many,
you can just clip them together.
After a while (and the first time around it could be a long time), the
programmers will have assigned a cost number to each story. Different
teams call this number different things: Difficulty Points, Perfect Engi-
neering Weeks, Bucks, even Gummi Bears. Whatever the name, these
numbers are the same thing: estimates of how difficult each story is to
build. With just one more fact, you’re ready to choose the stories for
the release plan.
To know what to schedule for the first release, you need to know how
fast the team can implement stories.
What?, you may be saying. I need all these things done by the release
date. I want it all. Well, as the song goes, you can’t always get what you
want, but if you try, you can get what you need. Here’s how you do
that.
73
Customer Defines Release
The programmers will tell you how many points they can do in a given
period of time. We call that number Project Velocity. If they’re estimat-
ing with perfect weeks as the basis, they’ll probably do one point per
programmer, every two or three calendar weeks. If they’re estimating
in perfect days, they’ll probably do one to three points per program-
mer per week. (Don’t get excited, those guys aren’t going faster,
they’re just measuring feet per minute instead of miles per hour.)
At the beginning of the first project of this kind for the team, this
number will be a pure guess. But as soon as you get experience doing
the stories, the team will measure the actual velocity. You can use that
experience-based number to steer the release. And that’s your job.
Once the stories are all estimated you have a bunch of story cards back
in your hands, with business value that you understand, and difficulty
points of one, two, or three. Sometimes one and a half, but don’t
worry, someone in the room probably has a calculator. The velocity is,
say, five points per two week iteration.
You know how many iterations there are between now and the sched-
uled release date. Suppose there are six. Then you should plan to get
about thirty points done by then, five times six. Go through your cards
and pick the ones with highest business value that add up to no more
than 30 points. That’s your plan.
Sounds easy, doesn’t it? Well, it is easy, except for one thing. You may
well wish you could have 35 points instead of 30. In fact, you surely
will wish that, since all things being equal, more features are generally
better than fewer.
Our advice is to go with the numbers. If the numbers say you can do
five points per week, and you could get your 35 total points if the team
could just do six, resist your temptation to plan for 6. Do work to get
the team’s velocity up. But plan based on your actual speed, not your
hopes. That keeps your plan closer to reality, and keeps you focused on
your most important role, steering the project to success.
74
Customer Defines Release
The team will go faster if it’s winning than if it’s falling short. Give the
team a chance to exceed their goals — they may surprise you. And your
own experience of the project will improve as you learn to guide it to
success.
As the customer, you define the release by selecting the best combina-
tion of stories that the team’s performance suggests can be
accomplished. As you learn more about performance, and more about
how the system is shaping up, you’ll adjust and tune the combination
of stories you request. An XP team will always adjust immediately, and
work on just the things you need to make the system as good as it can
be within your time and budget.
If you knew how long it would take to build each desired fea-
ture in your product, you could do a really good job of picking
the best mix of features given your time and money budget.
No one can tell you exactly, far in advance, just how long it will take to
build software. We’re just the only folks who admit it. What we can do,
75
Customer Defines Release
If you know how hard it is to build each piece of what you have in
mind, and you know how fast you can build things, you can estimate
very accurately what you can have by your due date — or when you
will have any batch of features done.
In Story Estimation (page 51), we describe how you get a story esti-
mate. Here’s how you use them.
The customer brings to the meeting all the stories she would like to
have for the first release. The programmers divide up into small
groups, and take batches of the cards, and pencil onto the card their
estimate of its difficulty. If they have questions about the story, they
ask and the customer answers. The programmers are quickly discussing
the tasks they’ll have to do for the story, and then they put down the
difficulty.
All the groups look at all the stories. The rule we use is that if a second
group thinks the story is easier, they’ll check with the first group and
ask how they got their answer. If they still think it’s easier, they can
reduce the number on the card. Formally, we only let folks reduce esti-
mates, but of course if the discussion identifies something forgotten,
the programmers can increase the difficulty rating.
Rate the easiest stories as one point. Rate the others as twos, threes,
and so on.
Soon (when you get good at this, you can do over a hundred stories in
a morning), all the stories have a difficulty estimate.
You give the stories back to the customer, and tell her what the antici-
pated velocity will be. This is a simple number: “We estimate that the
team will do 10 points in each iteration”.
Based on that estimate, and the due date, and the customer’s knowl-
edge of the value of each story, she can select the ones that will make
76
Customer Defines Release
for the best release by the date. That selection of stories makes up the
release plan.
Some teams find it interesting and comforting to lay the cards out
against the calendar — in the first iteration, we’ll probably do these 10
points, in the second these, and so on. Experienced teams don’t even
bother, because they know that the priorities will change and that
they’re completely comfortable working on whatever the customer
comes up with.
Either way, the main thing to remember is this: this is a plan, an esti-
mate, not a fact. What will happen isn’t just what you laid out here.
It’ll be a lot like that, but it’ll be somewhat better or somewhat worse
for most projects. Once in a while you’ll get really lucky and do lots
better, and sometimes you might not come close.
However it goes, the good news is that you don’t have to wait till the
end. Remember that in the iteration plan, every couple of weeks, you
have a chance to reassess where you are and how fast you are going. So
you have every opportunity to steer your project to success.
77
Customer Defines Release
78
Chapter 9
Iteration Planning
Inside each release, an Extreme team plans just a few weeks
at a time, with clear objectives and solid estimates.
As the XP customer, you have the right to get the highest possible
value out of every programming week. XP projects work in short “iter-
ations” of two or three weeks, and you select the work to be done in
each of those iterations. Your mission is to select work that will maxi-
mize the business value completed by the end of the iteration.
79
Iteration Planning
Your role in the meeting is to present and describe the stories one at a
time, while the programmers will quickly define programming tasks for
each story. When all the stories have been presented and tasked, the
programmers will sign up for work, and estimate the tasks. If there’s
too little or too much to do based on the stories you bring in, you get
to decide which ones to add or remove.
When all the stories have been explained and tasked, there is a brief
flurry of activity while the programmers sign up and estimate the work.
Usually at the end there is some balancing to be done. Programmers
with too much to do trade with those who don’t have enough. And
often, you will need to help out by removing some less important work
from the board. Occasionally you’ll even get to add more. In all cases,
it’s most effective to do the adjustment rather than try to cram just a
little more in. It is tempting to push the programmers to try a little
harder, and usually they’ll give in at the slightest push. But in the end,
their actual velocity, not their good wishes, are your best friend in
steering to success.
If you are planning to meet someone for dinner, in front of their hotel,
and it will take you a half-hour to get there, it won’t help to tell them
it’ll be fifteen minutes. They’ll just have fifteen anxious minutes of
waiting. It’s better to plan your dinners, and your projects, based on
your best measurements of actual performance.
The team won’t lay back. When they get things done early – and often
they will – they’ll ask you for more to do. When that’s the way you
work, everyone gets to feel good. And folks who feel good work
together better.
80
Iteration Planning
This begins the exploration phase of the iteration plan: explaining and
understanding the stories to be done. The customer has selected the
stories to be implemented during this iteration. She reads each story
card, and the team asks any questions they have until they understand
the story. Each team member is responsible for having a good grasp of
what the story requires.
Often, the approach will be clear to all, as the story will be a simple
extension of existing features. Other times, there will be two or more
possible approaches. Of these, the team picks the simplest that could
81
Iteration Planning
Now we begin the commitment phase, signing up for work and esti-
mating it. After all the stories and tasks are presented, programmers
sign up for the work to be done. It’s possible to sign up for all the tasks
for a given story, or just for specific tasks. Each programmer signs up
for as much work as she believes she can accomplish during the itera-
tion, making estimates and keeping track as she goes along.
It is usually best to estimate at the task level, even if you are signing up
for the whole story. If you’re not clear on what a story or task entails,
ask other members of the team. You might even ask how long similar
things are taken, but try hard to use your own estimates, not to be
swayed by the opinions of the other programmers.
82
Iteration Planning
sign up for all the tasks on a story, rather than picking and choosing.
The reason is that it’ll help ensure that stories get fully completed.
Suppose that story A has 10 points worth of tasks in it, and I sign up
for seven of it and you sign up for three. Well, you might work on
other important tasks, especially if they add up to a story, and put the
three off. That could put story A in jeopardy. How would the team
notice this problem?
You might get lucky. Someone else might notice the three tasks not
getting checked off the board and raise the issue, but if no one is “on
the hook” for the story, it may not get noticed.
If no one feels responsibility for the story, then only the tracker is likely
to notice problems. This puts a complex burden on tracker to notice,
and to cause tasks to be reallocated. This isn’t a good thing. Far better
to have a single programmer feeling responsibility for each story. The
simplest way to do that is to sign up for whole stories where possible,
and to have an individual feeling responsibility for the whole story even
if she hasn’t signed up for all the tasks. That’s what we recommend.
If there are tasks on the story that you just can’t do, then pair with the
person who can do them. That way you’ll learn something and will see
to it that you story gets done. If it’s physically impossible for you two
to work together, you have to fall back on far less effective approaches,
like following up, nagging, begging, and so on.
One more point on signing up for stories. What if the task brainstorm-
ing done by the team isn’t sufficient to complete the story? What if no
one remembered that you have to create an input record format? Well,
83
Iteration Planning
we hope someone will notice, but if there’s no one on the hook for the
whole story, chances are too good that no one will notice.
The team gathers around a whiteboard for the iteration plan. It often
helps if someone acts as facilitator or director to keep things on track.
The customer stands at the whiteboard and writes the name of one
story on the board, then describes what the story means. The team
asks questions and the group discusses the story, until they’re sure they
get it, but no longer.
Then the team proceeds to brainstorm the tasks for that story, while it
is fresh in mind, writing short names for the tasks on the whiteboard,
right under the story name. It might look like this:
Then the customer puts another story on the board, and the process
continues until enough stories are on the board. (See Project Velocity.)
84
Iteration Planning
Now it’s time to sign up. Programmers who are particularly interested
in some area jump up and put their initials on the board by the things
they want to do. In more civilized environments, you might ask for
volunteers, but it’s more fun to scramble. Other programmers who
don’t care what they work on this iteration will hang back and pick up
whatever is left over. If you’re a newer member, it’s usually a good idea
to sign up early, so you can balance your need to learn with what you
already know.
As programmers sign up, they estimate each thing they sign up for.
They stop signing up when their estimates reach their capacity for this
iteration. (See Project Velocity.)
If there is too much on the board, the customer decides what to take
off, and adjustments are made to signups. If there is too little, the cus-
tomer adds more stories as at the beginning. Continue until the board
is just right.
At this point, all the stories and tasks on the board are signed up and
estimated. Our original story and tasks might look like this, with Chet
signed up:
Note that it isn’t necessary that a single programmer sign up for all the
tasks on a story, and sometimes there’s a good reason not to. In gen-
eral, though, it’s nice to have a single person responsible for making
sure the story gets done.
You may find another way of doing iteration planning that’s better
than this one. If you do, please let us know. Just remember the three
85
Iteration Planning
86
Chapter 10
Truth is, once you have broken down a story into tasks, you often
already really know how to implement it. Often the best thing to do is
just to proceed to “Test-first programming”. But if you aren't clear on
just what to do, or if you would like a little reassurance, or if you
would like a few additional ideas, have a quick design session.
Get a few people together, and spend a few minutes sketching out the
design. Ten minutes is ideal - a half hour should be the most time
you'll spend to do this.
Do a CRC design with a few cards, or sketch some UML on the white-
board or a sheet of paper. The idea is to understand the objects
involved in what we're about to do, and to understand how to imple-
ment them, or change them, to accomplish the task.
It's important not to spend much time at this, because we'll do the
most learning in the first few minutes. After that, the best thing to do
is to let the code participate in the design session - move to the
machine and start typing in code. Proceed test-first if you can. Other-
wise perhaps work in a workspace or write some simple objects to get
the feel of the solution. Listen to what the code tells you about what it
wants to be. Then go for the real thing.
87
Quick Design Session
Don't waste time arguing over different alternatives. Hear those alter-
natives - embrace each idea - make sure you understand it. Then pick
the simplest one that could possibly work, every time. If a couple of
ideas seem plausible, try them both for a few minutes, pick the simplest
one that could possibly work, and get to it.
88
Chapter 11
Programming
It’s called Extreme Programming, after all. Here’s how we
do the programming part of things.
In the iteration plan, the team received stories from the customer,
broke them down into tasks, and signed up to do them. Here’s how
that “doing” gets done.
The pair work on just one task at a time. Programming is hard enough
without trying to do more than one thing at a time. After completing
each task, the pair will integrate it into the software already released,
then run all the unit tests in the entire system, making sure that they all
run at 100 percent, and they’ll then release the tests and code for their
task into the repository. Tasks are released one at a time because the
project goes best when everyone is working on the most current code
all the time. The best way to do that is to integrate often. We call that
Continuous Integration (page 96).
As the pair work, they’ll proceed by writing tests first, then “fixing”
them to work. Test a little, code a little. There’s a long example of this
89
Programming
Testing first also gives us tests. Tests – called unit tests when written by
the programmers for the programmers – are what lets us go fast and
fearlessly. Every feature that XP programmers build must have Unit
Tests (page 113), and those tests must test Everything that could possibly
break (page 265). And every time we release any software, each and
every one of those unit tests must be run, and must score 100 percent.
As the pair works, they will write new classes, but of course they will
also be using existing classes. Often they’ll find that they need to add
features to those classes, in support of their task. When this happens,
the pair makes the necessary changes. We call this process Collective
Code Ownership (page 93). It just means that everyone owns every
class, so that anyone is authorized to improve it at any time. But wait,
you’re asking, why doesn’t that lead to chaos and unreliability, as peo-
ple who don’t know my class work on it? Well, read that section to find
out. Here’s a hint: think about how pair programming, unit tests at
100 percent, and continuous integration might work together.
As we code, we try hard to express our intention first, then the algo-
rithm. That idea, too, is demonstrated in Test-first, by Intention on
page 129, so be sure to check it out. The basic idea is this: when we
learned to program, we learned by solving specific problems, like go
through all the personnel and compute the total of their salaries. So we
thought immediately about the algorithm, ending up with something
like this:
90
Programming
totsal = 0.0;
for ( i = 0; i < nPers; i++) {
pers = persons[i].
sal = pers.salary();
totsal += sal }
return totsal;
Now when you run across that chunk of code a few weeks later, it only
takes a few moments to figure out what it is, namely compute total sal-
ary. And if it’s expressed this way, it’s not much harder or much easier:
totsal = 0.0;
for ( i = 0; i < nPers; i++) {
totsal += persons[i].salary() }
return totsal;
Expressing intention makes the code clear the next time we, or other
programmers, pass by. Since we share the code ownership, the next
person passing through may well be new to the area. If the code says
things like “totalSalary”, they’ll get it more quickly, and they’ll be able
to change it more reliably. They’ll more readily notice that the code
exists, and will therefore reuse existing code instead of accidentally
duplicating the loop that they didn’t notice, buried somewhere else
strange. And if the meaning of the method is clear, we don’t need to
write a comment or a document about it, and if the method is where it
should be (like on the Person class), we won’t have to draw roadmaps
and do training for everyone. Want a method about persons? Look on
the Person. This makes everything go more smoothly and quickly. It’s
a good thing.
91
Programming
Working in pairs like we do, and sharing ownership of all the code, it’s
a good idea if we all program the same way. We need to name things
similarly, so we’ll all know where to look. We need to format things
similarly, so we’ll all see code that looks like we expect. We need to cre-
ate objects in standard ways, and so on. So every XP team needs to
have a Coding Standard (page 97). The team needs to agree on it, so
don’t worry too much. Having everything look the same helps every-
one go fast, so be reasonable about your favorite curly-bracket style
when it comes time to decide. You’ll quickly adapt to the new stan-
dard, so no stress, OK?
Now, with the tests all running and the code simplified, we’re ready to
release the code. That can be a moderately complex process, and we
talk about it in some detail in Releasing Changes (page 145). The basic
ideas are simple: keep the unit tests at 100 percent, so there are no
regressions; release often, so that there will be few collisions from peo-
ple editing the same classes; then resync with the rest of the team, so
that the system stays in continuous integration; use a single point of
integration to serialize updates. Good XP pairs release software multi-
ple times per day and retest the integrated system every time. The less
time you wait, the fewer problems you’ll have.
There you have it. Task done, built by a pair of programmers, testing
first, expressing intention, maintaining a simple design through start-
ing simply and Refactoring (page 95) as needed, following an
agreed-upon coding standard, and releasing code frequently to keep
everyone notching forward together. Check out some important
details below.
92
Programming
As you work with code, you’ll inevitably find that there’s behavior you
need on objects that you weren’t planning to work on. You’ll always
find that there’s commonality between what you’re working on and
some other object you weren’t thinking about when you started. You’ll
always find behavior on one object that belongs somewhere else. You’ll
find objects that you were planning to work with that are recalcitrant,
somehow resisting your attempts to use them. What should you do?
Well, if you owned those objects, you’d just fix them, wouldn’t you?
Yucch, this is wrong, you’d think. Let’s fix it. Whack, wham, a little
refactoring and now it’s all better. Then you’d move on.
If you didn’t own those objects, it’d be a different story. You’d have to
find the owner, explain your problem, talk him into the need for the
change, wait for him to whine and complain about how much he has
to do, then wait again while he gets around to making the changes you
need. Then, of course, they wouldn’t be quite right, either because he
didn’t understand – or because you didn’t.
The first way is better. Therefore, own everything. There you are, your
problem is solved. If an object needs to help you, own it, and make it
help you. If its interface isn’t quite right, own it and fix it. If it doesn’t
have quite enough power in some method, own it, and beef it up. If it
doesn’t inherit the right stuff, own it, and move it.
Simple Design
93
Programming
We all strive for simple and clear design, don’t we? Of course we do.
But in XP, we take it to extremes. At every moment in time, we want
the system to be as simple as possible. That means that we want no
additional functions that aren’t used, no structures or algorithms that
are more complex than the current need would dictate.
Code written like this, tested intensely, very expressive, free of duplica-
tion, and of minimal size, is a delight to work with. It’s a delight to
94
Programming
write as well, and of all the joys of XP, seeing this craftsmanlike simple
code emerge is one of the greatest.
Refactoring
We keep the code simple at all times.This minimizes invest-
ment in excess framework and support code. We retain the
necessary flexibility through refactoring.
Instead, when the design wants to change, as it will, change it. Put in
the new capability you need; move the functionality to the class where
it wants to be; create the new helper class that’s called for. Then bring
the entire system into line using the new capability, just as if it had
been there all the time. Nothing else is really good enough – every-
thing else leaves you with cruft growing on your code.
Refactoring is a formal process (but not a difficult one) that lets you
change the code without breaking it. Each refactoring step is revers-
ible, so that you can try things and if you don’t like the outcome, put
95
Programming
them back. This lets you have a curious, experimental, learning atti-
tude about your code. Hmm, this seems a bit off, let’s refactor it this
way. Oh, that’s better, we’ll leave it. Or, um, that’s not so good, let’s
put it back.
One more thing: one reason people don’t change existing code is that
they’re afraid they’ll break it. But you have all those unit tests that ran
at 100% when you started. When you’re finished with a little refactor-
ing, run them again. If they break, see what happened. Most likely, you
broke something, although sometimes you’ll just find that the test
needed changing to match the refactoring. The tests are your safety
net, protecting you from breaking the system during fast evolution.
Just don’t tell anyone we have a safety net – they wouldn’t let us call it
Extreme Programming if they knew we work with a net.
Continuous Integration
Integration is a bear. We can’t put it off forever. Let’s do it
all the time instead.
Here's an idea. Let's divide up the system. We'll all write our share of
the objects. It should only take a few months. We'll all test our work.
Then, when everything is written, we'll just integrate the code in a
week or so, run a few acceptance tests, and release.
Does this sound like a recipe for disaster, with mismatched method sig-
natures, objects that don't work as their users expected, and a system
that won't even load, let alone actually run? We sure hope it sounds
that bad.
96
Programming
Well, if your system already takes ages to build, you're on your own.
You'll have to get it down any way you can. But if you're starting
clean, it's really quite easy.
Build multiple times a day. If the build starts getting slow, get right on
it. Keep inter-file dependencies low. Set up your make dependencies to
keep as many files as possible in object form except when they're actu-
ally edited. Use dynamic loading where possible. Build intermediate
partially-linked object files. Build lots of DLLs wherever possible.
Experiment with different structures, learn what works best. But above
all, keep build time low.
Keep after it. Effort spent improving the build is more than repaid in
reduced integration hassles.
Work toward continuous integration. And while you're at it, keep the
unit tests fast.
Coding Standard
I can always read my own code. But wait, it’s all my own
code.
97
Programming
We all code to an agreed coding standard. This ensures that the code
communicates as clearly as possible and supports our shared responsi-
bility for quality everywhere. It doesn’t matter so much what the
standard is, as that everyone use it. Yes, we know it sounds regimented
at first, but it will pay off. Suppose someone went onto your computer
some night and reformatted all your code. Not only would you change
your password, but you’d be slowed down. You might even go to the
trouble to format it all back, because you need to be able to just glance
at the code and see what it’s up to.
Now, it turns out that it isn’t the specifics of your favorite formatting
that matter, it’s your familiarity with them. So push a bit for your
favorite style, but be prepared to bend and go with the team’s choices.
In a very short time, if you use the new style, you’ll get just as good at
it as you are with your favorite. You may even come to like the other
style better. No, really, it happened to us.
Now, what about the specifics? Easy and simpler is better. If you’re
programming in Smalltalk, our advice is to adopt Kent Beck’s Best
Practice Smalltalk Patterns as a whole. They’re well-thought out, con-
sistent, and simple. If you’re not lucky enough to be programming in
Smalltalk, you’re on your own. Look for some industry or local stan-
dards in your language, and adopt those. Here are some key topics
we’d think about in our standard:
98
Programming
tion, to explain something you just can’t simplify, and so on. Don’t’
write them when they don’t really help.
Method size. Keep methods small. If the code can’t be seen in one
display screen, it’s too long. It’s probably too long anyway. While
you’re at it, avoid those big comment blocks at the beginning of the
method - they obscure what matters, the actual code.
99
Programming
40-Hour Week
One time on your author’s common project, C3, the entire team went
into a heavy-overtime mode, in order to meet some delivery date. (Yes,
even extreme teams can get up against a deadline once in a while.
Scope had not been managed properly, and we were up against it.) We
worked really hard for a few weeks. Most of the team were coming in
on weekends, in the middle of the night, and generally acting like we
were in Silicon Valley instead of Michigan.
After the crunch was over, we all took some time off and then got back
to it. We looked upon the work we had done in the overtime period,
and unlike the Creator, we did not see that it was good. In fact, it was
bad1. We found poorly written code; we found untested code; we
found unrefactored code. We found all pig iron.
1.
We offer apologies here to our wise and more gentle colleague, Don Wells,
(http://www.extremeprogramming.org), who doesn’t like to call work done with the best
of will “bad”. Suffice it to say that we looked upon what we had wrought and did not see
that it was good.
100
Programming
During the crunch, we were honestly doing our best. Our best just
wasn’t very good, because we were tired. We can’t prove it yet2, but
from our own experience we are certain that heavy overtime is bad.
Thus the XP rule, 40-hour week: Do not work more than one consecu-
tive week of overtime.
Summary
Of course programming is what it’s all about. XP embeds program-
ming in a process that lets programmers stay healthy through the
course of a project, by helping the project to stay healthy. As we pro-
gram, we work in pairs, using simple design plus refactoring to keep
design quality high. We integrate all the time, so that we can all
improve the code wherever it needs improving. And we communicate
through the code by writing in a meaningful, standard way.
2.
We can’t prove it yet, but we’re working on it. Recent reserach has shown that the com-
mong hospital practice of working residents and interns results in more mistakes than let-
ting them work more normal hours. Remember that the next time you go to the hospital,
and the next time you decide to program while you’re tired.
101
Programming
102
Sidebar - Chapter 11
Code Quality
A little more detail on something close to our hearts:
simplicity.
Does this mean that every class must have tests? Not necessarily, but it
wouldn't hurt. If a class has no behavior that could possibly break,
then it doesn't need tests. If it could possibly break, test it.
What if another class uses ours, and the other class has tests? Does our
class need tests? More likely than not, it does. Does the test for the
other class test every feature of our class? Very likely it does not. Will
our class ever need enhancement or clarification or optimization? Very
likely it will. Changing our class and testing it by running other peo-
ple's tests isn't very well targeted.
103
Code Quality Express every idea
Most classes need tests for anything that could possibly break. Writing
those tests will find problems sooner and let the project go faster. Writ-
ing the tests first will make our classes easier to use and more compact.
The code we release must run all the tests. It's not sufficient that the
tests for our code run - all the tests in the entire system must still run
whenever you release. This ensures that changes we make don't impact
other people in surprising ways.
I have to turn all these strings into operands …turning this string into
an operand I have to see whether it starts with an operator … an oper-
ator is a plus or a minus … or it might not have one … if it has one, the
rest of the string is the variable name … if it doesn't, the whole string is
the variable name …
At the end of coding, the program should clearly express all of those
ideas. Yes, I know they're all there, embedded in our loops and nested
if statements. But they are hidden. Instead, let every idea be expressed
explicitly in the code.
Where there is duplicated code, there is usually an idea lurking: first I'll
convert this one, then I'll convert that one. That idea needs to be
expressed.
104
Minimize number of classes and methods Code Quality
Where there is duplicated code, there is more code to read before you
understand the program. Make the job easier.
• There is usually an idea lurking: first I'll convert this one, then I'll
convert that one. Express that idea.
• There is always a way to make the program smaller. Do it.
• There is always a chance for one of the duplicates to get fixed and
the other one forgotten. Remove this chance.
• There is more code to read and understand. Make your job easier.
105
Code Quality Minimize number of classes and methods
106
Chapter 12
Pair Programming
On an Extreme Programming team, two programmers sit-
ting together at the same machine write all production
code.
Pair programming has been around for ages. We’ve heard that P. J.
Plauger wrote about working in pairs back in the 70s, perhaps in one
of his "Programming on Purpose" columns, but haven’t been able to
find the reference. Nearly everyone has had the experience of working
together with another programmer on something really difficult, or on
a really serious emergency, and it’s usually a good experience.
A cat is not a dog, we are told, and pair programming isn't one pro-
grammer looking over another's shoulder as she bangs in code. Pair
1.
We’re not just making this up. Experiments in pair programming show its benefits. See,
for example, Laurie Williams and Robert Kessler’s article in the May 2000 Communica-
tions of the ACM.
107
Pair Programming
When you are typing in a method definition, you are spinning a lot of
dishes at once. You have an intention in mind for the method - it is
supposed to accomplish some task. You are trying to type legal state-
ments of the programming language, keeping the syntax rules in mind.
You are trying to type meaningful code, code that can be understood
now, and that will be understood months or years from now. You are
trying to conform to the team's coding standards. You are sending
messages to other objects, requiring you to remember what they are
and what messages they understand. All this is fitting into an overall
context of what you are trying to accomplish - and all of it has to be
spelled and punctuated correctly. No wonder we make so many mis-
takes. It's a wonder we can program at all.
Your experience may vary, but what I can keep well in mind is usually
the purpose of the method, the general algorithm I have in mind, and
most of the syntax rules of the language. If I need to think about any-
thing else, I have to kind of swap out one of those items and swap in
something else, like the protocol of some object I'm talking to. Every
time I make that mental swap, I have a chance of losing a little bit of
my picture. It slows me down and once in a while I even make a mis-
take. (On the average, I make at least one mistake requiring an edit (a
backspace or worse) in every method I write. I almost always need to
reformat or rename variables, though some of that can be done after
the method works.)
108
Pair Programming
variables we've just defined. Finally, my partner acts as the pair's con-
science, reminding us to keep the code communicative, to keep it
formatted to the team's standards, and to keep it tested.
The partner is responsible for being completely engaged. He's not just
along for the ride: he must understand everything that is being done.
If he doesn't, he needs to stop the process and get hooked up again.
The partner must be working the same strategy as the driver. He might
have another idea, he might even think it is better. Tough. When
you're the partner, your job is to help the driver do what the driver is
doing. Get with what's going on and support it.
We're trying to build a mind-meld here, and both players need to keep
it going.
It takes a little time to get used to pair programming, and at first it will
feel a bit awkward. Then you'll get good at pairing with someone, then
finally you'll get good at pairing with most anyone. Just keep at it.
Here are some tips:
The partner can say “Let me drive” when he can't express an idea ver-
bally, or when the driver is stuck. And it's a good idea to switch drivers
every so often anyway, just to keep from getting stale.
It's generally a good idea if the programmer who is least sure of what's
being done does most of the driving. It's easy to let yourself get sucked
109
Pair Programming
along when you don't really get it, and then you lose the advantage of
pairing.
As driver, notice all the times your partner helps. Acknowledge them
to yourself and to him. Sometimes it'll just be syntax or punctuation
help. Sometimes he'll remind you of the method name you were trying
to remember, or point to the window button you're looking for.
Sometimes he'll remind you to write a test that you need, or remind
you that the code isn't communicating as well as it could. Notice these
aids, and acknowledge them.
Speak in “I” terms when the going gets tough. Is the driver writing
something grotesquely ugly, the least understandable code you've ever
seen? Then just say “I don't understand that. Can you help me out?
Let's make it better.”
Partner, don't take the driver off his strategy unless you really need to.
If you do, try something like “I've got an idea. Let's try it that way for
five minutes, then I'd like to try something.”
Driver, don't run away with what you're doing. Chances are, your
partner does have a better idea, because while you've been down in the
mud, he's had his head up above the ground looking around. Listen.
We can't offer much more detail than that. Each pair is unique, but the
general moves are the same. Work at pairing. The result will be more
code, better code, better understanding of the system, and more fun.
Pair programming, if you can do it, will make you go faster in any situ-
ation. In Software Development (October 1999), Larry Constantine
put it this way:
110
Pair Programming
They spell each other, check each other's work, inspire each
other, fill in each other's weak spots, and crank out better code
with fewer defects. The same formula works for learning a lan-
guage or using a new piece of software - you not only learn
from the system or the material but from each other. A dynamic
duo who work well together can be worth three people work-
ing in isolation.
Pairing doesn't come naturally to everyone, but most folks get to like
it once they try it. Here are some techniques to try to get it going:
Send help by pairing. After folks get used to working with you,
encourage other pairs to get together: “Susan, could you sit with Jim
on that and help him with those formats? I'll join you if you need me.”
Drop in. Just plunk down at the desk of people who look confused.
“Whatcha doin'?”
Finally, remember that desk or table layout is critical for pair program-
ming. Computer in the inside corner doesn't work. Two chairs side by
side facing the monitor, that's the ticket.
Summary
Two programmers working together are more effective than two
working alone. Team knowledge grows faster and the work is more
fun.
111
Pair Programming
112
Chapter 13
Unit Tests
Extreme Programmers test everything that could possibly
break, using automated tests that must run perfectly all the
time.
XP teams share ownership of all classes. This lets them go fast, with no
waiting for someone else to get around to putting in a needed feature.
When code is being changed rapidly, you need to know you haven’t
broken anything. Therefore, XP teams build automated tests for every-
thing, and make sure they run perfectly all the time.
Let’s be clear about this: you need tests for every class – for everything
that could possibly break. And all those tests must run at 100% all the
time.
113
Unit Tests
runs correctly.” We hope this would give you some comfort. We know
it would give us comfort.
It turns out that if it’s your own ten thousand methods and fifty thou-
sand lines of code, the tests will give you the same comfort. So we
want tests for everything, and we want them to run perfectly all the
time. How do we do that?
The best way to write tests for a class is to write them first – test a little,
code a little – rather than writing them at the end, or even writing
them all at the beginning. All of these ways will result in what you
need, but going bit by bit is best. We’ll focus on that.
OK, you have decided you need a class to hold a collection of com-
pleted tax returns. It has to be able to give back a collection of all the
returns whose “score” is larger than a user-supplied number, and it has
to be able to remove all the returns whose score is less than some other
number. And it should be able to accept new forms being added. This
class, of course, will be driven by the tax auditor’s GUI that Bill is
working on, and it will use the return scoring capability that Martha is
building.
114
Unit Tests
We’ll even suppose that Martha isn’t done with the tax return objects,
and certainly we aren’t going to work with the GUI. Our mission is
just to write the Tax Return Scrutinizer.
We want to build up our tests, bit by bit. We’ll first test that we can
create a Scrutinizer with no elements, and that it behaves reasonably.
Our first test, therefore, just creates a Scrutinizer. Then we ask it some
questions: “Give me all the returns scoring more than 10". It should
answer an empty collection. We ask it to remove all the returns scoring
less than five. It should happily accept this command, removing none
of its already-absent returns.
testEmpty
create a new Scrutinizer
select returns with score > 10
result size should be zero
remove returns with score < 5
nothing should blow up
Finally the test can run. Probably it will run correctly. If it doesn’t
(maybe you forgot to initialize the Scrutinizer’s return collection), fix
it.
Now the test works, and we move on to another one. Let’s test adding
returns. But wait, there aren’t any, Martha isn’t finished! What shall we
do? I just asked Chet, and he said, “We’ll make a fake one.” That’s
right, we’re just here to implement and test the scrutinizer, and we can
test it without having any real returns to test. So create a stub class
called FakeTaxReturn, give it a constructor method, and write your
test to add them in.
115
Unit Tests
testAdding
create a scrutinizer
add some fake returns with various scores
see how many returns the scrutinizer has
The test for adding needs to find out how many returns are in the
Scrutinizer. We might have done that in the first test, but it wasn’t on
our mind, because we knew that a new Scrutinizer has no returns. Size
seems like a reasonable thing for it to be able to answer, so we’ll just
implement a public size method and move on. In rare cases you might
need to build a friend method or do something a little more exotic.
Generally, it can be this simple, and we recommend that you keep it
that way.
OK, now we want to test the selection. No problem, just extend the
FakeTaxReturn to have a score, and use it in a test. This would be a
good time to coordinate with Martha on the name of the method, so
she’ll use the same name in the real TaxReturn. Our test is easy: add a
few returns of various scores, ask for returns with scores bigger than X,
and see if we get the right number of returns.
testSelecting
create a scrutinizer
add some fake returns with various scores
select all the returns with score > 10
see if you get the right number
select all the returns with score > 5
see if you get the right number.
Here’s an important point: you are trying to test everything that could
possibly break in this object. If you can see from the code that if it
selects a return correctly, it’ll return it correctly, then there’s no need
116
Unit Tests
Now we’re in good shape to test the removal. We have enough mecha-
nism here to write the test, except for the removal method itself. Add a
few returns, select some, remove the ones less than some number,
check to see how many are left, check some selections to see if you get
the [new] right answer. Run the test, watch it fail until you fix the
removal mechanism. We won’t show the test pseudocode this time,
you can certainly visualize it by now.
Inch by inch, step by step, we test and build our way to a running class.
When we’ve got it doing everything we want, we’re ready to release
the tests and the code.
That’s right, tests and code. Now that we have written these tests, we
don’t throw them away, or file them for future use. We immediately
make them permanent parts of the system. We release our new test case
and our new classes to the version control system. That’s discussed in
more detail in Releasing Changes on page 145. But here’s a short
summary:
Everyone on the team only releases code when all the unit tests in the
entire system run at 100%. This is a critical point about XP, so we’ll
repeat it: in the released code, all the unit tests must run at 100% all
the time. When we put our Scrutinizer code away, with all tests at
100%, we’re sure that everything works in the whole system. When we
come back later and add some more new tests and code, then run the
whole suite and something breaks, we know that only our changes
could have broken it. If we’ve only worked for a little while between
releases, we’ll have no difficulty finding the problem.
The effect of all this is that the system kind of notches forward in cor-
rectness. Each time a pair of programmers releases a few more tests and
a bit more code, the system becomes more complete, and more correct
at the same time. Using this approach, you’ll rarely, if ever, get those
117
Unit Tests
The feeling of confidence that comes from building tests for every-
thing, together with the code, is one of the best parts of XP. Be sure to
experience it for yourself.
One more point on the Scrutinizer test, then we’ll move on to some
more general unit testing questions. We used a FakeTaxReturn to
make sure that the Scrutinizer worked, without having to wait for
Martha to get the real TaxReturn object working. That leaves a small
crack in the system, the possibility that Martha will name the method
that returns the score something other than score. A case could be
made that the Scrutinizer should be tested with real TaxReturns as
well.
We’d agree with that, but there are two key points to consider on the
other side. First of all, we don’t want to have programmers waiting on
each other. That just slows things down. You’ll naturally try to order
your tasks to help that happen, but things work fine if you just go
ahead. Second, with more complex collaborating objects than the
Scrutinizer, you can get strange interactions in your testing. So we
often find that using stub objects instead of the real thing will simplify
the testing and help you go faster.
But it’s important not to lose track of the connection between the
assumed method name and the real one. There are several good
approaches. Pick one that applies best in the actual situation.
118
Unit Tests
Having discussed all these options, we now think that the FakeTaxRe-
turn wasn’t the best possible idea. Probably creating the class or
editing it would have been better in this case, because it would require
less human coordination, instead relying on the source code and the
version control system to keep things in order. We decided to leave the
example this way, however, because it gave us a basis for the discussion
of the options. Sometimes you get lucky.
119
Unit Tests
3. Write a test. If your new behavior will change the state of the Scruti-
nizer instance, code an assertion to verify its current state. Then code
sending the message for your new behavior to the Scrutinizer instance.
If you don't know the message selector, now is the time to make one
up. Depending on your implementation language, you may need to
code a stub method. Now code an assertion to verify that your
expected change in state occurred.
4. Run the test. It will probably fail. If it does not, you are done, all
that is left to do is release. Go to step 6.
5. Modify the model code and repeat steps 4 and 5 until the test runs
successfully.
Testing questions
Now here are some questions that get asked every time we talk about
this subject, so we’re guessing that you’ll have some or all of them. If
we miss yours, drop us an email.
The basic trick is to write most of the tests that use data from the data-
base to use data from files, or from memory, instead. Just capture the
database result, as records or objects, and write it to a file in the test
directories. Then write the tests to use the file data instead of going to
the database. Often this is best done by having a dummy database
120
Unit Tests
object, perhaps one that looks at the SQL statement it’s sent and just
opens the corresponding data file. Other times you can test using the
data without needing to fake out the database.
But you do need to test the database access as well. Having a very few
tests that actually open the database and get records from it, making
sure that the connection works, that the records come back right, and
so on, is a really good idea. Just try to make that test as tight as
possible.
You might feel the need to test your record-mapping methods, the
ones that take the stuff that comes back from the database and turns it
into objects. These tests can be written against hard-coded data as well,
but it might be a good idea to have one that runs against the real data-
base, just in case a version changes or something, and they start
sending you data in a different format.
Tests that do too much should be fixed as well. Are you running an
entire paycheck to see whether the tax deduction is correctly taken?
Take a little more time in setup and just check the one object that’s
involved.
121
Unit Tests
Is the class hard to create? This is often due to not having, or not fol-
lowing, a coding standard that calls for a complete constructor
method. Taking the time to make the object easier to build will proba-
bly help.
Does the class being tested collaborate with a lot of other complex
objects that are hard to get? This is a hint that you need some stub
objects to work with, and also suggests that the class might benefit
from a little refactoring to make it less dependent on so many others.
Get with the team, talk about what’s hard to test. You’ll think of some-
thing most every time. And better tests make you go faster – you’ll be
glad you did.
Second, when the other tests fail due to an error in the inner guy,
you’ll have a bit more of a debugging session to fix it. Think of it this
way: if you have a test that says the square root of 4 should be 2, and
the test fails, you know it’s the square root that broke. If you have a
numeric routine that relies, among many other things, on getting cor-
122
Unit Tests
rect square roots, you could debug for a long time before noticing that
the root is wrong. Local tests give better diagnostics, so you go faster.
When the software gets an error in the users’ hands, you have a sign
that you need both acceptance and unit test enhancements. Learn
from this yourselves, in terms of what kinds of tests are needed. And
help the software learn as well, by writing the new ones you need right
now.
At first, it seems like the tests slow you down. But the first time you
release something that doesn’t work, and feel like a fool, remember
this section. Build a scaffolding of tests before you work. It’ll help keep
the walls from coming down while you rebuild them.
123
Unit Tests
between classes?
Our Scrutinizer problem above was an example of how a collaboration
error could show up, and discussed how to avoid it. If your system is
showing lots of such errors, there’s some kind of coordination problem
happening. People are writing different responses to the same ques-
tions, or they are naming classes and methods inconsistently, or there
are consistency checks needed in the unit tests, or something. When
this kind of error shows up, it’s a sign that some additional work needs
to be done. All we can say is to figure out what that work is, and do it.
Since these defects are hard to find, when they start showing up, it’s
time to invest in cornering them.
Our own experience is that we get very few of these errors. We’re
guessing here, but we think that somehow our focus on testing up
front is preventing them, but we can’t prove it. And we certainly agree
that when they do show up, such problems are difficult to find. So
when they show up, we go after them vigorously, trying to build a
tighter net of tests around those areas. More than that, we don’t know
how to answer.
124
Unit Tests
125
Unit Tests
126
Sidebar - Chapter 13
xUnit
Use the world’s lightest testing tool.
Starting with unit tests is a bit difficult – you just don’t know what to
do. Here’s some free software to give you a leg up.
Kent Beck wrote a testing framework, called SUnit, for Smalltalk some
years ago. It caught on all over, especially in XP teams using Smalltalk.
He and Erich Gamma then provided the jUnit tool for Java. Today,
there are CppUnit, PerlUnit, PyUnit for Python, VBUnit for Visual
Basic, and many other languages as well. These frameworks are avail-
able on Ron Jeffries’ website, www.XProgramming.com. Click the
“software” link.
Within the limitations of the language being tested, all the frameworks
work the same way. You build a subclass of the class “TestCase” to
contain your tests for some object that needs testing. (And they all do.)
The test case includes as many testing methods as you want. Typically,
each one begins with “test”. They might be named “testCreation”,
“testSelection”, “testRemoval”, and such. The versions of the tool in
languages with reflection build up test suites automatically, by collect-
ing all the methods named “test...”. In less advanced languages, you
have to set up the suites manually.
The testing framework collects up all the tests and executes them one
after another. Each one is initialized by calling a standard setup
method that you can override, and torn down by calling teardown.
This way you can ensure that each test runs in a clean environment, so
that mistakes in one test don’t impact another.
127
xUnit
Most versions of the framework come with a little GUI that you can
use to control which tests to run, and that shows progress and your
final score. Naturally, before your code release, you run all the tests in
the system, and your final score is perfect. Click, one more notch for-
ward on the progress dial.
It’s tempting to use more complex testing tools. There are even some
out there that claim to do intelligent “black box” testing of your
classes. Read about these with some skepticism – how could they possi-
bly know the right and wrong answers to feed to your class. They
might build some skeleton methods, but that’s not the important part.
The important part is your application of your intelligence to testing
what really needs it.
Try these xUnit tools. They’re free, and worth ten times that much. A
hundred, maybe.
128
Chapter 14
Test-first, by Intention
Code what you want, not how to do it. Chet and Ron do a
small task test first, trying always to express intention in
the code rather than algorithm.
Chet and I wanted to give a short demonstration to tie together all the
XP programming practices. We decided to work on an actual problem
that I’d encountered at a client location. This is a transcript of an
actual session, not a sanitized session made up for the book. The
ragged edges are real life.
There are two key things to watch for in this example. First, we only
write new code when we have a test that doesn’t work. We call this
test-first programming.
The task is this: we have two collections of Sum objects. A Sum has a
name (a string) and an amount (a number). The output is to be a sin-
gle collection of new Sums. If a Sum of the same name appears in each
collection, the new one should have the same name, and the total of
129
Test-first, by Intention
their amounts. If a Sum appears in only one collection, the new one
should have that amount. The order of the output should be the order
of the first collection, followed by any elements from the second that
didn’t occur in the first. For example,
A1 A 10 A 11
C2 B3 C2
B3
Chet asked a few questions, mostly about whether we had any code to
start with. I said that it had been ugly and didn’t work, so we decided
to start from scratch.
---Sum 10:25:00
The lines that look like the above just name the class we’re putting
code into, and the time we did it. They aren’t part of Smalltalk, they’re
just here to give you a sense of how long things took. Here’s the class
definition:
---Sum 10:25:00
Object subclass: #Sum
instance variables: ’name amount’
This defines a new class named Sum, with instance variables name and
amount. In Smalltalk, you don’t have to define the types of the
variables.
Now we build the Constructor Method for the class. This is a method
definition. Each method definition starts with the name of the
method, tabbed out one tab stop. A method definition in Smalltalk has
a name which consists of one or more keywords. This one is
130
Test-first, by Intention
The method itself starts on the next line, indented. So we’re defining
name:amount:. The definition is
^self new
setName: aString
amount: aNumber
which means (self new) create a new instance of this class (Sum), then
send it the message
We start every class definition this same way, with a Constructor and
Constructor Parameter method. It gets you going smoothly when you
always start a class the same way. Here’s the whole Constructor
method:
---Sum 10:25:15
setName: aString amount: aNumber
name := aString.
amount := aNumber
131
Test-first, by Intention
---Sum 10:25:30
name
^name
We also built an accessor for amount while we were there. Bad Ron.
Rolled-up newspaper for you.
---Sum 10:25:40
amount
^amount
OK, enough warm-up. It’s time to write our object, which we have
decided to call Summarizer. We began by making a test class named
SummarizerTest. Here’s the conversation we had as we pair pro-
grammed our way along.
132
Test-first, by Intention
For you non-Smalltalkers, the vertical bars set off definition of a tem-
porary variable named summarizer. The variable is local to the method.
Now Chet pauses before writing the first should: to discuss what to do.
133
Test-first, by Intention
defined. We expected that, but we like to run the test every chance we
get – it’s a good habit to have.
The name with:with: has good history in Smalltalk when you are creat-
ing some object with a couple of items that aren’t particularly
differentiated except for order. On the other hand, the name isn’t very
communicative. Ron may have been wrong with this suggestion.
Chet writes the method that way, putting two empty arrays into the
Summarizer.
134
Test-first, by Intention
We run the test again. This time it breaks because the Summarizer
doesn’t understand “summary”. Chet hasn’t an idea how to do the
method so he just creates one with a halt in it, then runs the test again
to get into the debugger.
135
Test-first, by Intention
just answer a literal empty collection, but Chet was just getting his
hands on the variables a bit.
Our first test runs. Even though the object is clearly wrong, we don’t
have a broken test to make us fix it. So we write another test.
Ron: Let’s test the example I wrote down. (The one in the table
up above.)
Chet: OK.
Here again, we’re talking about intention. We just say what we want,
not how to do it. When we get there, it’s always easy.
136
Test-first, by Intention
Having built the Summarizer object, Chet is ready to enhance the test.
I wasn’t ready, but he was, so I rode along as he enhanced the test to
check all the values.
Chet puts off the inevitable thinking a bit longer by declaring his
intention in the method, namely to have a collection with a and c, and
one with a and b. Again he assumes that a magic elf has already created
them. Programming his intention, not his algorithm.
137
Test-first, by Intention
Now it’s pretty clear what we need, since we have a name for it. So we
type in the methods:
Great, the test is written. We run it. Oops. The original summary
method that concatenates the inputs gives back four elements, not
three, and the test breaks. We aren’t surprised.
138
Test-first, by Intention
Ron: Good, I like that. So we’ll just process first, then process
second.
--- Summarizer 10:50:29
summary
self
processFirst;
processSecond
Chet: Where shall we put the answer?
Ron: Just make a new instance variable, summarizer.
Chet: OK, shall I just init it in the Constructor Parameter
method?
Ron: OK.
Chet updates the class definition to add the instance variable, and the
method to define it as an empty OrderedCollection.
Chet writes a loop over the collection, pausing for a moment to think
how to process the items. Then he remembers to just declare his inten-
tion (processItem:) and go on.
139
Test-first, by Intention
It’s getting close now. We run the test and notice we don’t have pro-
cessItem: defined yet. Chet makes a blank method, then we talk.
Chet codes just what we said. Our intention is find matching Sum, add
our input Sum into it. The code says just that. Now we run the test
and, of course, matchingSum isn’t defined.
140
Test-first, by Intention
have broken it up, but we discussed it and couldn’t find a way we liked
better.
We run the test. It doesn’t run because Sum doesn’t understand how
to add. We quickly build that:
We add the final line to summary, then run the test. It runs. We cele-
brate briefly, wishing we had a bell to ring. Then we begin to review
the code now that it works, to see if we should clean it up.
141
Test-first, by Intention
Chet: OK.
--- Summarizer 11:04:45
summary
self summarize: first.
self summarize: second.
^summary
Chet runs the test and it works. But he doesn’t like the new method
the way it is.
The test runs again. At this point we look at what we have done and
find it good. We stop, and go to lunch at Red Hot and Blue. We each
have a Pulled Pig sandwich.
Review Remarks
142
Test-first, by Intention
The bottom line is always judgment. If the code’s not clear enough, it
needs more work and probably more tests. Jim was right to wonder.
Summary
Experiment with writing little tests first, and with always expressing
your intention, not algorithm, in the code you write. We think you’ll
find that the work goes more smoothly, and you wind up with code
that communicates what you mean to do, not just how you did it.
143
Test-first, by Intention
144
Chapter 15
Releasing Changes
Using collective code ownership and comprehensive unit
tests, an XP team releases changes rapidly and reliably.
1. Local. This is the first phase of development. The pair has just start-
ed working, and their changes are not available to any of the other
developers.
2. Release Candidate. The programmers have finished their task, and
are ready to begin the process of releasing their changes to the oth-
145
Releasing Changes
er programmers.
3. Released. This is the current official version of the code. This code
is guaranteed to work, i.e. all of the Unit Tests run at 100%. Re-
leased changes are available to all of the other programmers.
1. Always start with all of the released code. This ensures that you are
starting with the latest and best versions of everything.
2. Write tests that correspond to your task. (Write the test first)
3. Run all the Unit Tests.
4. Fix any Unit Tests that are broken. Since you started with the re-
leased code, the only tests that should break are the ones you just
wrote. It is your responsibility to fix them.
5. When all of the Unit Tests run at 100%, your local changes become
release candidates.
6. Release Candidate changes are integrated with the currently-re-
leased code. We recommend the use of a separate integration ma-
chine for this, and we’ll describe it that way. Go to the integration
machine. Load all the released code. Load all of your Release Can-
didate changes. Then check for conflicting edits. The currently re-
leased code should be the same code you started from. If it isn’t,
another pair has released before you. This doesn’t happen often,
but let’s assume that it has.
7. If the released code was modified while you were making your
changes, compare the differences between your changes and the re-
146
Releasing Changes
leased code. Use a diff tool if possible, not just your eyes. Identify
the changes released by the other pair(s), and integrate those
changes with your changes. Ask the other pair for help if you need
it.
8. Once you have integrated any necessary changes, run the Unit
Tests on the integration machine. They should run to 100%, and
they usually will. If they don’t, it usually means you missed integrat-
ing something, or that your changes and someone else’s conflict.
The Unit Tests that fail will pinpoint the problem. Fix all the prob-
lems. Again, get help from the other pairs if you need it.
9. When the Unit Tests run at 100%, release all of your code - that is,
make the code on the integration machine the official version.
The release process generally goes more quickly than it took to write
about it. A few minutes to load your Release Candidates on the inte-
gration machine, a quick check for conflicting edits, ten minutes to run
the Unit Tests, and then the code can be marked as released.
The code mangement tool used on your project can make a huge dif-
ference in your productivity. Ideally, whatever tool you use, it should
make it easy to do the following tasks.
In other environments, or if you just can't swing the price, a tool like
Visual SourceSafe, PVCS, MKS, or TLIB will do the job. There are
147
Releasing Changes Troubleshooting
lots of code managment tools out there - read this chapter, then check
them out and pick one.
Troubleshooting
The two most common problems with releasing changes are lots of
conflicting edits, i.e. taking too much time to merge changes, and lost
changes.
148
Troubleshooting Releasing Changes
In rare cases, you may need a more formal check out / check in pro-
cess for a few classes. Avoid this if you can, but be alert for the need to
do it if too much time is spent merging changes.
Lost Changes
Losing changes are a time wasting nuisance. The main cause of lost
changes is incorrectly merging release candidate changes with released
code. This is usually either because a conflicting edit was not recog-
nized, or the programmer incorrectly merged the two sets of changes.
The solution to this problem has three parts. The first two parts of the
solution are: always start with all of the released code, and always
release changes frequently. Frequent releases minimize the chance that
the released code has changed. When changes do have to be merged,
frequent releases minimize the impact. Remember Continuous Integra-
tion, page 96.
The third part of the solution is your Unit Tests (page 113). Code
can't stay lost for long if there's a test somewhere relying on it. Be sure
you add tests whenever you add code to any class. Since all the Unit
Tests must all run at 100% before any code is released, this ensures that
other programmers can’t break your code by accident.
149
Releasing Changes Troubleshooting
candidates, and when the Unit Tests all run at 100%, the code can be
released.
Conclusion
A good Extreme pair saves code very frequently, multiple times per
day. If the code manager is making you want to release less often, fix
the code manager. Rapid progress depends on rapid and frequent
release.
150
Chapter 16
Do or Do Not
We’ve now covered most of the programming aspects of XP.
Here’s a summary of things we do — and things we don’t.
But XP is also about not doing. Over the years, software development
“methodology” has become encrusted with practices that, for most
projects, do not advance the actual effort of producing the product
that is wanted: the software.
Remember that: if you're doing XP right, at the end of the project you
will have a computer program that is
• well designed;
• well crafted;
• flexibly structured;
• properly documented;
• tested;
• meeting known and documented requirements .
151
Do or Do Not
Some of the things that we do not recommend doing are a bit contro-
versial. As you review the simple XP processes, we're sure you'll see
that the other things aren't always necessary after all.
• Don't try to design the whole system before you start implement-
ing. Usually, requirements changes alone will make this impossible.
In any case, no existing design methodology is effective enough to
avoid problems during implementation, and a process of design a
little, build a little will allow you to learn faster and get a quality sys-
tem done sooner.
• Do design all the time. Begin simply, and as you learn what the de-
sign should be, refactor to make it so. Never stop designing, never
stop making the code agree with what the design should be.
• Don't try to freeze requirements before you start implementing.
Requirements changes show that the customer is learning! Sure, it
would be nice if they knew just what they wanted before you started
building things, but the fact is that when they see what you're
building, they'll learn what they meant. XP lets you use a develop-
ment and planning approach that allows for change, without big
up-front investment in frameworks or flexibility.
• Do develop comfort with taking on any story in any order. Sure,
sometimes order makes a difference, but more often than we pro-
grammers think, it doesn’t. Just tell the customers the cost of every
story, and let them choose.
• Don't produce voluminous design documents at the beginning.
Don't even produce them in the middle: produce them at the end.
Extreme Programming teaches you how to keep the design flexible,
for highest flexibility and fastest implementation. The design doc-
uments you produce at the beginning will go out of date very
quickly (they always do, even on non-Extreme projects), and you'll
either waste time updating the docs or let them get out of date. Ei-
ther is bad.
• Do focus on communication. A few diagrams can help. A big pic-
ture on the wall can help. More important, however, are clear code
and talking, talking, talking.
• Don't produce documents or other artifacts that aren't being used.
152
Do or Do Not
153
Do or Do Not
154
Chapter 17 ‘
Experience improves
estimates
Each iteration we gain experience. Experience with stories
helps us estimate future stories more easily and more accu-
rately.
You’ve just released your changes for some task. When you finish all
the tasks in a story, it’s time to take a moment. For example, when
Chet and I wrote the Summarizer program in Test-first, by Intention
(page 129), it took us about an hour. From now on, we’ll have a good
idea of how long it takes to do tasks that are about that hard. And of
course it’s not difficult to estimate whether a given task is about the
same size as the Summarizer, or half as big or twice as big.
This means that when we have a task like that one, we can estimate
about how long it will take and be pretty reliable. And the more we
estimate, and the more we pay attention to how things really turn out,
the better we’ll do.
You can read more about task creation in Team brainstorms Engineer-
ing Tasks (page 81), and in How to estimate anything (page 217), but
155
Experience improves estimates
the simple answer is that when the team has a big thing to do, they
brainstorm how to break it down, until it is broken into small enough
tasks that experience allows them to estimate.
1. Estimate each task that you sign up for. Don’t try to get down to
minutes – it’s probably best in a project not to go below half a day.
For the Summarizer, I estimated two hours, but it only took one.
My partner was great.
2. Estimate the amount of actual time you will spend working, with a
partner, at the machine, on the task. Don’t worry about time you’ll
spend discussing it, drawing pictures, or thinking. These things will
all average out when you measure your velocity. The important
thing is the size of the solution, in time spent implementing.
3. Pay attention to the actual time you spend working, at the machine,
on the task. You can write it on a card if you want to, but since
you’ll probably want to focus on just one task at a time, memory
will probably suffice.
4. Take a moment. You estimated a day. It took a day and a half. Or
you estimated a day and it only took a half. What was it that you
missed in the estimate that would help you estimate better next
time?
It should be pretty clear that if you do this a lot, you’ll get really good
at estimating how long things will take. Feeding this knowledge back
into each Iteration Plan makes things go more and more smoothly and
easily.
156
Chapter 18
XP prescribes tracking and reporting approaches that are easy and pub-
lic. This ensures that everyone's always on the same page. Briefly, it
goes like this:
Resources
Keep track of key resources: planned vs actual: number of developers;
number of customers assigned to the project; number of testers; num-
ber of computers in development, test, production; and so on.
Scope
157
Resources, Scope, Quality, Time
Keep track of the number of stories over time: how many exist; how
many are done; how many more are expected. Consider tracking total
estimated time for the project and estimated vs actual scope
consumption.
Quality
Use a standard acceptance testing graph showing number of tests and
number succeeding over time.
Time
Track the results of each release plan. Graph schedule vs time. Discuss
dropped or added functionality and its impact on time.
Here are a few graphs. Pick the simplest one that could possibly work
in your situation. Don't move up the scale of complexity unless experi-
ence says you have to.
158
Resources, Scope, Quality, Time
Each iteration, graph how many story cards exist and how many are
done, on a bar chart. Color completed cards green and those still to be
done white. (Not red - red means bad.)
Completed Stories
160
140
120
100
# Stories
80
60
40
20
0
1 2 3 4 5 6
Iteration
Completed Stories
Take a look at this graph. What is it telling us? The growing portion of
green shows that we're making good progress. But we seem to be get-
ting more stories as well. What's up with that?
It's your project, so you'd better know. Here are some possibilities:
• Stories are being split for better planning. (release plan). In this case
the work isn't really increasing, and everything is probably OK.
Consider adjusting the historical columns to show the new story
count also.
• Stories are being added because progress is good. This can actually
happen, and if you can make the original date, might be OK. Con-
159
Resources, Scope, Quality, Time
I may seem fanatical about making the date. If I am, it's because many
years of development tell me that the date is what management
remembers. XP lets you predict your progress accurately, and manage
scope to make that date. For my money, that's the most impressive
thing your team can do.
Now unless you dozed off, you probably want to object that just
counting the cards loses the fact that some stories are big and some are
small. That's true, yet it probably doesn't matter. Since you're doing
worst things first, if anything you should speed up toward the end, as
you get into all that easy stuff.
But if it really matters to you, use the same graph but show estimated
weeks of work (Gummi Bears, points, XPUs) instead of card count.
Each time you do a new release plan, use the new numbers in all the
subsequent graph columns. There's probably no need to do anything
to the historical columns.
Frankly, the effort graph is probably overkill. The charts aren't that dif-
ferent, and the second one is much more hassle to create. If you just
draw the standard scope graph after each iteration, you'll know and
express more about your progress than most projects ever do.
Go for it!
160
Resources, Scope, Quality, Time
OK, the first and most important quality graph is unit test scores over
time. Here is it. Copy it out of the book and post it on the wall.
100%
80%
60%
Score
40%
20%
0%
1 2 3 4 5 6 7 8 9 10
Iteration
That's right. You must have unit tests for everything that could possi-
bly break, and they must always be at 100%, for every release of code
by every programmer. That's one graph done.
What should the graph of number of acceptance tests look like? As you
get close to release, it should flatten out, as you think of fewer and
161
Resources, Scope, Quality, Time
fewer things that still need testing. It will probably start slowly and
ramp up, giving a classic s-curve.
250
200
150
100
50
0
1 2 3 4 5 6 7 8 9 10
Month
Correct Tests
The success curve will generally be lower than the number of tests. Yes,
if your code is perfect and never breaks, they'll be equal. If you can
really do that, go for it, and write and tell us how. For the rest of us,
we'll color under the graph, green for success, red for failure. Put the
green on the bottom, it shows progress better.
162
Resources, Scope, Quality, Time
Graph the scores at the end of every iteration, for a graph like the one
above. Within the team, you’ll want to graph the scores more fre-
quently, preferably every day, for something like this:
250
200
150
# Tests
100
50
0
1 2 3 4 5 6 7 8 9 10
Month
Correct Tests
For your monthly report, these graphs will probably suffice to show
progress toward quality at completion. Internally, however, you may
want more.
Usually your acceptance tests will break out into some natural organi-
zation. Each of these breakouts will probably have several, perhaps
163
Resources, Scope, Quality, Time
many individual tests in it. If you want to see how errors are clumping
by product area, produce a graph showing success/failure by area.
100%
90%
80%
70%
60%
50%
40%
30%
20%
10%
0%
input rates payments insurance
Correct Tests
164
Resources, Scope, Quality, Time
The point of this graph is to identify, daily, things that are improving
or breaking. Compare this chart with the previous one:
100%
90%
80%
70%
60%
50%
40%
30%
20%
10%
0%
input rates payments insurance
Correct Tests
165
Resources, Scope, Quality, Time
4
2
0
-2
-4
-6
-8
1 2 3 4 5 6 7 8 9 10 11
Date
Take a look at that big dip. A bunch of tests broke that day. The next
day a few came back, and over the next couple of days, it looks like
they all came back. You can tell a lot about what's going on in the sys-
tem from a simple chart like this.
166
Resources, Scope, Quality, Time
10
9
8
T 7
e 6
s 5
t 4
3
2
1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Date
When it's time for your status report, even if it's to the president, take
the test charts off the wall and show him. If you just have to make
slides, I suppose it's OK to print them from the spreadsheet. But be
certain you are reporting the exact same data you look at every day.
Good or bad, that's the Extreme way: tell the truth straight out.
167
Resources, Scope, Quality, Time
per class, all kept historically of course. Source lines, story completion
rate, tasks per story. Pair programming time vs reported defect rate.
There are lots of great metrics. Color charts, rates of change. Fantastic,
and with a few scripts, a little Perl, some Excel, you could know so
much.
The success of your project is in your people. Watch these few vari-
ables, and if they show there's trouble, talk to the people. Watch the
people. Are people relaxed, friendly, playing around, throwing Nerf
balls at each other? Good. Are they heads down, avoiding eye contact,
snapping at each other? Not good.
Watch the people. You can spot trouble coming, and avert it, long
before it will ever show up in your metrics.
First, keep a daily journal. The real metrics are the things that happen
in the real world. Write in your journal every day at a fixed time,
recording the events of the previous 24 hours. Evening is probably
best for memory, if it works for you.
168
Resources, Scope, Quality, Time
Here are some things I've been asked, or asked myself, that I some-
times wish I had written down.
But here’s the telling thing: while a number of these would be interest-
ing in papers about the projects, or in conversations at conferences,
not one of them would have helped the project succeed even a little bit
better. Sure, we can see how they might have helped. But the chances
of their helping are awfully small. They’re not worth taking time away
from actually helping.
Your best friend is the facts, even if they aren't in your favor. But if you
track them, the facts probably will be in your favor! At the end of the
project, you may want to have a retrospective, to help the organization
learn. When you do, it can be difficult to reconstruct certain bits of
history.
• · What was the project velocity in terms of stories per time? Alter-
natively, what was the load factor, and how has it changed?
• · What was the schedule and number of completed points you pre-
dicted at each release plan?
• · How did the acceptance test scores change over time?
• · How rapidly did the code turn over? Did you get more or less re-
use within the system than you expected?
169
Resources, Scope, Quality, Time
In the end, you must use your own judgment on what to record. But
please, don’t be a manager who fiddles with his spreadsheet while the
project burns. Success is in your people — work with them.
170
Chapter 19
Steering
The estimates are wrong. Your priorities will change. You
must steer.
The results of this are clear. I’m sure your own history will agree when
you look back on it: on most projects, the estimates are often wrong,
far too high or far too low, and the priorities will change before you
even begin, let alone before you get to the end.
XP’s practices help with the estimates. Many of the estimates you’ve
heard about, where projects took far longer than they were “sup-
posed” to, were based on demands, not on measured performance and
estimates of difficulty. So the estimates of an XP team tend to be quite
good. However, there will still be variability. You will inevitably find,
over the course of the project, that a large number of stories get easier
and easier to do. Most teams find that there are stories where the first
couple take a few days each, and the remaining similar ones can be
done at ten per day.
171
Steering
Most teams also find that there are some stories that take much longer
than originally estimated. It’s easy to be upset by this, or to look for
someone to blame. (Look no further: It’s Chet’s Fault, page 223.)
Software is difficult. Specifying software is difficult, and designing it is
difficult. There’s no need to make it more difficult, so let’s not.
Instead, let’s just assess our situation and steer the project on to
success.
When things take longer than they should, take that result into
account in your planning. Fact is, “we’ll make it up later” rarely hap-
pens, and it’s not the way to bet. If you are the fortunately project that
hits the Making It Up Later lottery, don’t worry, you’ll have plenty of
ideas about what to do with the extra time. Consider selling it to all
the projects that don’t hit the lottery. Or use it to put additional value
into your project.
But plan to proceed at the pace you’re measuring. If certain stories are
going slowly, look for similar ones and assume that they will go slowly
as well. Get the team to reestimate those stories, or, for that matter, all
stories. Use that information to decide which stories to do first, and
which to defer. If it looks like some important story that you were put-
ting off won’t get done, move it forward, at the expense of things that
don’t look so important now.
Steering your project will give you great pleasure and great success. As
you learn what you need, an XP team can adapt to those needs. As you
learn how long things take, you can rearrange stories and split stories
to build an excellent product. Frankly, we believe that the process of
selecting and steering will deliver a better product than getting every-
thing you originally imagine you’ll want. And the most important
thing is, steering to success is actually possible.
172
Chapter 20
A better way would be to get nine of the stories completely done and
skip one — the least valuable one. Nine stories 100 percent done plus
one story zero percent done = nine stories done. Much better than
zero.
Improve estimates
Story delivery is important, but it’s not the only thing. Since we base
all our planning on estimated difficulty, we need to get feedback on
difficulty. When we planned the iteration, we broke each story into
173
Steering the Iteration
tasks, and the programmer who signed up estimated each task. During
the iteration, we need to reflect on these estimates.
Tracking
If the main point of the iteration is to get stories done, then the main
purpose of steering the iteration is to make that happen. All during the
iteration, we want to steer so as to make sure that as many stories as
possible get completed. To accomplish that, we need to track the tasks.
Sometimes we call the person with this responsibility the “Tracker”.
The team needs to check, every few days, how all the tasks are going.
You can do this by having Tracker go around to all the programers
every few days, or by covering task progress at the morning stand-up.
Going around is better if you can find someone who can do it without
driving everyone mad, do you hear me, mad.
Sometimes a programmer will go off track. She may make the wrong
choice at some decision point, or make a poor choice of where to
begin. Often programmers feel that they are “just about” to solve the
problem - they can be “just about” to crack it for days at a time. Track-
ing is an independent check of how things are going. The tracking
function identifies tasks that are going beyond their estimates and gives
the team a chance to deal with them. More tasks get completed this
way, and the team is more successful and confident.
174
Steering the Iteration
All you need to do tracking is the contents of the Iteration Plan: the
stories chosen, the tasks to be done, who signed up, and what their
estimate was. Oh, and you need the programmers, so you can talk to
them.
It's tempting to “just have the programmers email in their results every
couple of days”. Forget it. XP is about people, not computers. The eye
contact is important. The team-building human contact is important.
We’ll pretend you’re using cards. If you’re not, make suitable adjust-
ments to what we say here. Write each programmer's name at the top
of a card. On the card, write each task she signed up for, plus her initial
estimate. Leave a little space between tasks. But don't worry, you can
make a new card if you mess this one up.
“Hi Dave, I'm here to track. You signed up for task XYZ. How much
time have you worked on it so far? “
“You initially estimated two days. So it's going about as you thought?”
“Dave, you also signed up for ABC. How much time on that so far?”
175
Steering the Iteration
“Well, I've got two days in on that, and I think maybe about a day to
go.”
Your ears prick up. (If you can actually do this, you are a born tracker.)
“You originally estimated a day and a half. What happened?”
From Dave’s vagueness, you get that the ABC task is in trouble. You
may ask more questions, or might suggest a CRC session, or might ask
the coach to drop in on Dave and his partner. It's not Dave's fault:
these things happen. The thing to do is to make sure that it gets sorted
out.
There are several actions to initiate when you discover that the itera-
tion is off track.
First of all, bring the information to the team level. A story not getting
done is not an individual’s problem, it is the team’s problem. The
sooner you get to solving it, the better you’ll perform. See It’s Chet’s
Fault on page 223, for one way of dealing with the blame aspect.
Coach Dave to bring it up himself, and offer to handle it for him.
Don’t let it slide.
Second, solve the story problem within the programming team, if pos-
sible. Often other people are ahead of schedule or can buckle down a
little bit to pick up the slack to complete all stories. If this can be done,
it should be done. The programmers should have it be a point of
honor that the team makes commitments whenever possible.
Third, give some special attention to the particular task that’s off track.
Call a Quick Design Session (page 87) in almost every case, to make
sure the best minds are looking at what has turned out to be a problem
task. Consider giving the task owner a new partner, or even switching
owners. Sometimes you’re just down a rathole on a task and a new pair
will do better. Yes, we know that will feel like a defeat, but move to
another task and play that one to win. The iteration is the important
thing.
176
Steering the Iteration
Fourth, if the team just can’t pick up the slack, get the customer
involved. Level with them on what’s happening, and ask them to make
the call on what to do. Sometimes they’ll have you drop a story — and
often it’s a different one from what you expect. Sometimes they’ll sim-
plify a story, so that all stories can be completed, but one is a bit
smaller than first planned. In any case, it’s the customer’s call. Strange
as it may seem, you’ll engender more confidence by getting them
involved as soon as there’s trouble. And confidence is what will get you
a successful release.
Benefits of Tracking
We do recommend that teams have a Tracker. However, every team
we’ve worked with has had trouble getting someone to fit the position.
The tracker needs to have a non-threatening approach to getting infor-
mation, needs to be sensitive to body language and other non-verbal
behavior, and needs to be willing and able to track on a regular basis.
We’ve tried it with peers, customers, managers, and people who hap-
pened by on the street. Our best experience was with a manager who
was really good at being non-judgmental, but whose slightly sad
expression when people fell short encouraged people to do their best
for him. Our worst experience was when we tried switching to a differ-
ent tracker each iteration, making it a rotating duty among the
programmers. The individual variability in personality and style just
didn’t help.
177
Steering the Iteration
It helps programmers focus for a few moments every now and then on
their estimates, improving your ability to know what’s going to hap-
pen. And it puts a little attention on the task brainstorming process, by
finding those cases where the task list wasn’t complete, and helping the
team learn from the mistake.
You can track with a Tracker, if you can find one. You can track during
your daily stand-up meetings. There are probably other ways, as well.
One way or another, track the iteration as it goes along. Steering is
best if you do it all the time.
178
Chapter 21
Often you have a specified date by which you are supposed to release
your product. By controlling scope, you can steer your project to
release the best possible product by that day.
179
Steering the Release
Look, one-fourth done! Could it be that easy? To a first, but very good
approximation, the answer is yes. Here’s why:
The number of points per iteration will probably be pretty stable from
one to the next. It’ll go down if a lot of people are on vacation or get
distracted by a course or a planning meeting or lots of support require-
ments. It’ll go up, too, especially as the team learns how to do things
or builds new tools. Factoring out lost time, the number of points built
per iteration will probably stay flat or climb slowly.
So what about your release? Just do the math. Want to know what will
be done by six weeks from now, and you’re doing nine points per
two-week iteration? There are three iterations, for 27 points. Pick the
most valuable cards adding up to 27 points — that’s your best estimate
of what will be done.
Note why we say always to put the most valuable stories into each iter-
ation. By choosing the most valuable stories, we make sure that the
system has the highest value on any given day, including that most
important day, release day!
180
Steering the Release
Summary
Steering the release is the most critical aspect of XP. No matter how
fast or slow the team is, no matter what happens as you go along, your
best shot at a successful release by your scheduled date is to steer. For-
tunately, steering is easy. Know what’s done, know how fast your team
is moving, and use that knowledge to decide what to do next, and
what to defer.
1.
PERT and project software often do more harm than good. They give you the illusion of
truth when all you have is estimates. They give you the illusion of control when all you
have is data. They focus your attention on your computer screen instead of your people.
For an XP-sized project: Resources, Scope, Quality, Time (page 157) — that’s all you
need. Do we repeat ourselves? Then we repeat ourselves.
181
Steering the Release
182
Chapter 22
Handling Defects
Report ’em, schedule ’em, test and fix ’em, avoid ’em. Just
don’t call ’em bugs.
Defects reduce the value of the software to the customer. That runs
counter to our primary purpose, delivering value to the customer.
Defects also get in the way of delivering future value, by taking time
that could have been used to do new things. Finally, defects that get
through to the customer slow us down disproportionately. Since we
don’t have a test that shows the defect, it is often harder and more
time-consuming to fix.
OK. We don’t want defects. Hold that thought while we deal with
them.
Reporting Problems
The users are on line and perhaps in their own space, away from you.
They may want an easy way to submit problem reports. If they want to
183
Handling Defects
use email, that’s OK. If they ask you to build a problem reporting tool
right into the software, that’s OK too. The question is what do you do
with those emails when you get them.
Write them on cards. That’s right. You already have a good scheduling
mechanism. Why not use it?1 Let’s go on to ...
Scheduling Corrections
If the problem is of low enough priority, the customer can just write it
on a card (or someone can), and schedule the fix in a future iteration.
This is by far the best thing to do, as it uses your normal practiced flow
of planning. Where possible, write them on cards and schedule them
like any other story.
Keep track of the time spent fixing defects. Graph it on the wall. It’s a
potentially important consumption of resources, and well worth
watching. When you get some history on your velocity in the presence
of defect fixes, use that velocity figure in planning the next iteration. As
always, past history is your best indicator of future performance. But
keep the defect time broken out and displayed. It may be taking a big
bite of your development time, and if it is, you want to know it and do
something about it.
1.
Some folks want to have a defect database. We call that a defeatist attitude. On a green-
field project you may well be able to do without. Give it a try!
184
Handling Defects
When the flow of support issues is high enough, some XP teams dedi-
cate programmers to support and fixes, usually on some kind of rolling
schedule. Feel free to try this, as it has the advantage of letting the bulk
of the programmers focus on new value without interruptions. There is
an important downside to this approach, however: it reduces feedback
in your process. By masking the impact of problems, you lose quite a
bit of the incentive to avoid them. Some teams like to have defects
sting a bit as they come in, to help remind them not to send them out.
Customers, your acceptance tests let the defect through as well. While
the problem is fresh in your memory, specify new tests for that prob-
lem and anything it brings to mind. This is your chance to avoid some
irritation in the future. Programmers, do the same with the unit tests.
There’s a really good chance that when you do this, you’ll both find
additional problems. That’s great – those are problems that won’t
come back to slow you down later. Don’t let the press of time urge you
to skip the tests. It will just slow you down later. Testing makes the
whole project go faster and lets it deliver more value.
Preventing Defects
Let’s face it, the fewer problems we have, the better we’re going to like
it. It’s not realistic to expect no problems, but it’s not healthy to get
complacent. Sure, there are bugs in almost all software. So let’s not get
too excited when there are bugs in ours. Maybe we’re afraid that if
bugs aren’t inevitable, bugs in our code will mean we aren’t as good as
we should be. Well, we’ve all worked hard to get where we are, so we
probably are as good as we should be. But unless we’ve stopped learn-
ing, we aren’t as good as we could be. We aren’t as good as we can be.
185
Handling Defects
Where did they come from? One thing is almost certain: a bunch of
them will show up right at the edges of your testing. Problems will
show up right at the edge of your input testing, and downstream of
your last output check. Some of these, even if they’re many, will look
like there’s nothing you can do.
“If they send us bad data, there’s nothing we can do about it.” I wish I
was collecting a royalty on that sentence. It’s used all over the world,
and I’d be rich today. Often, however, there is something you can do.
Tighten up your validity tests, do some early cross-checking or hash
totals. Let your mind run a bit. Tightening this up with make your life
easier.
There’s another common event when you look at where the problems
are coming from. Often there’s a part of the system where they seem
to cluster. This is your clue to do a little scrambling in that area. Maybe
you need more tests there. Maybe a little refactoring is in order. Figure
out what’s needed, and give the area a little attention.
One more thing – often a team will notice a bug cluster in an area
where there hasn’t been pair programming. Yes, it’s true. Many teams
stop pairing once in a while, often in highly specialized areas. Take a
look at your defect sources. If they show more problems where there
has been less pairing – well, think about it. Maybe even do something
about it.
Summary
That’s the scoop on defects. Report them, schedule them, test to fix
them, prevent them. Go now, and bug no more!
186
Advanced Issue: Bug Databases
Sidebar - Chapter 22
Advanced Issue:
Bug Databases
After release, if you have multiple users reporting problems, you clearly
need some mechanism to keep track of them. If you have lots of users,
writing on a card may not do. In December 1999, Ron was involved in
a newsgroup conversation. Here, he is addressing whether a bug data-
base is needed during active, pre-release, full XP development.
Ken asked:
Bug tracking will allow you to uncover 'smells' in code (to use a
refactoring phrase). If there are a large number of problems in a
particular segment of your project then you may want to really
focus on that segment and stabilize it. How do you identify this
clustering unless you keep track of the errors. For example: we have
50% of our errors relating to monthly payroll.
Yes, the potential problem is real. If there are a large number of prob-
lems in some area (even over time), then that area needs focus. Ken
asks a good question: how do you keep track of this clustering?
How are the defects that cluster in BadClass detected and found? Or,
rephrasing the question slightly, and I hope harmlessly, how do the
existing XP practices naturally identify and deal with bug clusters?
187
Advanced Issue: Bug Databases
Susie and Bill, using BadClass as they build more function, encounter
those defects, which show up when their Unit Tests stop working
because of a problem in BadClass.
All its tests work now, so they write one that shows the defect. They fix
the defect.
Fixing BadClass, Susie and Bill notice that the code is ugly. They may
refactor it a little, or might make a note to do it later.
At the next stand-up meeting (no more than 24 hours away) they
report what they're doing. Because they're a little ticked off about
BadClass biting them, they mention it. Everyone gets a little nudge
that BadClass is in fact bad.
Soon, a couple of people will say “BadClass has been ticking us off
long enough, let's go after it”, and they'll do just that.
That's how the XP practices naturally identify and deal with bug
clusters.
During the active implementation of the system, then, the whole team
tends to know what's bad, and the bad tends to heal.
188
Advanced Issue: Bug Databases
Might this be enough to ensure that there will in fact be no bug clus-
ters? Well, it could happen. In my experience, it DOES happen. The
team knows where the system is weak and they go after the weak places
as a natural part of their work.
Yes. And the XP value of Simplicity suggests that a team might well
start with the core practices and improve their practices where they
need it, rather than assume they will need it and add weight to the pro-
cess from the beginning. That's where I'm coming from. I’d strive to
keep the defect count low enough to avoid the slower feedback from
the database. But when I needed the database, I’d get one.
There are other elements that could be picked up from the other
camps.
189
Advanced Issue: Bug Databases
YMMV, and if you use a bug database, you won't lose your XP merit
badge. But remember the Courage value, try the practices clean, and
watch for where you need process improvement. The real world will
surely surprise both of us.
190
Advanced Practice: Tests as Database
Sidebar - Chapter 22
Advanced Practice:
Tests as Database
Here’s a tip from Chet on keeping track of defects by using process
elements you already have: your tests. Stop relying on a trail of paper
or emails, or even a database, to keep track of bugs. Instead, write tests
to show bugs. You will need to decide how you will implement a
test-driven bug list. Since the unit tests must be kept at 100%, you
must decide how to have tests that document current bugs. There are
several ways to do this.
When the team responsible for capturing bugs finds one, they do
enough analysis to be able to reproduce it in a test. The test is then
passed along to the team responsible for fixing the bug. The correction
team now have a concrete example of the failure and are able to work
in the standard 'write the test first' mode.
191
Advanced Practice: Tests as Database
You’re doing this right when bugs are corrected quickly, without
excessive meetings and confusion about what the bug report meant.
192
Sidebar - Chapter 22
Test to show a defect
When a defect is detected, begin with a test.
You will need to decide how you will implement a test-driven defect
list. Since the unit tests must be kept at 100%, you must decide how to
have tests that document current defects. There are several ways to do
this.
When the team responsible for capturing defects finds one, they do
enough analysis to be able to reproduce it in a test. The test is then
passed along to the team responsible for fixing the defect. The correc-
tion team now have a concrete example of the failure and are able to
work in the standard 'write the test first' mode.
193
Test to show a defect
You’re doing this right when defects are corrected quickly, without
excessive meetings and confusion about what the defect report meant.
194
Chapter 23
Conclusion
We have been working with Extreme Programming for over four years,
and would never go back to what we did before. XP has equipped us to
communicate better with our peers, our customers, and our managers.
It has helped us to manage the stresses of working hard on something
we deeply care about. We think it’s good stuff and we recommend it to
anyone working on a project of suitable size and scope.
195
Conclusion
The rules and practices we have described here are not intended to be
followed blindly, although we do sincerely recommend that you start
by following the practices closely. What’s really important, though, is
not to work as if XP is a checklist, but instead to let it get into your
bones. Just as an XP project controls itself by steering — making small
adjustments as you go — the application of XP in your environment
should be the same. Get the feeling for what is happening in your
196
Conclusion
organization, get the data, then adjust your process, keeping in mind
simplicity, communication, feedback — and courage.
We have enjoyed the journey so far, and sincerely hope that you will as
well. If we can help — like the truth, we’re out there. Look us up.
197
Conclusion
198
Conclusion
199
Conclusion
200
Conclusion
201
Conclusion
202
Conclusion
203
Conclusion
204
Section I
Bonus Tracks
Here are some things we’ve paid a lot to learn. Since you
bought the album, we wanted to give you a little something
extra. Thank you, and we hope we passed the audition.
205
206
Chapter 24
We’ll Try
“We’ll try” can be the saddest words a programmer has ever
spoken, and most of us have spoken them more than once.
We’ve covered this material in other forms already, but it
bears repeating here.
These words are often the preface to months of grueling effort against
a deadline we know in our heart we cannot make. At the end, we come
up tired, burnt out, beaten, and short. Management hates us, we hate
ourselves, our families don't know us any more or have fallen by the
wayside. The software, if it works at all, is nothing to be proud of.
There has to be a better way. Here's one that couldn't really happen.
Suppose you knew everything they were asking for, and suppose you
knew how long it would take your team to do every one of the things
they were asking for.
Suppose you knew that, and you weren't afraid of the truth. Suppose
you wrote down everything they were asking for, maybe on little cards,
and you went in to them and laid your cards on the table.
207
We’ll Try
Suppose you said: Here's everything you have asked for, and on each
card I've put down how long it is going to take to get done. I've bro-
ken them down into three-week periods, and in each period I've put as
many cards as will get done in that period. As you can see, it will take
14 periods to do all this.
You lay a pencil vertically between two of the columns, and say: Here's
the date we want to deliver. We have too much to do. Our job now is
to put the cards we want the most on the left side of the pencil. When
we put a card over there, we have to remove a card with the same num-
ber on it.
They rant. They rave. They call you names. Secure in your perfect
knowledge, you say, This is how long each of these things will take. To
get the best product by our date, we need to put the cards we want
most on the left side of the pencil, removing cards with the same
number.
They threaten your job. You say, trying not to smile, This is how long
it will take our current team to do it. Maybe if you fire us, you can
find, recruit, hire, and train a team that will get it done sooner.
Bask for a moment in how calm, how strong, how totally cool and
heroic you would be, because you know how long it will take.
But that couldn't happen, could it? Yes, it could. We may not be able
to do as well as the lucky devil above, but it turns out we can do pretty
well. Here's how an XP team does it:
For a moment, get in touch with that feeling you have when you're
just coding along. The world goes away, you code and test and test and
code, and quickly you're done with whatever it is.
208
It couldn't be that easy! We’ll Try
The entire team, customers and programmers, goes through all the
stories. Customers explain what the system has to do, and the team
brainstorms quickly how it might be done. Estimate each card in pro-
grammer weeks: 1, 2, or 3 weeks of one programmer's time.
If a story seems much less than a week, batch it with a few other small
ones. If it seems more than 3 weeks, take that as a sign that you don't
understand it. Get the customers to break it down into two or more
stories and explain it again. Repeat until you have all the stories
estimated.
Now no one believes we can do all those stories in the time we esti-
mate, because there is so much else to do that we haven't counted.
We'll start by assuming we are off by a factor of three. Yes, three! We’ll
guess that it takes us three weeks to do what we could do in one per-
fect week. If that’s too conservative, we’ll know soon enough and
reflect it in future plans. We call our actual speed of delivering stories
209
We’ll Try What if you don’t have all the stories?
Pick an iteration size (we’ll use 3 weeks) and figure out how many pro-
grammer weeks there are in an iteration: 3 weeks X N programmers /
3. (We divided our factor of 3 back out. If we had guessed 2, we’d
have divided by 2.)
Voila, you plan for 1 week's work done in every iteration, for each of
your N programmers! This is a really good starting point.
210
How do you get estimates? We’ll Try
With all the programmers together, estimates will tend to average out.
And remember: we have our velocity adjustment to give us some slack,
and we will be revising the schedule many times as we go along.
Repeat until all teams have looked at all stories. If you take 10 minutes
per story, you can do 150 stories in 3 days. You'll probably wind up
going faster than that after the first few.
1.
In this book, there’s more about estimation in Iteration Planning (page 79), Experience
improves estimates (page 155), and How to estimate anything (page 217).
211
We’ll Try We can't tell management our real estimates!
Here are some of the things that make up the number. There are prob-
ably more.
You probably don't have all the stories, and some of the ones you do
have will change as customers learn more about what they really need;
You don't know your real velocity yet, and 1/3 is a fairly decent start-
ing point.
212
This can't possibly work! We’ll Try
You're asking how this could possibly work. The amazing thing is that
it actually works pretty well, even for your first estimate of the project.
But what makes it really work is that you do it again and again.
When you present your first Release Plan, explain to management how
you got the schedule. Then tell them that you do not believe this
schedule, and that neither should they. You go on:
“Many things can, and will change in the course of this development.
Customers will change requirements, some things will turn out to be
easier than we thought, and some will be harder. That has happened in
every project we have ever done, and it will happen this time.
“The difference with this project is that we will do this schedule every
nine weeks as we go along, and we will report the results to you. We
will refine all our estimates of the remaining stories, based on what we
have learned in the preceding iterations. Each time we get together, we
will all see how many stories are done, and how many there are to go.
“Each time we get together, expect to see that we are closer to comple-
tion. Expect also that the date may move in, and it may move out. But
you will be able to see exactly what our estimate is, and that will enable
you to make good decisions about the project.
“We're confident that we can give you quality information about how
we're doing, and we're confident that with that information you will
have the best chance of helping us be successful.”
And you will do just that: you will observe your own performance, and
you will estimate the stories over again based on what you know, and
every two months your ability to estimate the schedule will get better
and better.
Even better, customers and management will learn that you are telling
the truth as you know it; they will learn that your estimate of the
schedule is the best they can get; they will learn that they can help with
the delivery by providing you what you need, and by adjusting scope
judiciously to help you make the date.
213
We’ll Try Not with my management!
Well, if it's really that bad, I'd advise you to hit the silk.2
But more likely, management has been lied to, misled, bamboozled,
and spun so many times that they have come to believe that nothing
works but pressure.
Give the process a chance. Most managers really are more than ready
to use accurate information to make better decisions. Every time you
do this process, your knowledge of what will happen improves. If every
time you report, you tell them what you really believe will happen,
even the most dull management will finally figure out that your esti-
mates are better than whatever they are shouting.
In March, you'll say: “Notice that we got done about what we pre-
dicted. Our schedule shows that we'll be done in September, or June
on reduced scope.” They'll shout, but they won't be so sure. Expect
them to say you have to work harder. “We're working as effectively as
we can,” you reply. “Would you like to see our Tracking Report?”
2.
This is a reference to bailing out of an airplane. Used back when Jeffries was a boy.
214
Not with my management! We’ll Try
215
We’ll Try Not with my management!
216
Chapter 25 ‘
Let’s look now at a complex story and see how we might break it down
into tasks that can be estimated.
I didn’t promise a reasonable story, and in fact didn’t want one. Part of
what we’ll see here is that even a story that seems impossibly weird will
yield to task breakdown, especially if the team works together. Help
me out here. What are some of the tasks we have to do to implement
the story? Speak up, we haven’t implemented the Psychic Operating
System yet.
217
How to estimate anything
These tasks are starting to look reasonable. They all seem to be possi-
ble, except for maybe number 5, and it’s not hard to estimate how to
do them. For this story, some of them are minutes, so maybe we went
too far. In most stories, it’s enough to get the tasks down to a day or
so.
The team has taken a story they couldn’t possibly estimate, and broken
it down into finite tasks that they can actually estimate and do. This is
design. An XP team does design throughout the project, and we just
did a little bit of it here.
The tasks are mostly estimatable. The one that may not be easy to esti-
mate is broken out as well, and someone already has an idea. We’ll do
an experiment.
218
Chapter 26
Infrastructure
What about that database you need to build first? What
about that framework? What about that syntax-directed
command compiler? Get over it!
All too often, projects go dark for a few months at the beginning while
they build some absolutely necessary bit of infrastructure. Usually the
team really believes that it’s necessary, and that it will make things go
faster in the long run.
YAGNI: You’re Not Gonna Need It. This slogan, one of XP’s most
famous and controversial, reminds us always to work on the story we
have, not something we think we’re going to need. Even if we know
we’re going to need it.
219
Infrastructure
One project we know of took 100 percent of one of its final iterations
on infrastructure issues. This really confused the customers, who had
had stories in mind for that iteration. Doctor, it hurts when I confuse
my customers.1
Finally, someone got the answer. The story was “When we (the cus-
tomers) show up in the morning at 8 AM, the data is there live on our
systems.” Naturally, the customers wanted this — they gave it a very
high priority. And this story served quite well as the basis for the tasks
of writing the tasks.
Use this trick ruthlessly. Every time you are thinking of some big infra-
structure investment that you may have to make “behind the scenes”,
tie it back to customer business value. If you can’t, maybe the cus-
tomer doesn’t need it. If the customer doesn’t need it, don’t do it.
OK, you’ve got it tied to a story: “The system records name, address,
age, height, weight, and a lot of other personal information for each of
10 million patients. It can display any patient by record number and
can display statistical analyses for any and all stored data. It knows
everything that has ever happened to any patient at any time. And
that’s just the beginning.”
First of all, that story is too big to estimate. Let’s break it down. Here
are a couple of the new stories:
“The system records name, address, age, height, and weight for
patients.
1.
If you don’t know this joke, email us. No, on second thought, “Don’t do that”.
220
Infrastructure
OK, but doesn’t that still imply a database? Well, yes, but maybe not
right away. What if you wrote the initial system to know just those
fields for a few patients, and stored them on a file? Might you be able
to deliver some value with such a system? Sure you could. Up to 1,000
patients, or even 10,000, it might work just fine on the file.
On a file, you could easily add fields or change their size or content:
just write little programs to do it.
“Do you have a story for that,” we reply. “And how hard is integrity
anyway?”
Files actually have very high integrity, especially if you’re a little careful
about writing them in the right order. You could go a long way on
files.
But wait, won’t it be really hard to convert the system to use the data-
base after making it run on files? That could take ages!
Go back and read Code Quality on page 103, especially rule three, Say
everything once and only once. Quality, well-factored code for your
file-based application will have just one place that reads each record
221
Infrastructure
type, and one place that writes it. Those places will converge down to
just one place that reads an arbitrary record and just one that writes.
Those are the places — the only places — where the database access
code will have to go.
Now as you begin with XP, we realize you’ll have trouble fully signing
up for this philosophy. At first you’ll feel the need to do some amount
of infrastructure work, and you’ll have to find some technique to avoid
going completely dark on your client. Maybe the level of effort thing.
Observe what happens. We’re sure you’ll find that you don’t need to
invest as much in the future as you used to, and that things will actually
go more smoothly in the process. As you go along, put more and more
simplicity in at the beginning, relying on your ability to refactor to add
the generality — or the general tools — that you’ll need.
222
Chapter 27
Everyone knew that it wasn’t Chet’s fault, which was what made his
act so powerful. It jolted us all out of looking for who and back into
looking for what to do. It reminded us that we were a team, on a team
effort, and that we all accept responsibility for everything that happens.
It was one of Chet’s famous stone-snatching moves1.
Since then, other teams have taken advantage of the fact that it’s
Chet’s fault. Some just use the same old Chet who is writing this book,
but others have adopted their own Chet. Here’s a picture of the real
Chat and the Chet from a very special team in Omaha. See if you can
guess which is which.
Pictures of Chets.
1.
Another was the famous Balancing Hopes and Fears (page 225), where Chet snatched the
stone from the very hand of Beck himself.
223
It’s Chet’s Fault
224
Chapter 28
Those of you who have heard Ron, Ann, or I speak about XP are prob-
ably wondering where are all the war stories. Well, here’s one.
C3 was about ready to launch, the acceptance tests where in the low
nineties and moving steadily upwards, performance was within accept-
able levels. But we had one hole. Early in the project, we had decided
to retain the reporting portions of the legacy system. This would allow
us control the new system’s interface, and maybe, launch the first
phase of the system much quicker. Unfortunately, that interface con-
tained several hundred poorly understood data items. And it is difficult
to write acceptance tests when the customer doesn’t know what the
data means let alone what the correct value is.
About this time Kent made one of his periodic visits and after assessing
the situation, called us all together. We then had the most uncomfort-
225
Balancing Hopes and Fears
We had let our emotions rule our actions, so much so that we co-oped
Kent into our emotional state. Because we had allowed this major
piece of functionality to be developed outside the mainstream of our
process, it had no acceptance tests. We had no way of knowing how
close it was to being finished and no way of measuring its progress
towards completion except for days expended. This lack of knowledge
caused us to become afraid of the tapeworm and as we know "Fear
leads to Anger. Anger leads to Hate. Hate leads to Suffering."
226
Chapter 29
SharedManagedPopulation>>intervalToProcess
| slice start end |
slice := disclosures size // numberOfThreads.
start := (slice * threadNumber - 1) + 1.
end := (start + slice) max: disclosures size.
^Interval
from: start
to: end
We had written a little workspace code to make sure we had the algo-
rithm right, but now we needed to write a unit test. The method we
had written was self-contained; all the information we had needed was
stored as instance variables on the class. So to test it we would have to
create a series of SharedManagedPopulations, each with a collection of
227
Testing Improves Code
SharedManagedPopulation>>intervalForThread: threadInteger
numberOfThreads: threadTotalInteger
collectionSize: sizeInteger
SharedManagedPopulation>>intervalToProcess
^self
intervalForThread: threadNumber
numberOfThreads: numberOfThreads
collectionSize: disclosures size
The pressure to test has forced us to write better code. This is a pattern
we have encountered time and again during our XP experience, code
that can not be easily tested is not factored properly. This happens
because we are breaking an XP rule: Write the tests first. If we had
written the unit test first, instead of using an experiment written in a
workspace, we would have discovered the need for the helper method
much sooner.
Does this mean Rich and I are bad people? No, it just means we are
people. And, it also means this lesson has two morals. If you are having
228
Testing Improves Code
difficulty writing the test, refactor the code to split out the behavior.
And secondly, if you find yourself needing to split out the behavior to
improve testability, write the test first. In either case, you will discover
that testing will improve the quality of your code.
229
Testing Improves Code
230
Chapter 30
A change to use one LDAP over another sounds easy, right? Just
change a parameter or two and off you go. What I found was anything
but. The layouts of the two directory servers were different.
231
XPer Tries Java
The first attempt at making and testing this change was a nightmare.
The connection parameters resided in one of the java classes. A dupli-
cate file directory structure was copied on the test Unix server to
accommodate the changes being made to the SSJS and the java classes.
Changes to the files were being made using ‘vi’ on the test machine.
Since the test server did not have the correct environment to test the
change (no comment on this one), the files were then moved to a
pre-production machine. A make-file was run that compiled and linked
all the associated files together. The web server then had to be stopped
and restarted for the changes to take effect. The web browser then was
engaged through a special port (set up for testing) that started the
application.
This totally unacceptable. Each code change required five to ten min-
utes to verify its effects. Three developers spent three days mucking
around in all this code. The only error message displayed in the
browser was ‘Exception 52’ (translated: ‘The LDAP server is unavail-
able”). The end result was three days of frustration and nothing
accomplished (except knowing what a ‘52’ LDAP exception is). As a
bonus, all that hacking and slashing of files and directories corrupted
the already unstable environment and the server was unavailable for
over a week.
What we were missing were some good testing tools. The developers
in the group were used to writing applications in scripting languages
like SSJS and PERL. And boy did they love coding in ‘vi’! Coming
from a Smalltalk environment, I was used to working in a great IDE
and being able to step through RUNNING code to help debug prob-
lems. So I knew there had to be another way.
232
XPer Tries Java
Having used ENVY/Developer for many years, I was also familiar with
the paradigm on which VAJ operated. I immediately felt the power of
knowing that all the java code I wrote would be in a code repository
and I wouldn’t be dealing with files all over the place. And version
control would be greatly simplified.
The next step was to browse the code and try to make a little sense of
how it all operated so I could start writing some unit tests. The two
immediate advantages I had at this point were being in a good editor
and having VAJ browser tools at my disposal. Unlike the generic ‘vi’,
the ability to double click inside a pair of {} brackets and have the
entire block of code between them highlighted made reading code
much easier. The availability of the search tools in VAJ was what really
made things fly. Being able to open a class browser on referenced
classes and find senders and implementers of methods in a unified envi-
ronment makes you realize that there is no other way to do it.
It was now time to write a unit test. I figured I should be able to get a
simple test written that used the untouched imported code to connect
to our own LDAP and return a UserData object. Since I was running
VAJ on my local Windows’95 PC and the LDAP directories were on
our pre-production unix servers I at first thought that I would have to
set up some kind of local LDAP test server. But I decided against that
233
XPer Tries Java
strategy. One of our XP mottoes was ‘let Smalltalk tell you’. Even
though this was java, I knew that if there was a problem, the code
would tell me. I knew how VAJ worked and that it would actually help
me find any problems.
Since I had configured VAJ with JUnit as a tool, I simply right clicked
on the LdapTest class and selected Tools>Junit. The JUnit gui opens
and automatically runs the test. The best part of all this was that the
progress bar came up green….the test had run!! And it ran in 0.054
seconds. A few hours of work and I now had a repeatable test that
would test any changes I made to the Ldap classes. I think that is a
good thing. I then expanded the number of assertions to test every
piece of data the UserData object is supposed to retrieve.
Now that the test was up and running, I placed a breakpoint at the
start of the test and was able to step through the running code. There
is no better way to decipher how an application works than to watch it
do its thing. The reaction from other members of the team was imme-
diate. They realized that debugging time in this environment would be
only one-tenth of what it was currently. After watching me for that one
day writing tests and stepping through code, another member of the
234
XPer Tries Java
team took a small java application he had written, imported the code
into VAJ, and spent time walking step-by-step through the code he
had written.
Now I set my sights on the task at hand. I needed to change the code
to point to the new corporate LDAP directory server. The LdapServer
creates an instance of LdifServer which actually does the LDAP inter-
facing. After reviewing how LdifServer worked (by stepping through
the unit tests), I realized that a refactoring of the class was needed to
proceed. And with my unit tests in place I knew it would be a snap.
235
XPer Tries Java
if(!results.hasMoreElements()) return(null);
As you can see, the code was a mess. Not only was it hard to follow,
but the individual pieces were impossible to test. Without getting into
too much detail of how the LDAP interface works, the LdapConnec-
tion executes a search() function that takes a whole bunch of
parameters. These parameters specify which Ldap directory server to
connect to and what attributes to retrieve. It was obvious that refactor-
ing the search attributes would make it possible to subclass LdifServer
and override the connection parameters.
236
XPer Tries Java
}
catch(Exception ex) {systemOut(ex);
}
}
Since I had written my new unit test, I just hit the ‘Run” button and,
what do you know, the test ran! That wasn’t so hard. I decided to
refactor the other method so I could test it thoroughly.
if(!results.hasMoreElements()) return(null);
237
XPer Tries Java
ldapScope(),
ldapFilter(tid),
ldapAttributes(),
ldapAttributesOnly(),
ldapSearchConstraints());}
catch(Exception ex) {systemOut(ex );}
return(results);
}
I was now able to write more unit tests for each piece of the puzzle.
Here are a few examples:
try {ldifServer.connection().disconnect();}
catch(Exception ex){};
assert(!ldifServer.connection().isConnected());
assert(ldifServer.connection().isConnected());
assertEquals(ldifServer.ldapPort(), 389);
assertEquals(ldifServer.ldapScope(), 2);
assertNull(ldifServer.ldapAttributes());
assert(!ldifServer.ldapAttributesOnly());
assertNull(ldifServer.newUserData(null));
}
238
XPer Tries Java
After more refactoring and more tests, we were ready to move from
the test environment to real time. I exported the *.class files from
Visual Age and replaced the ones on the server. Then I logged into the
application. To my complete and utter non-surprise, it worked! This
compared to three days of hassle on the previous attempt with NO
success and no clues. A day’s worth of coding, testing, and refactoring
had gotten the job done.
239
XPer Tries Java
240
Chapter 31
A Java Perspective
We would like to thank Bill Wake for allowing us to use his
article. It is the second in a series entitled "The Test/Code
Cycle in XP". His website http://users.vnet.net/wwake
contains the entire series plus a whole lot more.
Example
Suppose we’re developing a simple search engine. We’d like the user
interface to look something like this:
241
A Java Perspective
We’ll develop it in the XP style, working back and forth between test-
ing and coding. The code fragments will reflect this: tests will be on
the left side of the page, and application code on the right.
Model First
When you’re creating a GUI (graphical user interface), you should
develop and test the model first. We’ll assume this has been done, and
that it has the following interface:
242
A Java Perspective
243
A Java Perspective
assertNotNull(panel.resultTable);
}
Things to notice:
We can make another test, to verify that the widgets are set up
correctly:
244
A Java Perspective
public SearchPanel() { }
}
Testing Interconnection
Somehow, we must associate a Searcher with our GUI, and verify that
we display its results.
The panel still can’t do much, but now we can associate a Searcher
with it.
245
A Java Perspective
Because this is a unit test, I don’t want to depend on the real Searcher
implementations: I’d rather create my own for testing purposes. This
lets me control behavior in a fine-grained way. Here, I’ll create a new
Searcher called TestSearcher. We’ll have the query string be an integer,
which will tell how many items to return. We’ll name the items "a0"
(for first author), "t1" (second title), etc.
But first... a test. (Notice this is a test of our testing class, not of our
GUI.)
Go through the usual compile/fail cycle, and create the test classes,
starting with TestDocument:
246
A Java Perspective
The TestResult class has a constructor that takes an integer telling how
many rows should be present:
TestSearcher uses the number value of the query string to create the
result:
0, 1, Many
We’ll build tests for the 0, 1, and many cases:
At last, we’re using the GUI: setting text fields, clicking buttons, etc.
247
A Java Perspective
We run the test - and it passes! This means we already have a working
solution - if our searcher always returns 0 items.
We move on:
When the button is clicked, we want to form the string in the text field
into a query, then let the searcher find us a result we can display in the
table. However, we have a problem in matching types: the Searcher
gives us a Result, but the table in our GUI needs a TableModel. We
need an adapter to make the interfaces conform.
248
A Java Perspective
Adapter Implementation
Let’s write the button code as if a ResultTableAdapter class existed:
findButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Query q = new Query(queryField.getText());
resultTable.setModel(
new ResultTableAdapter(getSearcher().find(q)));
}
});
249
A Java Perspective
implement the column names, column and row counts, and finally
getValueAt().
switch(c) {
case 0: return doc.getAuthor();
case 1: return doc.getTitle();
case 2: return doc.getYear();
default: return "?";
}
}
}
What else can give you problems? One possible problem occurs when
we do a sequence of queries - can we get "leftovers"? For example, a
query returning 5 items followed by a query returning 3 items should
only have 3 items in the table. (If the table were improperly cleared, we
might see the last two items of the previous query.)
250
A Java Perspective
sp.queryField.setText("5");
sp.findButton.doClick();
assert(sp.resultTable.getRowCount() == 5);
sp.queryField.setText("3");
sp.findButton.doClick();
assert(sp.resultTable.getRowCount() == 3);
}
(Would we bother with these tests? Perhaps not, we might just put the
panel on-screen and deal with its contents manually. There are times
when such tests would definitely be appropriate: perhaps when we’re
working against a style guide, or when the window format is expected
to be stable.)
To make this test run, we need to put our panel in a frame or window.
(Components don’t have their screen locations set until their contain-
ing window is created.)
251
A Java Perspective
display.setSize(500,500);
display.setVisible(true);
The test fails, as we haven’t done anything to put widgets on the panel.
(You can un-comment the sleep() if you want to see it on-screen.)
252
A Java Perspective
public SearchPanel() {
super (new BorderLayout());
findButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Query q = new Query(queryField.getText());
resultTable.setModel(
new ResultTableAdapter(getSearcher().find(q)));
}
});
JPanel topPanel = new JPanel(new BorderLayout());
topPanel.add(searchLabel, BorderLayout.WEST);
topPanel.add(queryField, BorderLayout.CENTER);
topPanel.add(findButton, BorderLayout.EAST);
this.add(topPanel, BorderLayout.NORTH);
253
A Java Perspective
this.add(new JScrollPane(resultTable),
BorderLayout.CENTER);
}
Main
To complete the system, we’ll create a main() routine:
Conclusions
254
A Java Perspective
Unit tests can be tedious to write, but they save you time in the future
(by catching bugs after changes). Less obviously, but just as important,
is that they can save you time now: tests focus your design and imple-
mentation on simplicity, they support refactoring, and they validate
features as you develop.
Resources
• code for this example can be found on
http://users.vnet.net/wwake.
• The Test/Code Cycle in XP: Part 1, Model, William Wake, found on
http://users.vnet.net/wwake.
• Extreme Programming Explained, Kent Beck.
• Refactoring, Martin Fowler.
• JUnit can be found on http://www.XProgramming.com.
255
A Java Perspective
256
Chapter 32
A True Story
Ron Jeffries [re]learns something about simplicity.
It turns out that Dolphin doesn’t have an object that represents a file
directory or folder. It can give you a list of “file specs” that match a
pattern. Well, obviously I’m going to need a Directory object, so I
started on it last night. I wrote some tests, some code, and got a
decent start at it. I put a few hours into it, and it was a bit harder than
I thought, and I hadn’t even gotten to the idea of a Directory holding
another Directory. So I posted a question on the Dolphin news group,
and went to bed.
257
A True Story
This morning I still wanted the list of files that weren’t in the book,
but I had come to my senses. The value was in that list of files, not
some general support for directories. I went back to the story: report
which files in the book directory aren’t in the book file.
Then I wrote a test that asks the BookAnalyzer for the “unused files”.
I implemented that by intention, by asking for the chapter files and
removing all the used files. I implemented the chapter files by asking
for the “.fm” files and removing all the “backup.fm” files. I imple-
mented the “.fm” files by getting the list of file specs from the book
directory that matched “*.fm”.
Well, guess what. Done in an hour, works perfectly, and doesn’t need a
Directory object at all.
You might have one major concern – didn’t I wind up with some very
specialized and hacked-together code, instead of the Directory object I
will really need some day?
258
A True Story
259
A True Story
260
Chapter 33
We believe, and therefore teach, that we cannot know how long it will
take to do software we’ve never done before. Therefore we cannot,
with integrity, promise exactly what will be done. What we can do is
estimate what will be done, and promise to do our best and to tell the
truth about what happens.
No one really knows how long the project will take. The very best ways
of finding out only result in estimates. If we were to break the project
down into tiny tasks each of which was obvious, and arrange them in a
PERT chart with a complete contingency analysis, and if we did every-
thing we could think of like that, what we would wind up with would
be a very complex estimate. The first snow day or the flu could make a
mockery of our plan. Something will make a mockery of any such plan.
Therefore all planning processes have mechanisms for dealing with the
uncertainties. They produce a range of results, perhaps, or they fudge
the result by multiplying all the estimates by 3.14.
261
Estimates and Promises
Here’s what programmers can promise, and what the practices in this
book will help deliver:
1. You can promise to estimate the difficulty of the entire project, each
story in the project, and each task in each story, as accurately as you
can.
2. You can promise to track those estimates to improve your ability to
know how hard things are going to be.
3. You can promise to track delivery performance, to make it clear
how fast the project is really going.
4. You can promise to use the facts to refine the picture of when the
project will be done, by estimating again as you learn, and by using
the velocity so far to predict where you’ll be by any given date, or
to predict the date when you’ll have chosen features done.
Estimate the stories by breaking them down into “tasks” that are small
enough to understand and to estimate in day-sized chunks.
Track the tasks you do in each iteration, and note how long each one
takes compared to how long you expected. This gives you an even bet-
262
Estimates and Promises
If you have a critical release date, expect that you will have to make
some choices of what to leave out. And remember – that’s a good
thing, because so many projects deliver less than you asked for without
letting you choose what you get.
Because an XP team can give you good estimates of how hard each
story is to do, and good reports of how long such things take, you can
make your project a success in spite of the fact that no one really knows
exactly how long the project will take.
263
Estimates and Promises
264
Chapter 34
In XP, programmers write unit tests for all their code. The rule is, Test
everything that could possibly break.
Sometimes people get really angry at us when we talk about this rule.
Don't you know it's impossible to prove that a program works by test-
ing, they'll shout. Don't you know an infinite number of things could
go wrong?
Yes, your authors took those courses and read those papers, too. Hold
your horses, as people used to say in Jeffries' day, and hear us out.
First of all, like all the XP rules, this one is meant to make us think, and
to keep us on the hook. In XP we turn all the dials up to 10, not up to
Reasonable.
265
Everything that could possibly break
So we Test to show a defect (page 193), and while we're at it, we make
some notes and write a few more tests, whatever this mistake reminds
us of.
But wait, don't answer yet. There's more! If you go back and see what
is really being said in those famous old theories, it's that it's not possi-
ble to completely test any program of sufficient complexity. Those
theories always show the exponentially-increasing number of branches
and paths through the system, and they whine about how you couldn't
possibly test them all.
This is all true. The more complex an object is, the harder it is to test it
completely. But the reverse is also true: the simpler an object or
method is, the easier it is to test.
When we are testing some object and we can see things that might
break, but they seem impossible to test, we let the pressure to test
everything that could break flow back into the design. Maybe we can
simplify this object, break it up, make it easier to test.
It turns out that usually we can simplify the code and make it easier to
test. In the process, we make the code easier to understand, and easier
to get right. It's a win all around. We get better code and better
testing.
Try to test everything that could possibly break. You'll be glad you
did.
266
Everything that could possibly break
The rule is to test everything that could possibly break. To save time,
don't test things that couldn't possibly break. There are more things
that couldn't possibly break than you might imagine.
Even if it's more complex, sometimes code just can't break. Here's an
example in Smalltalk:
printAccounts
accounts do: [ :each | each print]
That method loops over the collection named accounts, and tells each
account to print. The method can't possibly break. There are other
things that could break: accounts might not be a collection, and
depending on the code you might need to test that. Accounts might
not know how to print themselves, or their printing might break. You
might need to test that. But, in my opinion, you don't need to test the
printAccounts method. (Just my luck there'll be a bug in it. Well, live
and learn.)
267
Everything that could possibly break
That same day I was browsing the code, as I do when I stop by to visit,
and I found a method that couldn't possibly work. I mean could not
possibly work. There was an if in it, and one of the branches answered
a different kind of object from the other branch. Sure enough, there
were no tests for that branch of the if. The right thing to do might
have been to write a test and then fix the method. In fact, however,
when we looked at the users of the method, no one was using the
object in a way that could ever invoke that branch of the if. So we
removed the feature instead of writing the test.
268
Everything that could possibly break
!Transaction methodsFor!
setAmount: anAmount
amount := anAmount!
value
^amount! !
deposit: anAmount
"A deposit is a Transaction with a positive amount"
^self new setAmount: anAmount!
withdraw: anAmount
"A withdrawal is a Transaction with a negative amount"
^self new setAmount: anAmount negated! !
We have written two tests, one that checks an Account with no entries
(written mostly because it was the simplest test that we could think of),
and one that checks an Account with three entries:
!AccountTest methodsFor!
testEmpty
self should: [Account new balance = 0]!
269
Everything that could possibly break
testThree
| account |
account := Account new.
account add: (Transaction deposit: 100).
account add: (Transaction withdraw: 15).
account add: (Transaction withdraw: 25).
self should: [account balance = 60].! !
The class contains transactions. We'll see below that these are an
OrderedCollection (similar to Java Vector).
add: aTransaction
self transactions add: aTransaction!
This method gets the transactions collection (see below), and adds the
method parameter, aTransaction, to the collection. The method add: is
standard behavior on OrderedCollections, and can add anything at all
to the collection.
balance
^self transactions
inject: 0
into: [ :sum :each | sum + each value]!
This particular usage takes the transaction collection and sums what-
ever it gets by sending value to each Transaction. (We happen to know
270
Everything that could possibly break
that this will be a positive number for a deposit and a negative number
for a withdrawal.)
transactions
transactions isNil ifTrue:
[transactions := OrderedCollection new].
^transactions! !
Now then. We want to assert that for our new class, Account, we have
tested everything that could possibly break. (Hold your objections
regarding the Transaction class and the system as a whole for a
moment. Let's just talk about Account. What else could break?
Transactions that don't answer numbers when sent value could blow up
the system, but the error would not be in this class. (Some people
would suggest asserting that aTransaction is a Transaction, or that it
supports some interface. That would address system integrity, but
would not increase the correctness of the code we have, in our
opinion.)
Now then. We have not shown the tests for Transaction. Would they be
hard to write? Probably not. The big question would be whether we
should check integrity for the amounts, determining that they're
numeric. Again, not our job in the style we advocate, but in Eiffel
you'd certainly be tempted. Since Transaction has essentially no behav-
ior, it's not hard to test.
271
Everything that could possibly break
That leaves us with the system problems like “What if someone creates
a non-transaction and sends it to an account”? Well now. Any class that
(thinks it) is creating Transactions and putting them in Accounts must
have a test. Would it be hard to test? We don't know, but our rule is
that we must test everything about it that could possibly break. If the
code visibly started with numbers and created Transactions with
deposit: and withdraw:, it would probably be pretty clear how much to
test.
And finally, remember that there are acceptance tests as well. These are
designed to test whether the system, in actual use, every gets wrong
answers or blows up. If/when these tests found something, they would
point, almost inevitably, to a particular class (like AccountBuilder) that
was doing something wrong, i.e. that wasn't tested enough. We would
(a) learn something about what to test in such classes, (b) add the nec-
essary test[s], and (c) add similar tests everywhere we thought we
needed them.
272
Everything that could possibly break
273
Everything that could possibly break
Other testers might test more or less. It always comes down to one’s
best professional judgment in the specific case: have I tested everything
that could possibly break in this code?
274
Annotated
Bibliography
Robert C. Atkins, M.D., Dr. Atkins’ New Diet Revolution, Avon Books,
1999; ISBN 0-71001-00750-3.
Don’t ask.
275
For Smalltalk programmers, you can do no better than to follow these
patterns to writing clear, consistent, communicative code. Need a cod-
ing standard? Use this one.
Boris Beizer, Black-Box Testing, John Wiley and Sons, 1995; ISBN
0-471-12094-4.
Beizer hopes “that for most of us, testing ceases to be a profession, but
an inseparable aspect of what every conscientious developer routinely
does.”
David Bellin and Susan Suchman Simone, The CRC Card Book, Addi-
son-Wesley, 1997; ISBN 0-201-89535-8.
Bellin and Simone take CRC where it has never gone before. Good
material on brainstorming, requirements gathering, collaboration.
Even some implementation.
Kenneth Blanchard, Ph.D. and Spencer Johnson, Ph.D., The One Minute
Manager, Berkley Books, 1982; ISBN 0425098478.
276
Be present. Delegate. Follow up.
Dan Carrison and Rod Walsh, Semper Fi, Business Leadership the Marine
Corps Way, American Management Association, 1999; ISBN 0-8144-0413-8.
You think not? Think again. What about recruiting the best, communicating
clearly, leading with integrity, accepting responsibility?
277
Alistair Cockburn, Surviving Object-Oriented Projects, A Manager’s Guide,
Addison-Wesley, 1998, ISBN 0-201-49834-0.
Alistair is a project anthropologist. He has visited many projects, observing
not just what they say, but what they really do. From this experience, he pro-
vides guidance and suggestions for your projects. Alistair favors a light
approach to projects. The book is worth it for the pullout checklist alone, but
has much much more!
James C. Collins and Jerry I. Porras, Built to Last, HarperCollins, 1997; ISBN
0-88730-739-6.
What makes great companies great? Collins and Porras studied eighteen
“visionary” corporations, with an eye to what makes them truly different. If
you must be in a large company, these are the kind you’d like to be in.
Daryl R. Conner, Leading at the Edge of Chaos, John Wiley and Sons, 1998,
ISBN 0-471-29557-4.
________, Managing at the Speed of Change, Villard, 1992, ISBN
0-679-40684-0.
Conner sees his job as “leading people through the jungle of change.” With
its focus on embracing change, XP is one tool for dealing with the need for
rapid change in information systems.
The Editors of Cook’s Illustrated, The Best Recipe, Boston Common Press,
1999; ISBN 0936184388
“Would you make 38 versions of Creme Caramel to find the absolute best
version? We did. Here are 700 exhaustively tested recipes plus no-nonsense
kitchen tests and tastings”. Here is the best cook book I have seen. They
really did try 38 different Creme Caramel recipes and explained which one
was best and why.
278
Alan M. Davis, 201 Principles of Software Development, McGraw-Hill, Inc.,
1995; ISBN 0070158401.
This book is a "reader's digest" of some of the classic software engineering
references.
________, Why Does Software Cost So Much?, Dorset House, 1995; ISBN
0-932633-34-X.
This book of essays covers a lot of issues, and they’re all interesting and
enjoyable. Tom’s angle on managing the software process is light-handed
and collaborative. Again, he was there first.
Barrie Dolnick, The Executive Mystic: Intuitive Tools for Cultivating the Win-
ning Edge in Business, HarperCollins Publishers, 1999; ISBN 0887309542.
Ron got this confused with The Corporate Mystic. Both books are worth
reading, but are completely different. So when Ron was explaining what he
got out of the book, it was quite humorous….
Carlton Egremont III, Mr. Bunny’s Big Cup o’ Java, Addison-Wesley, 1999,
ISBN 0201615630.
279
A humorous look at Java and its programmers. As Smalltalkers, we found it
almost enough to make Java tolerable. But take our word for it, it’s a fun
read. This is not a techical book.
Emily Eisele, You Don’t Eat Spiders, not yet published, 1996.
A common sense look at the complexities of life in the post-modern world.
Daniel D. Ferry and Noelle Frances Ferry, 77 Sure-fire Ways to Kill a Software
Project, Buy Books on the web.com, 1999; ISBN 0-7414-0010-3.
By thinking “what could we do to really screw this up”, the Ferrys provide a
delightful yet terrifying reminder of most every mistake possible in the soft-
ware business. Well, 77 of them, anyway. Short, fun, scary.
280
Daniel P. Freedman, Gerald M. Weinberg, Handbook of Walkthroughs, Inspec-
tions, and Technical Reviews, Dorset House, 1990, ISBN 0-932633-19-6.
XP’s Pair Programming obviates the need for most inspections and reviews.
If you must do them, do them right. Freedman tells you how.
Erich Gamma, Richard Helms, Ralph Johnson, and John Vlissides, Design
Patterns, Elements of Reusable Object-Oriented Software, Addison-Wesley, 1995;
ISBN 0-201-63361-2.
A collection of key design patterns, providing description of problem and
solution. These patterns now serve as the official terminology for these
ideas. Know them and use them in your designs. If you can’t quote from this
one, your computer geek friends will make fun of you.
Donald C. Gause and Gerald M. Weinberg, Are Your Lights On?: How to Fig-
ure Out What the problem Really Is, Dorset House, 1990; ISBN 0932633161.
This is a fun book about problem solving and creativity.
James Gleick, Genius: The Life and Science of Richard Feynman, Pantheon,
1992; ISBN 0679408363.
The story of one of the great minds of the 20th century. The man who used a
spike solution to discover why the shuttle Challenger exploded.
281
Adele Goldberg and David Robson, Smalltalk-80: The Language, Addi-
son-Wesley, 1989; ISBN 0201136880.
If you use Smalltalk, you know the purple book. If you are just wondering
why we won’t let it go, here is the bible.
Gay Hendricks, Ph.D. and Kate Ludeman, Ph.D., The Corporate Mystic, Ban-
tam Books, 1996; ISBN 055337494X.
One of the groups I work with recommends this book to all of their employ-
ees.It addresses integrity, leadership, working with others. Excellent.
282
upcoming Olympics. Good ideas, good focus on personal skill. Rather regi-
mented.
283
Simplicity, Clarity, Generality: the cover says it all. Good programmers,
especially Extreme ones, profit from having a deep bag of tricks into which
they can dig when they need to. Kernighan and Pike show that the tricks
need not be complex — the best tricks are delightfully simple.
Steve McConnell, After the Gold Rush, Microsoft Press, 1999; ISBN
0-7356-0877-6.
McConnell believes that licensing is coming as software development
moves into the future. We share a concern over quality and good practice.
Our approaches differ substantially, as we lean much more toward simplicity
and good internal practices. But read this book.
284
Glenford J. Myers, Reliable Software through Composite Design, Mason/Char-
ter Publishers, 1975; ISBN 0-88405-284-2.
Together with Constantine’s work on Structured Design, one of the seminal
works on modularity. High cohesion, low coupling, just how to build good
objects even today.
Miyamoto Musashi, A Book of Five Rings, The Overlook Press, 1974; ISBN
0-087951-018-8.
Originally written in 1645, this book hold the philosophy of Japan’s most
renowned warrior. He intended this book “for any situation where plans and
tactics are used”. It’s no longer acceptable to kill your programmers with
swords, but this is a fascinating book.
M. Scott Peck, M.D., The Road Less Travelled, Simon and Schuster, 1978;
ISBN 0-671-25067-1.
There comes a time when we need to take a look at our lives.
285
Elaine St. James, Simplify Your Life: 100 Ways to Slow Down and Enjoy the
Things That Really Matter, Hyperion, 1994; ISBN 0786880007.
A small book with lots of advice for thinking about your life and what's
really important.
Michael Schrage, Serious Play, Harvard Business School Press, 2000; ISBN
0-87584-814-1.
Collaboration, play, “Demo or Die”. Learning what you want comes from
playing with what might be. Schrage offers prototyping as a way of life. XP
suggests making your prototypes real.
Philip Toshio Sudo, Zen Computer, Simon and Schuster, 1999; ISBN
0-684-85409-0.
This short book asks us to acknowledge the spiritual, meditative side of our-
selves as we work with the computers we face every day. It’s not deep, not
heavy. Rather, it’s mild and calming. Good preparation for facing the Blue
Screen of Death.
Guy L. Steele, Jr., the Hacker’s Dictionary, Harper & Row, 1983, ISBN
0-06-091082-8.
“BOGOSITY - the degree to which something is bogus. See autobogopho-
bia, a fear of becoming bogotified.”
Dave Thomas, Spicy Chicken Sandwich, Fresh Every Day, 2000; 213 G 410
Cal.
One of the other Daves. Good cook, we don’t know about his programming.
Sun Tzu, James Clavell, The Art of War, Delta, 1999; ISBN: 0385299850.
Life is a battlefield. Plan to win.
286
________, The Psychology of Computer Programming, Dorset House Publish-
ing, 1998, ISBN0-9263-42-0.
This is the silver anniversary edition! Way back in ‘71, Weinberg made it
clear that programming is a people business, not a technical business. Why
won’t Ron ever learn?
Garry Willis, Lincoln at Gettysburg, The Words That Remade America, Simon
and Schuster, 1992; ISBN 0671769561.
How those 272 words we memorized in school changed the meaning of the
Constitution. A reminder that a powerful message simply expressed can
change the world
287
Good material on the use of coupling and cohesion. Dated now, in these
days of objects.
288