In his paper written inKevlin Henney presents the NullObject Pattern to deal with objects that potentially can be null and that should, if they are null, behave in a default manner this without requiring an explicit null check. Below I give a quick summary of the idea for details you should check his paper. A prototypic example is a logging class that is used for example in a service. You want this to work even if no logging was configured without having to write things like:
These names alone tell us nothing about how a Stack should behave. If a developer knows that a test name must start with should often because they have seen this pattern within existing tests it is hard for them to revert to a different style and write a test that is not a proposition.
The verbosity of should pays for itself by forcing developers to think about tests in the right fashion. Kevlin Henney compares shoulds to training wheels on a bike - a device to help while we are learning.
So when should we take the training wheels off? This depends on the makeup of the team and how often the team changes. If the majority of people who are likely to work on the codebase over its lifetime are accustomed to writing tests in this style then the added verbosity is not worth it.
If a sufficiently large proportion are not then it is probably best for the team to stick with the convention. Use the Example Style When Specification Style Does Not Work Occasionally, it is not possible to follow the specification naming style because the descriptions become too long and unwieldy.
If it feels like your method names are becoming overly long ask yourself two questions: Am I really testing only one thing? Is my unit doing too much? In example style the name describes only the "When" part.
It does not describe the expected behavior, e. For this reason, this specification style should be preferred when possible. On a practical level this avoids the extra overhead of updating test names if method names are ever refactored. More subtly, including names can make you think in the wrong fashion - verifying method implementation rather than specifying unit behavior.
The domain language may also overlap with the method names, so you may find yourself using the same words as are also used as a method name.Apr 20, · How to Write a Method Statement Method statements are important documents that outline the way a process or task will be carried out.
This statement (also referred to as a "safe system of work") will detail potential hazards and the steps that will be taken to complete the job safely%().
This blogpost is based on this amazing talk by Kevlin Henney. Dedicating a whole blogpost to curly brackets might seem like overkill but I believe it's worth thinking about them. Not just because of one curly bracket, but because there's a bigger message in all this.
Software development is a knowledge-based process, and testing is no exception to this. From writing a test to running it and understanding its outcome, there is always something to be learnt.
Name Tests With A Specification Style. Name Test Cases With a Specification Style. i.e.
a statement that could be true or false. The method name should start with should. This is superfluous once you get good at writing test names, but in a mixed team it is useful as it encourages thinking about the test in the right way.
Kevlin Henney. Kevlin Henney is an independent consultant, trainer and writer based in the UK. His development interests are in patterns, programming, practice and process.
Presented at Agile Singapore (13th November ) These days testing is considered sexy for programmers.
Who'd have thought it? But there is a lot more to effe.