The value of code

Value in stories

Generally, the features on a project should be those that will provide a value to the customer, for some definition of ‘value’. We can use the template

As a <role>
I want <something>
So that <I get some value or benefit>

to help us describe the stories which we use to prompt conversation about what that value is. The value doesn’t have to be monetary; it could mean a reduction in risk, an ease of maintenance, the ability to use a technology that will still be supported in five years’ time.

Value in code

A piece of code should provide value to the class, module, application, system etc. in which it resides. If a piece of value can be encapsulated into one block of code, chances are that it’ll be easier to read, maintain, find and delete, should that value ever become obsolete.

Here’s an example of something that I didn’t find any value in. I was coding John Conway’s Game of Life as a kata.

public void testShouldReturnTrueIfTheCellHasLife() {
    Grid grid = new Grid();
    grid.causeBirth(4, 8);
    assertTrue(grid.hasLife(4, 8));

This isn’t valuable, because I can easily make this pass with

public class Grid {

    public boolean hasLife() {
        return true;

One way to get around this is to add another test:

public void testShouldReturnFalseIfTheCellHasNoLife() {
    Grid grid = new Grid();
    grid.causeBirth(4, 8);
    assertFalse(grid.hasLife(4, 2));

So now we have two tests, describing one benefit. I prefer this:

public void testShouldDetermineIfACellHasLife() {
    Grid grid = new Grid();
    grid.causeBirth(4, 8);
    assertTrue(grid.hasLife(4, 8));
    assertFalse(grid.hasLife(4, 2));

One block of code, one value which it provides. Using two tests for this would mean that the value was split; neither test has is truly valuable without the other.

Value in tests

The test describes the behaviour of the code; that is, the features that the code provides, and the value that you can expect from those features.

The test doesn’t fully define the behaviour. There’s no definition for what happens if the parameters are negative numbers, for instance, or any indication as to whether the grid can cope with non-integers. There’s no indication of what might cause the behaviour of the grid to change.

Normally, when we have a story, we have some scenarios which are associated with that story. The scenarios form acceptance tests for that story, but they may not completely define the system behaviour. After all, if all the scenarios defined for a story play out, then any bug is just a scenario we didn’t think of – and we still find bugs.

In a story, the story card is just a placeholder for a conversation about that story; the scenarios are there to help you work out if the story has been developed properly.

In a test, the name of the test method forms the ‘story card’ for that piece of code, and the implementation of the test forms the ‘scenarios’. But it’s still just a placeholder for conversation.

This entry was posted in Uncategorized. Bookmark the permalink.

2 Responses to The value of code

  1. anonymous says:

    What could you say if the return type of the method was not a boolean, but an enum? What if the enum had 3 possible values? Or 30? Would you still use this technique? Why? Or why not?

    Tim Haughton

  2. sirenian says:

    I’d ask what value I was trying to get from the code, and how I might achieve a reasonable certainty that I’m getting it (I get to decide what a reasonable certainty is).

    I would then think of a suitable phrase that describes the behaviour that gives me the value. (Although, sometimes, I know what the behaviour is; it’s putting the value into words that proves the problem.)

    Then, I’d do as above, for as many values as necessary to give me my reasonable certainty. The grid may be 100 x 400 in the example above, but I don’t have to check every coordinate to be reasonably certain that the grid is behaving. I just need to check two of them.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s