Emergent Design The Evolutionary Nature of Professional Software Development Scott L. Bain Upper Saddle River, NJ • Boston • Indianapolis • San Francisco New York • Toronto • Montreal • London • Munich • Paris • Madrid Capetown • Sydney • Tokyo • Singapore • Mexico City Illustrations by Andrea Chartier Bain 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 designa- tions 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 war- ranty of any kind and assume no responsibility for errors or omissions. No liability is assumed for incidental or con- sequential 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 corpsales@pearsontechgroup.com For sales outside the United States please contact: International Sales international@pearsoned.com V isit us on the Web: informit.com/aw Library of Congress Cataloging-in-Publication Data Bain, Scott L. Emergent design : the evolutionary nature of professional software development / Scott L. Bain. p. cm. Includes index. ISBN 978-0-321-50936-9 (hardcover : alk. paper) 1. Computer software—Development. 2. Computer software—Development—Vocational guidance. I. Title. QA76.76.D47B345 2008 005.1—dc22 2007050637 Copyright © 2008 Pearson Education, Inc. All rights reserved. Printed in the United States of America. This publication is protected by copyright, and per- mission 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. For information regarding permissions, write to: Pearson Education, Inc Rights and Contracts Department 501 Boylston Street, Suite 900 Boston, MA 02116 Fax: (617) 671-3447 ISBN-13: 978-0-321-50936-9 ISBN-10: 0-321-50936-6 Text printed in the United States on recycled paper at Courier in Westford, Massachusetts. First printing, March 2008 To Alan Shalloway and all instructors and staff at Net Objectives, whose collegial support and innovative ideas have contributed endlessly to my development as a technologist and educator. And . . . To Andrea and Griffin, who have enriched my entire life, and for whom I do pretty much everything I do. This page intentionally left blank Contents Series Foreword ___________________________________________________________xvii Preface _____________________________________________________________________xxiii Acknowledgments ________________________________________________________xxix About the Author__________________________________________________________xxxi Chapter 1 Software as a Profession___________________________________1 How Long Have Human Beings Been Making Software?________________1 What Sort of Activity Is Software Development? ______________________2 What Is Missing? __________________________________________________6 Who Is Responsible? _______________________________________________8 Uniqueness _______________________________________________________9 Chapter 2 Out of the Closet, Off to the Moon _______________________11 Patterns and Professionalism in Software Development________________11 Andrea’s Closet __________________________________________________12 Off to the Moon __________________________________________________18 Forces Lead to Forces ________________________________________22 Different Forces, Different Design ______________________________22 And There Are More Contextual Forces ________________________23 The Costs and Benefits _______________________________________24 On to Mars _________________________________________________26 vii The Value of Patterns _____________________________________________26 Summary________________________________________________________27 Chapter 3 The Nature of Software Development __________________29 We Fail Too Much ________________________________________________30 Definitions of Success _____________________________________________31 The Standish Group ______________________________________________32 Doing the Wrong Things __________________________________________34 Doing the Things Wrong __________________________________________35 Time Goes By, Things Improve _____________________________________38 One Reason: The Civil Engineering Analogy _________________________38 Giving Up Hope __________________________________________________41 Ignoring Your Mother_____________________________________________42 Bridges Are Hard, Software Is Soft __________________________________43 We Swim in an Ocean of Change ___________________________________43 Accept Change ___________________________________________________44 Embrace Change _________________________________________________45 Capitalize on Change _____________________________________________46 A Better Analogy: Evolving Systems ________________________________49 Summary________________________________________________________52 Chapter 4 Evolution in Code: Stage 1 ________________________________55 Procedural Logic Replaced with Object Structure _____________________56 The Origins of Object Orientations and Patterns ______________________56 An Example: Simple Conditionals and the Proxy Pattern ______________58 The Next Step: Either This or That __________________________________62 Why Bother? ____________________________________________________65 One Among Many________________________________________________66 Summary________________________________________________________67 viii Contents Chapter 5 Using and Discovering Patterns _________________________69 Design from Context: More Carpentry from Scott_____________________70 Patterns Lead to Another Cognitive Perspective ______________________79 Patterns Help Give Us a Language for Discussing Design _______________79 Patterns in This Book _____________________________________________80 Summary________________________________________________________81 Chapter 6 Building a Pyramid _________________________________________83 Elements of the Profession_________________________________________83 A Visual Representation ___________________________________________85 Summary________________________________________________________86 Chapter 7 Paying Attention to Qualities and Pathologies _________89 Encapsulation____________________________________________________91 Cohesion ________________________________________________________91 Method Cohesion____________________________________________92 Cohesion of Perspective Level _________________________________94 Class Cohesion ______________________________________________96 How Cohesive Is Cohesive Enough? ___________________________98 Coupling ________________________________________________________99 Intentional Coupling Versus Accidental Coupling ________________99 Types of Coupling __________________________________________101 Redundancy ____________________________________________________106 Redundancy and Coupling___________________________________110 Testability ______________________________________________________112 Readability _____________________________________________________114 Pathologies _____________________________________________________114 Indicators of Weak Cohesion _________________________________115 Indicators of Accidental or Illogical Coupling ___________________116 Indicators of Redundancy____________________________________118 Summary ______________________________________________________119 Contents ix Chapter 8 Paying Attention to Principles and Wisdom ___________121 Separating Use from Creation _____________________________________122 Fowler’s Perspectives________________________________________122 Another Kind of Perspective _________________________________123 The Perspective of Use ______________________________________125 A Separate Perspective: Creation _____________________________125 Considering Construction Details Last _________________________127 The Real World_____________________________________________129 The Open-Closed Principle _______________________________________129 Open-Closed at the Class Level _______________________________131 Open-Closed at the Method Level ____________________________132 The Dependency Inversion Principle _______________________________133 Advice from the Gang of Four_____________________________________135 Designing to the Interface of a Method ________________________136 Designing to the Interface of a Class___________________________138 GoF: Favor Object Aggregation Over Class Inheritance __________139 GoF: Consider What Should Be Variable in Your Design and Encapsulate the Concept That Varies ___________________________143 Summary ______________________________________________________146 Chapter 9 Paying Attention to Practices ___________________________147 Consistent Coding Style __________________________________________148 Comments_________________________________________________149 Naming Classes, Methods, and Variables _______________________151 Virtues of Coding Standards__________________________________152 Programming by Intention _______________________________________153 Are They Really All Private? _________________________________154 Encapsulating the Constructor ____________________________________155 Principles Versus Practices ___________________________________159 Making the Decision ________________________________________159 Commonality-Variability Analysis _________________________________161 Practices and Freedom ___________________________________________166 Summary ______________________________________________________167 x Contents Chapter 10 Paying Attention to Disciplines: Unit Testing__________169 Economies of Testing ____________________________________________169 Unit Testing________________________________________________171 Up-Front Testing ___________________________________________173 JUnit Framework________________________________________________175 JUnit Basics________________________________________________176 JUnit Examples ____________________________________________178 Rule.java: Code First, Then Test ______________________________179 RuleContainer.java: Test First, Then Code______________________187 Eliminating Redundancy: @Before and @After _________________196 Automating Tests in Batches _________________________________199 Exceptions and Unit Testing__________________________________200 Mock Objects ___________________________________________________204 MockObject Frameworks ____________________________________206 Faking It __________________________________________________209 Dependency Injection and the Endo-Testing Technique__________210 Endo-Testing_______________________________________________211 Summary ______________________________________________________212 Chapter 11 Paying Attention to Disciplines: Refactoring __________213 Refactoring Bad Code ____________________________________________215 Refactoring Good Code___________________________________________216 Structural Changes Versus Functional Changes______________________218 Refactoring Helps You Choose Your Battles _________________________219 Patterns Can Be Targets of Refactoring _____________________________220 Avoiding Refactoring: Prefactoring_________________________________220 The Mechanics of Refactoring _____________________________________221 Refactoring Legacy Code _________________________________________231 Summary ______________________________________________________233 Contents xi Chapter 12 Test-Driven Development _______________________________235 What Makes Development Test-Driven?____________________________235 Test-Driven Versus Test-First _________________________________236 Designing from the Perspective of the Unit Test_________________237 Testing and Quality ______________________________________________238 Testing and Cohesion _______________________________________238 Testing and Coupling________________________________________240 Testing and Redundancy_____________________________________240 Test-Driven Development and Patterns_____________________________241 The Strategy Pattern ________________________________________241 Turtles All the Way Down ___________________________________242 Mock Object/Mock Turtles ___________________________________243 Mock Objects ___________________________________________________244 Mock Turtles____________________________________________________248 Testing the Decorator Pattern _____________________________________248 Summary ______________________________________________________253 Chapter 13 Patterns and Forces _____________________________________255 Making Decisions in an Evolving Design ___________________________255 Christopher Alexander and Forces_________________________________256 The Signal Processor Example ________________________________257 The PKZip Example _________________________________________262 Testing and Forces __________________________________________265 More Choices, More Forces _______________________________________266 Summary ______________________________________________________271 Chapter 14 Emergent Design: A Case Study _______________________273 The Problem Domain: The MWave Corporation _____________________273 The Teams______________________________________________________275 The Simplest Thing That Could Possibly Work_______________________277 xii Contents [...]... the needs of customers and the strategic goals of the company.” Mary and Tom Poppendieck, in their excellent book, Implementing Lean Software Development: From Concept to Cash (2006), note: It is the product, the activity, the process in which software is embedded that is the real product under development The software development is just a subset of the overall product development process So in a... “optimize the whole”: 1 The whole organization: Integrating enterprise, team, and individuals to best work together 2 The whole product: Not just its development, but also its maintenance and integration 3 The whole of time: Not just now, but in the future We want sustainable ROI from our effort Emergent Design: The Evolutionary Nature of Professional Software Development This particular book addresses the. .. software profession would provide What Sort of Activity Is Software Development? that would be of direct benefit to developers and the quality of the software they produce If you’ll allow me this, consider the following: We have not been creating software for long Most professions and crafts are hundreds or even thousands of years old and have had a long time to work out the fundamentals that underlie them... are licensed by the state to ensure that they are competent, that their skills and training meet a minimal standard Generally, this is because of the extensive damage they can do if they are not competent Of course, this also implies that there is an agreed-upon set of minimal standards that these professionals can be held to, and that they are the right standards Furthermore, because of the rigors and... support these efforts on their own time, because they feel such groups are valuable Therefore, I would say that software development is, by nature, a professional activity Whether or not we have been conducting it as a profession, we should be, in my opinion I’m going to set aside the question of whether we should be regulated by the government, because my focus here is to discover those things that a software. .. real sense, we can call software development a subset of product development And thus, if we want to understand lean software development, we would do well to discover what constitutes excellent product development In other words, software in itself—isn’t important It is the value that it contributes—to the business, to the consumer, to the user—that is important When developing software, we must always... are probably still at the leechand-rattle stage, at least to some degree In other words, I am saying that I think software should be conducted as a professional activity, but that it isn’t yet Not quite 5 6 Chapter 1 • Software as a Profession What Is Missing? Let’s compare software development to what other professions typically have • Specialized language In software development, the language has always... each new batch of doctors, lawyers, and builders, and in each case has led to the formation of an organism we call the profession Professions have their own lives, their own existence For example, the profession of carpentry has been around for thousands of years, though no carpenter is that old Professions provide a sort of safety net for those individuals in their practice The purpose of this book... to the professionals and those they serve Also, there is a clearly defined path to follow if you want to become a doctor, or a lawyer, or a master cabinetmaker When society needs more of them, we know what process to take individuals through to increase their numbers, and we know how to tell young people to prepare for a given professional career So where does software development fit? Software development. .. all concerned There is other work to be done, certainly I do not pretend to have solved the problem by bringing valuable ideas and practices to my fellow developers; but this is my part along the way I believe strongly that software development is on the brink of becoming a profession—in the true sense of the word—and that going that last mile, filling in the missing pieces, is one of the most important . integration. 3. The whole of time: Not just now, but in the future. We want sus- tainable ROI from our effort. Emergent Design: The Evolutionary Nature of Professional Software Development This. L. Emergent design : the evolutionary nature of professional software development / Scott L. Bain. p. cm. Includes index. ISBN 978-0-321-50936-9 (hardcover : alk. paper) 1. Computer software Development. . Emergent Design The Evolutionary Nature of Professional Software Development Scott L. Bain Upper Saddle River, NJ • Boston • Indianapolis