Book review - Practical OO Design in Ruby
Sun, Jul 14, 2013I just worked my way through Sandi Metz’ “Practical Object Oriented Design in Ruby” and found it to be a very good book with clear examples and points made about a subject that can otherwise be cloudy and complex.
The book generally explains how a Ruby program might follow SOLID design principles, but wisely avoids getting hung up on definitions and theory, and instead teaches by example (and counter-example).
The book touches on all of the SOLID principles, but I felt that (again wisely) it spent more time on the 2 more important principles:
- S - Single responsibility principle.
- D - Dependency inversion principle.
It does this across 9 chapters:
- Object Oriented Design
- Designing Classes with a Single Responsibility
- Managing Dependencies
- Creating Flexible Interfaces
- Reducing Costs with Duck Typing
- Acquiring Behavior Through Inheritance
- Sharing Role Behavior with Modules
- Combining Objects with Composition
- Designing Cost-Effective Tests
While I do favor OO designs, I’m not a big fan of inheritance, so I’m glad it was handled with care and I felt the suggestions to favor shallow hierarchies, or prefer duck typing and composition over inheritance where possible is great advice.
I also appreciated the many practical comments about deferring decisions and waiting for patterns to emerge instead of engineering for a predicted future.
I personally took away a greater appreciation for duck-typing. So many OO design books concentrate on is-a vs has-a relationships, but of equal (or perhaps greater) importance for long-term flexible design is the behaves-like-a relationship.
- Use Inheritance for is-a relationships
- Use Composition for has-a relationships
- Use Duck Types for behaves-like-a relationships
Quotes
This is definitely not an architecture-astronaut book - it gives practical advice with many concrete examples, but also has lots of little nuggets of advice that are ripe for a highlighter pen…
“You do not have to know where you’re going to use good design practices to get there. Good practices reveal design.”
“Once you begin to treat your objects as if they are designed by their behavior rather than their class, you enter into a new realm of expressive flexible design”
“Well designed applications are constructed of reusable code. Small, trustworthy self-contained objects with minimal context, clear interfaces, and injected dependencies are inherently reusable.”
“Creating a hierarchy has costs”
“Refactoring - a precise process that alters code via tiny, crab-like steps and carefully, incrementally, and unerringly transforms one design into another.”
“Tests give you confidence to refactor constantly. Efficient tests prove that altered code continues to behave correctly”
To Summarize
This is a great book for the intermediate ruby developer, its easy to read, clean and concise. At 240 pages it is the perfect size for a quick read - and the color coded example code is always appreciated.
Advanced developers might find it somewhat familiar, but its presented in a way that reinforces those best practices and still a worthwhile read to join alongside other highly recommended ruby books: