Ylan Segal

Book Review: 99 Bottles of OOP

99 Bottles of OOP bills itself as a practical guide to writing cost-effective, maintainable and pleasing object-oriented code – otherwise known as “good” code. It delivers on that promise.

Sandi Metz (often mentioned in this blog) and Katrina Owen (also previously mentioned) team up to write this highly instructive guide. They use a simple coding exercise – print out the lyrics to the 99 bottles of beer song – to illustrate several different implementations and provide some critiques of them.

They go much father, though. By introducing a new requirement, they embark on a fascinating and painstaking refactoring process. By changing one – and only one – line at the time, they use their test suite as a safety net to discover abstraction hidding in the code. Slowly, they improve the code bit by bit, following Kent Beck’s advice:

for each desired change, make the change easy (warning: this may be hard), then make the easy change

Often, refactoring and coming up with abstractions seems esoteric. Experienced engineers know how to do it, but often can’t explain their process to more junior engineers. Worse, it’s hard to express why one design is better than another. Sandi and Katrina provide some relief from the paralysis that can result from starting at a piece of code without knowing which is the correct abstraction that it needs. The prescribe to follow the Flocking Rules:

  1. Select the things that are more alike.
  2. Find the smallest difference between them.
  3. Make the simplest changes that will remove the difference.

By example, the authors apply these rules to extract one abstraction after another. Naming is still hard, but is easier once the extraction has been made. Along they way they should how to judge code with facts (like cyclomatic complexity and the ABC metric), as opposed of just on opinion.

Overall, I learned a lot from this book. The examples are written in Ruby, but the syntax is so simple, that it can be easily understood by anyone already familiar with another programming language.