- Tests Pass
- Expresses Intent
- No Duplication
Corey Haines lead numerous developers during code kata’s and witnessed
how they developed code and design. He coached the developers on using
the above principles and observed how the code became simpler, easier
to extend and better to maintain. Corey wrote down his observations in
4 rules of simple design.
The kata used is Conway’s Game of Life. Game of life is a cellular
automaton that describes how a grid of cells evolves over time by
providing local rules how a cell changes. Although the rules are
easily understood, Life allows for a very rich set of behaviours.
The example code that illustrate various points of the rules and how
they apply to design are written in Ruby. It is chosen because it has
a readable syntax and does not need a lot of boilerplate. No Ruby
specific language features are used, so the ideas are readily
transferable to other languages.
The main part of the book is spend on discussing code examples and how
to improve them using the four rules. These chapters are short and
illustrate fine points of the rules. For example in the chapter Test
Names Should Influence Object’s API the following snippet is
def test_a_new_word_is_empty world = World.new assert_equal 0, world.living_cells.count end
In this example the name of the test method is far removed from the
actual implementation and therefore does not express
intent. Furthermore it reaches deep into the bowls of an object,
effectively duplicating information of the internals of the
object. Corey provides an alternative that does not violate
def test_a_new_world_is_empty world = World.new assert_true world.empty? end
The book is a very nice read and is full of practical advice on how to
improve your design. With 88 pages it can be read in an evening but
the lessons it teaches you make it a book that you easily come back to
to reread an explanation. I recommended wholeheartedly to every
software developer that takes their work seriously.