Jul 30 2014
Last time, MojoTech Developer David Leal discussed some reasons that should help you to at least consider writing living documentation. (The Why.) In this article, he explores what makes good living documentation. (The How.) Update: Be sure to check out Part Three: How You Can Implement Living Documentation
The examples are written in Gherkin, Cucumber’s living documentation format, because Cucumber is the tool I use at work. It is not perfect — in fact, it has a few annoying shortcomings — but I believe it is a step in the right direction. No matter what tool you think is best, this article will benefit you anyway.
This article will focus on documentation that is meant to be written by developers, for developers. In the last article, I showed that developers benefit from this kind of documentation in several ways:
We have come a long way since Cucumber first introduced its
web_steps.rb. I remember writing my first features the way many did at the time:
This scenario, as it is written, has several problems.
First, it exposes a few implementation details: database-specific field names (
shot_at) and a URL.
When we write living documentation, we should not be concerned with implementation details. As I said above, living documentation is supposed to help you think about the domain, not the implementation. It should be written before you start writing code.
If I had an hour to solve a problem I'd spend 55 minutes thinking about the problem and 5 minutes thinking about solutions.
— Albert Einstein
Second, it’s not clear why
shot_at is required. What happens if we remove it? And, whatever does happen, why does it happen?
Third, the scenario is written in an imperative manner, a bit like a user manual. “Visit a”, “click b” they describe how to do something, but they don’t describe what the user is doing — viewing a photo shoot. Imagine that there are more scenarios that use the photo shoot page. Are we going to keep repeating the same steps over and over again?
Plus, “URLs”, “visiting”, and “clicking” are also UI concepts — another kind of implementation detail. We don’t care about links, text fields or combo boxes (unless a business rule explicitly mentions these details.)
Fourth, do you think that scenario is readable at all? I certainly don’t. The combination of problems I described above, and the unpleasant duplication (see how the photo names are repeated twice?), make the scenario boring and artificial sounding. I can understand why a software developer would write it this way, but I can’t imagine why anyone (even other software developers) would want to read it. It will become another maintenance burden, instead of being regarded as an important piece of documentation.
So now it’s time to try to fix the problems mentioned above. Here’s the same scenario, written the way I currently believe it should be written:
This scenario is much shorter than its previous iteration. Each bit of information is there for a reason.
A designer knows he has achieved perfection not when there is nothing left to add, but when there is nothing left to take away.
— Antoine de Saint Exupéry
Not to say it’s perfect, but at least I don’t know what else I could take away.
But now we have a few problems. For example, nowhere is it mentioned that the client must be signed in. And what’s a “completed” photo shoot?
I’ve come to believe that living documentation is mainly about 2 different areas:
Entities in the system, and the set of all relevant properties that comprise them. Relevant properties are those that are part of the business domain (i.e, they’re not simply an implementation detail.)
The behavior of the system. This is where someone, or something, under a certain role causes the system to move from a state to another. This state, in turn, can be divided into state of the UI, and state of the data.
I believe the scenario above is about a behavior of the system. In this case, only the UI changes. As will often be the case, we need to complement these behavioral scenarios with entity-based scenarios. So we’ll first need to define what it means for someone to be a client:
So we have just unambiguously defined an entity in the system and the context in which that entity is usually referenced. As a nice side effect, this eliminates the need to keep repeating steps like I am signed in as a client.
One down, one to go. What exactly is a “completed” photo shoot? Before I explain, let me ask you: where would you look?
If you answered
photo_shoot.feature, you’re absolutely right!
Before I show the definition of a completed photo shoot, remember how the first iteration we discussed contained an explicit reference to
shot_at? I’ll fill you in on why it’s used:
shot_at is used by the UI code to determine whether to show the photos listing or a message saying the photo shoot hasn’t happened yet, and some tips to make the most out the photo shoot (how to dress, what to expect, etc). That could look like this, if using Ruby on Rails:
And that is why it’s being set in the scenario. The problem is, we are substituting an implementation detail for an actual business concept. It doesn’t matter which field we use — what we are really interested in knowing is whether a photo shoot took place already. To do that, we must come up with a term that will unambiguously express that state. In this article, after considering the problem for a few seconds, I decided to go with “completed”. In real life work, some terms will be easy to come up with, others will likely require some thought and discussion.
So, now that we have a fitting term, we can refactor our template code:
But in real life this refactoring would probably not have been necessary. Having written the scenario first, I would be aware of the appropriate term during implementation, and naturally, I would have used it the first time.
(As an aside, * is a valid Cucumber step marker, just like Given, When, and Then.)
This is acceptable, but there is a potential problem: if the photo shoot doesn’t have a shooting date, what state is the photo shoot in? If only the completed state matters, it’s possible that doesn’t make a difference. On the other hand, you’re left with uncertainty that may come back to bite you in the future. Let’s say we decided that there are no other relevant states. We can simply say that a photo shoot is “not completed.” We can make that decision explicit:
In this article, I’m not dealing with validation issues, like entering a shooting date that is in the future, or trying to set a shooting date without associating any photos. Are any of those possible? We don't know but, if this was actual documentation that we were writing, we would find out. The response to these questions would likely influence our understanding of what the completed state means, but it also illustrates how useful it is to explore the domain we’re working on. Once we’ve properly defined the problem (and recorded that understanding using living documentation), it should be easier for us to come up with better solutions.
These two examples illustrate how we can use living documentation to document the domain’s entities. This approach gives us the following benefits:
With the definitions out of the way, we can go back to compare the two scenarios:
First, all implementation details are gone. There are no more references to id columns or URLs.
Second, we replaced the reference to
shot_at with the more concise and understandable concept of photo shoot “completeness.”
Third, the imperative steps have been replaced with the concept of viewing a photo shoot. This is another implementation-to-concept kind of substitution, and — again — it helps us discuss the scenario (and the concept) with other people.
Fourth, the scenario is now much easier to read and understand. The first version of this scenario was written in the manner of “show, don’t tell” — it forces the user to infer the business rule. But, when writing living documentation, this technique is not helpful. We need to tell, concisely and unambiguously, what each feature is about. We don’t want to waste our readers’ time, and force them to do unnecessary work.
I’d like to conclude with a few clarifications about the process of writing living documentation:
First, this is not Big Design Up Front. I’m not arguing that you should document the entire system in one sitting and only then proceed to implementation.
Second, this is all about the domain. Consequently, I don’t believe it should be relegated to a unit test framework, just because it deals with a single entity in the system. Living documentation is not simply about end-to-end tests. It’s about documenting your system properly, and basing that documentation on tests that give you enough confidence to believe that the system is working.
I hope this article has helped clarify how good living documentation looks, and how it can help you understand the problem domain better without writing a single line of code. I hope to continue exploring this subject in future articles.
If you made it all the way to the bottom of this article, we probably want to hire you. Check out our jobs page.