Practical Object-Oriented Design in Ruby
An Agile Primer
Sandi Metz
Upper Saddle River, NJ Boston Indianapolis San Francisco
New York Toronto Montreal London Munich Paris Madrid
Capetown Sydney Tokyo Singapore Mexico City
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and the publisher was aware of a trademark claim, the designations have been printed with initial capital letters or in all capitals.
The author and publisher have taken care in the preparation of this book, but make no expressed or implied warranty of any kind and assume no responsibility for errors or omissions. No liability is assumed for incidental or consequential damages in connection with or arising out of the use of the information or programs contained herein.
The publisher offers excellent discounts on this book when ordered in quantity for bulk purchases or special sales, which may include electronic versions and/or custom covers and content particular to your business, training goals, marketing focus, and branding interests. For more information, please contact:
U.S. Corporate and Government Sales
(800) 382-3419
For sales outside the United States, please contact:
International Sales
Visit us on the Web: informit.com/aw
Cataloging-in-publication data is on file with the Library of Congress.
Copyright 2013 Pearson Education, Inc.
All rights reserved. Printed in the United States of America. This publication is protected by copyright, and permission must be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. To obtain permission to use material from this work, please submit a written request to Pearson Education, Inc., Permissions Department, One Lake Street, Upper Saddle River, New Jersey 07458, or you may fax your request to (201) 236-3290.
ISBN-13: 978-0-321-72133-4
ISBN-10: 0-321-72133-0
Text printed in the United States at RR Donnelley in Crawfordsville, Indiana.
First printing, September 2012
Editor-in-Chief
Mark Taub
Acquisitions Editor
Debra Williams Cauley
Development Editor
Michael Thurston
Managing Editor
John Fuller
Project Editor
Elizabeth Ryan
Packager
Laserwords
Copy Editor
Phyllis Crittenden
Indexer
Constance A. Angelo
Proofreader
Gina Delaney
Publishing Coordinator
Kim Boedigheimer
Cover Designer
Chuti Prasertsith
Compositor
Laserwords
Praise for Practical Object-Oriented Design in Ruby
This is great stuff! Your descriptions are so vibrant and vivid that Im rediscovering the truth buried in OO principles that are otherwise so internalized that I forget to explore them. Your thoughts on design and knowing the future are especially eloquent.
Ian McFarland, President, New Context, Inc.
As a self-taught programmer, this was an extremely helpful dive into some OOP concepts that I could definitely stand to become better acquainted with! And, Im not alone: theres a sign posted at work that reads, WWSMD? What Would Sandi Metz Do?
Jonathan Mukai, Pivotal in NYC
Meticulously pragmatic and exquisitely articulate, Practical Object Oriented Design in Ruby makes otherwise elusive knowledge available to an audience which desperately needs it. The prescriptions are appropriate both as rules for novices and as guidelines for experienced professionals.
Katrina Owen, developer, Bengler
I do believe this will be the most important Ruby book of 2012. Not only is the book 100% on-point, Sandi has an easy writing style with lots of great analogies that drive every point home.
Avdi Grimm, Author of Exceptional Ruby and Objects on Rails
For Amy, who read everything first
Contents
Foreword
One of the core truisms of software development is that as your code grows and requirements for the system that you are building change, additional logic will be added that is not yet present in the current system. In almost all cases, maintainability over the life of the code is more important than optimizing its present state.
The promise of using object-oriented (OO) design is that your code will be easier to maintain and evolve than otherwise. If you are new to programming, how do you unlock these secrets to maintainability using OO? The fact is that many of us have never had holistic training in writing clean object-oriented code, instead picking up our techniques through osmosis from colleagues and a myriad of older books and online sources. Or if we were given a primer in OO during school, it was done in languages such as Java or C++. (The lucky ones were taught using Smalltalk!)
Sandi Metzs Practical Object-Oriented Design in Ruby covers all of the basics of OO using the Ruby language, meaning that its ready to usher countless Ruby and Rails newcomers to the next steps in their professional development as mature programmers.
Ruby itself, like Smalltalk, is a completely object-oriented (OO) language. Everything in it, even primitive data constructs such as strings and numbers, is represented by objects with behavior. When you write your own applications in Ruby, you do so by coding your own objects, each encapsulating some state and defining its own behavior. If you dont already have OO experience, it can feel daunting to know how to start the process. This book guides you every step of the way, from the most basic questions of what to put in a class, through basic concepts such as the Single Responsibility Principle, all the way through to making tradeoffs between inheritance and composition, and figuring out how to test objects in isolation.
The best part, though, is Sandis voice. Shes got a ton of experience and is one of the nicest members of the community youll ever meet, and I think she did a great job getting that feeling across in her writing. Ive known Sandi for several years now, and I wondered if her manuscript would live up to the pleasure of actually getting to know Sandi in real life. Im glad to say that it does, in spades, which is why Im glad to welcome her as our newest author to the Professional Ruby Series.
Obie Fernandez, Series Editor
Addison Wesley Professional Ruby Series
Introduction
We want to do our best work, and we want the work we do to have meaning. And, all else being equal, we prefer to enjoy ourselves along the way.
Those of us whose work is to write software are incredibly lucky. Building software is a guiltless pleasure because we get to use our creative energy to get things done. We have arranged our lives to have it both ways; we can enjoy the pure act of writing code in sure knowledge that the code we write has use. We produce things that matter. We are modern craftspeople, building structures that make up present-day reality, and no less than bricklayers or bridge builders, we take justifiable pride in our accomplishments.
This all programmers share, from the most enthusiastic newbie to the apparently jaded elder, whether working at the lightest weight Internet startup or the most staid, long-entrenched enterprise. We want to do our best work. We want our work to have meaning. We want to have fun along the way.