Code Kata

Any seasoned programmer who has tried doing Test-Driven Development (TDD) knows that it is a completely different paradigm of development from traditional methods.  So for most of us, it takes a lot of practice to become proficient at TDD.  This kata provides a simple exercise to practice the 3 basic laws of Test-Driven Development.

Scoring a Game of Bowling

Coming soon.

Start with a High-Level Design

Following is a suggested high-level design for this application.  However, as you apply the laws of TDD, don't get hung up on following this design.  Let this serve as a starting point and provide some basic vocabulary for your application.  It's okay if your code takes you down a separate path from this design.  That often happens with TDD, which is exactly how good architectures evolve.

One Possible Design

Start with a Game class, which has two methods: Roll and Score.  The Roll method take the number of pins knocked down for a given game.  The Score method is called at the end of the game and returns the score of the game.  Next is the Frame class — and a Game has 10 frames.  The Frame class has one function, Score, which returns the score for a given frame.  However, in the case of a spare or strike, the Score function will need to look ahead to the next frame to score the current frame — we therefore have the reference for the frame object back onto itself, indicating that dependency.  Next is the Roll class, and a Frame has 1 or 2 Rolls, except in the tenth frame, where it will have 2 or 3 rolls — as indicated by the sub-type Tenth Frame, which has one additional role (as indicated).  The Roll class has a private attribute of pins which holds the number of pins knocked down on a given roll. 

3 Laws of Test-Driven Development
  1. Write no production code except to pass a failing test.
  2. Write only enough of a test to demonstrate a failure.
  3. Write only enough production code to pass the test.

This is a kata that is borrowed from Bob Martin, who often uses this example to teach the practice of Test-Driven Development.

Related Items

subject Article

Why Bother With TDD?

Does it really make sense to test code that hasn’t even been written yet?  Or to disrupt your development mojo every minute to stop, write tests, and refactor code?  It does if you want to deliver software faster, through better code, with fewer defects, and greater agility.  As software development leaders, it is important to understand that Test Driven Development goes well beyond quality control.

Read More
school Training

Principles & Practices of Test-Driven Development

This course teaches the principles and practices of Test-Driven Development (TDD) and demonstrates how proper software design evolves through application of the eXtreme Programming principle of Test First.  Unit testing principles are introduced, along with a thorough discussion on the benefits of TDD.  An application is developed (from start to finish) during this course to explain step-by-step and demonstrate first-hand how a high quality, testable design evolves by applying the three laws of Test-Driven Development.

Learn More schedule 2 Hours
school Training

Unit Testing and Beyond

This course teaches the principles and practices of unit testing, along with core techniques for writing testable software and avoiding test smells.  Dependency Inversion is taught as an effective method for isolating the system under test, along with test doubles for overriding behavior.  Test-Driven Development (TDD) is also taught and demonstrated to show how proper software design evolves through application of the XP principle of Test First.  An application is developed during this course to demonstrate how a high quality design evolves by applying the three laws of TDD.

Learn More schedule 4 Hours