Extreme Programming Adventures in C# ISBN:0735619492 by Ron Jeffries Microsoft Press © 2004 The author of this book demonstrates that you can write well-designed, resilient code incrementally and safely, while minimizing your investment in speculative up-front design Table of Contents Microsoft Extreme Programming Adventures in C# Foreword Introduction Extreme Programming Chapter 1 - XML Notepad Stories Chapter 2 - Testing with NUnit Chapter 3 - A Pressing Matter Chapter 4 - Extracting the Model Chapter 5 - Expressing Ideas Chapter 6 - The First Customer Acceptance Test Chapter 7 - Warts and All Chapter 8 - The InputCommand Object Chapter 9 - Story One Is Done! Chapter 10 - Find a Bug, Write a Test Chapter 11 - Adding the Section Tag, Part I Chapter 12 - Adding the Section Tag, Part II Chapter 13 - Discovering the Menu Chapter 14 - Don’t Try This at Home Chapter 15 - Climbing Out of the Rat Hole Chapter 16 - Adding the Tag Chapter 17 - How Do We Do a Test That We Can’t Do? Chapter 18 - A Simple Feature Teaches Some Lessons Chapter 19 - Dark Clouds on the Horizon Chapter 20 - Finishing the Shift+Enter Story Chapter 21 - Some Things We Ought to Do Chapter 22 - The Delegate from Troy Chapter 23 - Planning Interlude Chapter 24 - File Save and Load Chapter 25 - Tagging Along Chapter 26 - Menu Arrays Chapter 27 - Displaying the Article Chapter 28 - Oh No! Undo! Chapter 29 - The Final Optimized Undo Chapter 30 - The Long Dark Teatime of the Soul Chapter 31 - Project Retrospective Bibliography Index List of Lessons Back Cover See eXtreme Programming (XP) in action at the hands of an XP mater—and learn Microsoft NET and C# programming in the process! In this fast-paced, handson exposition, Ron Jeffries—one of the leading voices and practitioner in the XP community—demonstrates that you can write well-designed, resilient code incrementally and safely, while minimizing your investment in speculative up-front design As Jeffries builds his sample application, you get firsthand insights into what successful XP development looks like, complete with real-world challenges such as the eleventh-hour change order For further practice and study, you can download all the author’s code— including missteps—so you can see XP and agile concepts in action and assess how they fit into your own work Pair program with an XP master, discovering how to: Streamline and simplify the software development process Work more effectively as part of an XP development team Reduce missteps by designing, testing, and refining code in increments Receive clearer specifications and feedback from customers Write cleaner, more expressive code—and weed out more bugs Conserve resources by planning and reassessing progress as you go Maintain a sustainable work pace—and avoid burnout Step up delivery dates, shipping the most crucial features first Improve customer satisfaction! About the Author Ron Jeffries was the onsite coach for the original eXtreme Programming software development project He also helped plan and teach the first XP immersion course An independent consultant and veteran systems developer, he’s been a leader in the XP movement for more than eight years, speaking at developer events, writing articles, and editing the Xprogramming.com Web site Microsoft Extreme Programming Adventures in C# Ron Jeffries PUBLISHED BY Microsoft Press A Division of Microsoft Corporation One Microsoft Way Redmond, Washington 98052-6399 Copyright © 2004 by Ronald E Jeffries All rights reserved No part of the contents of this book may be reproduced or transmitted in any form or by any means without the written permission of the publisher Library of Congress Cataloging-in-Publication Data Jeffries, Ron, 1939Extreme Programming Adventures in C# / Ron Jeffries p cm Includes index ISBN 0-7356-1949-2 1 Computer software Development eXtreme programming C# (Computer program language.) I Title QA76.76D47J437 2004 005.1'1 dc22 2003068645 Printed and bound in the United States of America 1 2 3 4 5 6 7 8 9 QWE 8 7 6 5 4 3 Distributed in Canada by H.B Fenn and Company Ltd A CIP catalogue record for this book is available from the British Library Microsoft Press books are available through booksellers and distributors worldwide For further information about international editions, contact your local Microsoft Corporation office or contact Microsoft Press International directly at fax (425) 936-7329 Visit our Web site at www.microsoft.com/mspress Send comments to mspinput@microsoft.com IntelliSense, Microsoft, Microsoft Press, Visual Basic, Visual Studio, and Windows are either registered trademarks or trademarks of Microsoft Corporation in the United States and/or other countries Other product and company names mentioned herein may be the trademarks of their respective owners The example companies, organizations, products, domain names, e-mail addresses, logos, people, places, and events depicted herein are fictitious No association with any real company, organization, product, domain name, e-mail address, logo, person, place, or event is intended or should be inferred This book expresses the author’s views and opinions The information contained in this book is provided without any express, statutory, or implied warranties Neither the authors, Microsoft Corporation, nor its resellers or distributors will be held liable for any damages caused or alleged to be caused either directly or indirectly by this book Acquisitions Editors: Linda Engelman and Robin Van Steenburgh Developmental and Project Editor: Devon Musgrave Indexer: Julie Kawabata Interior Artist: Joel Panchot Desktop Publisher: Carl Diltz Body Part No X10-25681 To Kent—without whom none of this would have happened To Ricia— without whom none of this would have mattered Ron Jeffries Ron Jeffries has been writing software longer than most people in the profes sion have been alive In spite of academic degrees and help from some of the best software developers in the world, he has learned more from his mistakes, through having made so many of them Ron’s teams have shipped over half a billion dollars worth of commercial software, and he wonders why he didn’t get more of the money He’s happy, though, that he has had so much fun Foreword “Sir! Torpedo Lock! God, there are dozens of them!” The tactical officer’s eyes stared wide at his display This couldn’t be happening “Where away, Ensign Farber?” Captain Jeffries’ dark penetrating eyes flashed urgency at the young officer He should have announced torpedo location and trajectory without having to be told Seconds counted “Thirty by minus 50, range 1-6-0 thousand, 10 thousand kps, sir.” The radial coordinates and incoming velocity vector snapped out with trained precision “A Ferengi vessel just came out of warp behind the torpedoes! Cripes, it must have been firing as it made the transition!” “Hard over, Helm! Full impulse toward that bogey.” “Aye, aye, sir! Damn it!” Commander Langr clearly didn’t like that order but immediately turned the ship into the path of the incoming torpedoes and flooded the engines with power The bridge crew grabbed hold of their consoles as the inertial dampers fought to compensate for the horrific shift in g-forces First Officer Ch’t-Hen locked eyes with the captain and calmly stated, “Sir, heading into the torpedoes gives us approximately 7.23 seconds to impact.” “Full power, Commander Langr.” Jeffries’ eyes never wavered from Ch’tHen’s Ch’t-Hen continued unabated “I count 42 torpedoes with 12 warheads apiece, each with a yield of 200 megatons The 100.8 gigaton detonation will completely overwhelm our shields Two seconds One ” The crew held their breath for an infinitely long second “Sir,” Ch’t-Hen continued without breaking rhythm, “it would appear that the torpedoes have failed to detonate.” Jeffries nodded “Ferengi are too cowardly to arm their torpedoes until they are a safe distance from their ship I think we managed to squeak inside that radius Helm, stay on your course! I want you to miss that Ferengi by inches, Langr, inches! Lieutenant Hill, target our rear phasers on their warp nacelles Wait until we are two klicks past them, then.” OK, now that I have your attention, I want you to know that you have chosen the right book I presume you picked it up because you wanted to learn something about C# and agile methods If so, don’t put it down now This book will take you on an adventure that is every programmer’s dream By reading this book you’ll learn side-by-side with Ron Jeffries, one of the most talented master programmers in our galaxy You won’t just learn facts about C#—you’ll learn a master’s principles, patterns, and practices In this book, Ron pair programs with you As you read it, you will feel that you are sitting next to him, watching him—even helping him—to write C# code You’ll read his thoughts, his fears, his complaints, and his rejoicings You’ll laugh with him, and you’ll get mad at him You’ll participate in his mistakes and successes You’ll agree and disagree with him You’ll argue with him, and some of those arguments you’ll win Reading this book is as close to having Ron as a one-on-one mentor as it’s possible for print to allow Are you new to C#? If so, keep a language reference nearby Ron will teach you a lot about C#, but he’s not going to spoon-feed you This is not a C# tutorial There are many things you are going to have to look up for yourself However, this book will give you an order in which to look those things up Each chapter will lead you to more and more interesting concepts in C# and to a deeper and deeper understanding of agile methods Treat this book as a pathway instead of as a reference Do you already know C#, or Java, or C++? If so, read this book for the skills, the attitudes, the practices, and the thrill of discovery You’ll learn along with Ron as he explores C# and NET You’ll gain deep insights into the thought processes, personal values, and subtle gestures of a master Ron Jeffries started his programming adventures in 1961 (before the Beatles appeared on Ed Sullivan!) at the age of 21 Through an accident of fortune, he found himself working summers at Strategic Air Command (in a hardened underground installation) and was able to play with their computer systems He learned programming from some of the brightest folks who had been charged with our security, and he could apply that learning with some of the best and most powerful tools in existence From that auspicious start, Ron went on to learn more about software than there was to know at the time He wrote compilers, databases, tax systems, payroll systems, obscure mathematical processors, business systems, multi-programming operating systems, etc, etc He’s worked in APL, Basic, C, C++, C#, Commercial Translator, Delphi, Fortran, IPL- V, LISP, Logo, Pascal, Prolog, Smalltalk, Snobol, IBM 704, 7090, 1401, 1620, and 360 series, the DEC PDP-1 and -7, the SDS/Xerox 940, Sigma 7 and Sigma 9, the 6502, 8080.… In short, he’s been around Ron has been fortunate (or devious) enough to work with some of the best in our industry He’s worked with Ward Cunningham (yes, by reading this book you’ll have a Ward number of at most 2), Kent Beck, Ken Auer, Bill Wake, Chet Hendrickson, Ann Anderson, Michael Feathers, Jeff Langr, Michael Hill, Robert Koss, and even—er—me Ron is an outspoken XPer and a founding signatory to the Manifesto of the Agile Alliance Ron loves to write code If he has a spare minute, he wants to spend it coding He’ll go to the Michigan Union to meet a friend, and the two of them will crack their laptops and write code together More importantly, Ron loves to write good code He’s not happy just making the code work Ron wants to make it right And making code right is something that Ron Jeffries knows better than anyone else Ron takes a rare pride in the code he produces and in the way he produces it He craves the creation of magnificence I first met Ron in 1999 Ron had been doing Extreme Programming (XP) for nearly five years by then He was the first XP coach ever and had helped many companies improve their practices Ron drove to my house (in his hatchback Z3) to meet with me to help plan the first XP Immersion course I wanted Ron to be one of the instructors This is a decision I have never regretted Ron is an outstanding instructor who is deeply motivated to share his vast wealth of knowledge Ron understands software with a rare clarity and has the even rarer ability to share that clarity with others If you want to learn how to use C# well, if you want your code to be right, if you want to learn the skills, disciplines, and attitudes of a true master, then you’ve done well to pick up this book Keep reading—you’re in for a wild adventure You’ll boldly go where you’ve never gone before Robert C Martin December, 2003 List of Lessons Chapter 1: XML Notepad Stories Lesson: What Have We Learned So Far? Chapter 3: A Pressing Matter Lesson: What Have We Learned? Chapter 5: Expressing Ideas Lesson: Digression—Doing It Right Lesson: The Key Lesson Chapter 6: The First Customer Acceptance Test Lesson: Remarks on Technique Lesson: What We Learned Chapter 7: Warts and All Lesson: Lessons Relearned Today Chapter 8: The InputCommand Object Lesson: What We Learned Chapter 9: Story One Is Done! Lesson: Retrospectives Lessons Chapter 15: Climbing Out of the Rat Hole Lesson: My Point, and I Do Have One Chapter 17: How Do We Do a Test That We Can’t Do? Lesson: Let’s Sum Up Chapter 25: Tagging Along Lesson: A Little Reflection Lesson: Copy and Paste? Lesson: How’s It Going So Far? Lesson: Incremental Design and Development Lesson: Open Questions Chapter 26: Menu Arrays Lesson: Quick Reflection Lesson: Where Are We Now? Chapter 27: Displaying the Article Lesson: Is There a Lesson Here? Chapter 28: Oh No! Undo! Lesson: Let’s Reflect Lesson: Let’s Reflect Chapter 30: The Long Dark Teatime of the Soul Lesson: First-Take Excerpt—Dragging On and On Lesson: First-Take Excerpt—The Value of Reflection ... He wrote compilers, databases, tax systems, payroll systems, obscure mathematical processors, business systems, multi -programming operating systems, etc, etc He’s worked in APL, Basic, C, C+ +, C# , Commercial Translator, Delphi, Fortran, IPL- V,... Reduce missteps by designing, testing, and refining code in increments Receive clearer specifications and feedback from customers Write cleaner, more expressive code—and weed out more bugs Conserve resources by planning and reassessing... your local Microsoft Corporation office or contact Microsoft Press International directly at fax (425) 936-7329 Visit our Web site at www.microsoft.com/mspress Send comments to mspinput@microsoft.com IntelliSense, Microsoft, Microsoft Press, Visual Basic, Visual Studio, and