CHANNELS
 
 
 
 
 
 
 
 
ON THE WEB
 
 
 
 
PRINT EDITION
 
 
 
 
BZ MEDIA
 
 
 
 
ADVERTISER LINKS
 
 
 
 
 
 
 
AS OF 11/19/2008 7:03AM EST
BDD Coming to a tool near you
Stories Columns Opinions Resources

By Andrew Binstock

July 15, 2008 — 

Behavior-Driven Development (BDD) sounds like either a modern theory of raising children or yet another way to mess with how programmers write code. It is the latter, but fortunately, unlike other xDD methodologies, it suggests only small changes to the way you currently do things. In its simplest form, BDD specifies the behavior of code and tries to have the code be expressive of that behavior, rather than maintaining a disconnected relationship between specification and implementation.

Let me illustrate. Behaviors are frequently expressed with keywords, such as: given, when, then and should. So, for example: given the 2008 tax tables, when a head of family has taxable income of less than US$12,000, then the tax should be $0. At a lower level that is closer to code: given a new linked list, then the list should be empty. And: given an empty linked list, when we add an item, then the list should not be empty.

At first blush, this might seem like another way of writing an if/then statement, save with a level of verbosity that exceeds COBOL. True, but that misses the point: First, you can define your scenarios, then insert the code into the scenarios that make it happen. Tools, such as easyb (which uses the JVM scripting language Groovy), enable code to be inserted into the scripts resulting in a very readable explanation of what is being done. Here is an example from the easyb site that illustrates the process.

Starting with the requirements articulated with BDD-style language: Given that a user mistypes a ZIP code, and given the ZIP code-validation service is up and running, when validation is performed, then the service should indicate the ZIP code is invalid.

In easyb, the implementation is woven into the constraints, and the result is:

      given "an invalid zip code", {
        invalidzipcode = "221x1"
      }

      and "given the zipcodevalidator is initialized", {
        zipvalidate = new ZipCodeValidator()
      }

      when "validate is invoked with the invalid zip code", {
        value = zipvalidate.validate(invalidzipcode)
      }

      then "the validator instance should return false", {
        value.shouldBe false
      }

Notice that the code implementation is specified between the braces. Easyb knows how to sequence that code correctly so that this works as expected. (For pure Java developers, Concordion does something similar.)

At this point, you might still be wondering what the advantage would be of the extra keystrokes. They are in fact manifold.

Perhaps most important is the ability to have real communication with the user about the code. For example, you can sit down with the user and capture all the various requirements in a series of scenarios. Then, as you place the needed method calls into the requirements, you have a good sense that your code is indeed matching the stipulated requirements. Moreover, the user can also see that the requirements have been implemented, as each one has matching method calls. This last point is important because it gives the user or manager a way to look at code in a reader-friendly fashion.

The benefits of BDD are not limited to user-developer communications. They also show up in a big way in unit testing. The previous example could easily be used to specify a unit test. In fact, unit-testing frameworks have begun adding BDD-like syntactical sugar.

For example, version 4.4 of JUnit contains the Hamcrest extensions, which confer far greater readability on JUnit tests. Those extensions were originally a popular part of JMock, the mock object library. (To use Hamcrest, download JUnit 4.4 and statically import org.hamcrest.CoreMatchers.* and org.junit.matchers.JUnitMatchers.* into your test classes.)

Then you can write far more readable unit tests. For example, you can test that x is(notANumber()) or that a collection hasItems(x,y,z) or that it hasKey(x). Not only does this simplify having to look up methods of various collections, but it also makes the intent of the test clearer.

For myself, this is a boon. Occasionally, I have made a change that makes a long-ago test fail. When I go back to the code, I struggle to recall exactly what I am doing in the test, and I have to consult the code I’m testing. In part, this is because tests don’t provide nearly the opportunity for pellucid coding that standard code does. The Hamcrest extensions make it easier to see at a glance what I was trying to do.

The extent to which BDD enters into the mainstream depends on the willingness of developers to write code more to obtain clarity. For my money, improved user communication and increased test legibility are well worth it.

Andrew Binstock is the principal analyst at Pacific Data Works. Read his blog at binstock.blogspot.com.



Related Search Term(s): Software development


Share this link: http://sdtimes.com/link/32488
 


 
 
 
 
 
 
 
 
 
 
SUBSCRIBE TODAY!
 E-Newsletters:
  News on Mon/Thurs.  More info
  Test & QA Report  More info
  EclipseNews  
  SPTech Report  More info
 
 
 
PDF & PRINT EDITION
* Requires Resource Account!  LOGIN or SIGN UP

Download Current Issue!
ISSUE 11/15/2008 PDF

Need Back Issues?
DOWNLOAD HERE

Receive The Print Edition?
SUBSCRIBE HERE
 
REGISTER
 
GET NOTIFIED!
About all of the latest Resources
 
 
SD TIMES 100
It's time once again to
recognize the organizations
or individuals that have
demonstrated leadership in
their markets.