$ cat "

The Importance of Nomenclature

"

Naming is a crucial part of programming, and is probably one of the most challenging parts of the craft. The Behaviour Driven Development movement currently does its best to bring attention to the fact that terminology and naming not only effects the way we read and understand code, it also effects the way approach problems and thereby also the solutions that we come up with.

An example of how terminology influences the code you write is the naming of the attribute which specifies that a method is a test method. NUnit uses the traditional jUnit style by using the [Test] attribute. However, xUnit.NET has chosen to call their attribute [Fact]. This simple change is quite effective in pointing you in the right direction when you sit down to write your unit test.

The [Test] attribute is quite weak since it does not say much, except that the method is to be executed by the test runner. For example, a common style of writing unit test is to do something like this:

[Test]
public void TestPush()
{
// Create a stack instance
// Do some stuff with it
// Write a bunch of asserts
}

The [Fact] attribute on the other hand sets quite a firm expectation on the test method. If you change the [Test] attribute above to a [Fact] attribute it makes no sense. TestPop is certainly not a fact. You are pushed to write tests with names that actually state some fact about the code under test. To be able to write a fact about the code you are testing you also have to chose a more narrow scope, probably with a single logical assert.

[Fact]
public void GivenEmptyStack_WhenPushing_ThenSizeIsOne()
{
Stack stack = new Stack();
stack.Push(0);
Assert.Equal(1, stack.Size);
}
Written by Erik Öjebo 2010-03-29 20:33

    Comments