(This page has no text content)
(This page has no text content)
97 Things Every Programmer Should Know Collective Wisdom from the Experts Edited by Kevlin Henney Beijing · Cambridge · Farnham · Köln · Sebastopol · Taipei · Tokyo
97 Things Every Programmer Should Know Edited by Kevlin Henney Copyright © 2010 Kevlin Henney. All rights reserved. Printed in the United States of America. Published by O’Reilly Media, Inc. 1005 Gravenstein Highway North, Sebastopol CA 95472 O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://my.safaribooksonline.com). For more information, contact our corporate/institutional sales department: (800) 998-9938 or corporate@oreilly.com. Editor: Mike Loukides Series Editor: Richard Monson-Haefel Production Editor: Rachel Monaghan Proofreader: Rachel Monaghan Compositor: Ron Bilodeau Indexer: Julie Hawks Interior Designer: Ron Bilodeau Cover Designers: Mark Paglietti and Susan Thompson Print History: February 2010: First Edition. The O’Reilly logo is a registered trademark of O’Reilly Media, Inc. 97 Things Every Programmer Should Know and related trade dress are trademarks of O’Reilly Media, Inc. Many of the designations used by manufacturers and sellers to distinguish their products are clarified as trademarks. Where those designations appear in this book, and O’Reilly Media, Inc. was aware of a trademark claim, the designations have been printed in caps or initial caps. While every precaution has been taken in the preparation of this book, the publisher and au- thors assume no responsibility for errors and omissions, or for damages resulting from the use of the information contained herein. This book uses Repkover,™ a durable and flexible lay-flat binding. ISBN: 978-0-596-80948-5 [SB]
(This page has no text content)
v Contents Contributions by Category . . . . . . . . . . . . . . . . . . . . . xv Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii Act with Prudence . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Seb Rose Apply Functional Programming Principles . . . . . . . . . . . . 4 Edward Garson Ask, “What Would the User Do?” (You Are Not the User) . . 6 Giles Colborne Automate Your Coding Standard . . . . . . . . . . . . . . . . . . 8 Filip van Laenen Beauty Is in Simplicity . . . . . . . . . . . . . . . . . . . . . . . . 10 Jørn Ølmheim Before You Refactor . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Rajith Attapattu Beware the Share . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 Udi Dahan
vi Contents The Boy Scout Rule . . . . . . . . . . . . . . . . . . . . . . . . . . 16 Robert C. Martin (Uncle Bob) Check Your Code First Before Looking to Blame Others . . . 18 Allan Kelly Choose Your Tools with Care . . . . . . . . . . . . . . . . . . . 20 Giovanni Asproni Code in the Language of the Domain . . . . . . . . . . . . . . 22 Dan North Code Is Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 Ryan Brush Code Layout Matters . . . . . . . . . . . . . . . . . . . . . . . . . 26 Steve Freeman Code Reviews . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 Mattias Karlsson Coding with Reason . . . . . . . . . . . . . . . . . . . . . . . . . 30 Yechiel Kimchi A Comment on Comments . . . . . . . . . . . . . . . . . . . . . 32 Cal Evans Comment Only What the Code Cannot Say . . . . . . . . . . 34 Kevlin Henney Continuous Learning . . . . . . . . . . . . . . . . . . . . . . . . . 36 Clint Shank Convenience Is Not an -ility . . . . . . . . . . . . . . . . . . . . 38 Gregor Hohpe
viiContents Deploy Early and Often . . . . . . . . . . . . . . . . . . . . . . . 40 Steve Berczuk Distinguish Business Exceptions from Technical . . . . . . . 42 Dan Bergh Johnsson Do Lots of Deliberate Practice. . . . . . . . . . . . . . . . . . . 44 Jon Jagger Domain-Specific Languages . . . . . . . . . . . . . . . . . . . . 46 Michael Hunger Don’t Be Afraid to Break Things . . . . . . . . . . . . . . . . . 48 Mike Lewis Don’t Be Cute with Your Test Data . . . . . . . . . . . . . . . . 50 Rod Begbie Don’t Ignore That Error! . . . . . . . . . . . . . . . . . . . . . . . 52 Pete Goodliffe Don’t Just Learn the Language, Understand Its Culture . . 54 Anders Norås Don’t Nail Your Program into the Upright Position . . . . . . 56 Verity Stob Don’t Rely on “Magic Happens Here” . . . . . . . . . . . . . . 58 Alan Griffiths Don’t Repeat Yourself . . . . . . . . . . . . . . . . . . . . . . . . 60 Steve Smith Don’t Touch That Code! . . . . . . . . . . . . . . . . . . . . . . . 62 Cal Evans
viii Contents Encapsulate Behavior, Not Just State . . . . . . . . . . . . . . 64 Einar Landre Floating-Point Numbers Aren’t Real . . . . . . . . . . . . . . . 66 Chuck Allison Fulfill Your Ambitions with Open Source . . . . . . . . . . . . 68 Richard Monson-Haefel The Golden Rule of API Design . . . . . . . . . . . . . . . . . . 70 Michael Feathers The Guru Myth . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 Ryan Brush Hard Work Does Not Pay Off . . . . . . . . . . . . . . . . . . . 74 Olve Maudal How to Use a Bug Tracker . . . . . . . . . . . . . . . . . . . . . 76 Matt Doar Improve Code by Removing It . . . . . . . . . . . . . . . . . . . 78 Pete Goodliffe Install Me . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80 Marcus Baker Interprocess Communication Affects Application Response Time . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 Randy Stafford Keep the Build Clean . . . . . . . . . . . . . . . . . . . . . . . . . 84 Johannes Brodwall Know How to Use Command-Line Tools . . . . . . . . . . . . 86 Carroll Robinson
ixContents Know Well More Than Two Programming Languages . . . . 88 Russel Winder Know Your IDE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 Heinz Kabutz Know Your Limits . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 Greg Colvin Know Your Next Commit . . . . . . . . . . . . . . . . . . . . . . 94 Dan Bergh Johnsson Large, Interconnected Data Belongs to a Database . . . . . 96 Diomidis Spinellis Learn Foreign Languages . . . . . . . . . . . . . . . . . . . . . . 98 Klaus Marquardt Learn to Estimate . . . . . . . . . . . . . . . . . . . . . . . . . . . 100 Giovanni Asproni Learn to Say, “Hello, World” . . . . . . . . . . . . . . . . . . . . 102 Thomas Guest Let Your Project Speak for Itself . . . . . . . . . . . . . . . . . 104 Daniel Lindner The Linker Is Not a Magical Program . . . . . . . . . . . . . . 106 Walter Bright The Longevity of Interim Solutions . . . . . . . . . . . . . . . . 108 Klaus Marquardt Make Interfaces Easy to Use Correctly and Hard to Use Incorrectly . . . . . . . . . . . . . . . . . . . . 110 Scott Meyers
x Contents Make the Invisible More Visible . . . . . . . . . . . . . . . . . . 112 Jon Jagger Message Passing Leads to Better Scalability in Parallel Systems . . . . . . . . . . . . . . . . . . . . . . . . . . 114 Russel Winder A Message to the Future . . . . . . . . . . . . . . . . . . . . . . 116 Linda Rising Missing Opportunities for Polymorphism . . . . . . . . . . . . 118 Kirk Pepperdine News of the Weird: Testers Are Your Friends . . . . . . . . . 120 Burk Hufnagel One Binary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 Steve Freeman Only the Code Tells the Truth . . . . . . . . . . . . . . . . . . . 124 Peter Sommerlad Own (and Refactor) the Build . . . . . . . . . . . . . . . . . . . 126 Steve Berczuk Pair Program and Feel the Flow . . . . . . . . . . . . . . . . . . 128 Gudny Hauknes, Kari Røssland, and Ann Katrin Gagnat Prefer Domain-Specific Types to Primitive Types . . . . . . 130 Einar Landre Prevent Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132 Giles Colborne The Professional Programmer . . . . . . . . . . . . . . . . . . . 134 Robert C. Martin (Uncle Bob)
xiContents Put Everything Under Version Control . . . . . . . . . . . . . 136 Diomidis Spinellis Put the Mouse Down and Step Away from the Keyboard . 138 Burk Hufnagel Read Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140 Karianne Berg Read the Humanities . . . . . . . . . . . . . . . . . . . . . . . . . 142 Keith Braithwaite Reinvent the Wheel Often . . . . . . . . . . . . . . . . . . . . . 144 Jason P. Sage Resist the Temptation of the Singleton Pattern . . . . . . . . 146 Sam Saariste The Road to Performance Is Littered with Dirty Code Bombs . . . . . . . . . . . . . . . . . . . . . . . 148 Kirk Pepperdine Simplicity Comes from Reduction . . . . . . . . . . . . . . . . 150 Paul W. Homer The Single Responsibility Principle . . . . . . . . . . . . . . . . 152 Robert C. Martin (Uncle Bob) Start from Yes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154 Alex Miller Step Back and Automate, Automate, Automate . . . . . . . 156 Cay Horstmann Take Advantage of Code Analysis Tools . . . . . . . . . . . . 158 Sarah Mount
xii Contents Test for Required Behavior, Not Incidental Behavior. . . . . 160 Kevlin Henney Test Precisely and Concretely . . . . . . . . . . . . . . . . . . . 162 Kevlin Henney Test While You Sleep (and over Weekends) . . . . . . . . . . 164 Rajith Attapattu Testing Is the Engineering Rigor of Software Development . . . . . . . . . . . . . . . . . . . . . 166 Neal Ford Thinking in States . . . . . . . . . . . . . . . . . . . . . . . . . . . 168 Niclas Nilsson Two Heads Are Often Better Than One . . . . . . . . . . . . . 170 Adrian Wible Two Wrongs Can Make a Right (and Are Difficult to Fix) . 172 Allan Kelly Ubuntu Coding for Your Friends . . . . . . . . . . . . . . . . . 174 Aslam Khan The Unix Tools Are Your Friends . . . . . . . . . . . . . . . . . 176 Diomidis Spinellis Use the Right Algorithm and Data Structure . . . . . . . . . 178 Jan Christiaan “JC” van Winkel Verbose Logging Will Disturb Your Sleep . . . . . . . . . . . 180 Johannes Brodwall
xiiiContents WET Dilutes Performance Bottlenecks . . . . . . . . . . . . . 182 Kirk Pepperdine When Programmers and Testers Collaborate . . . . . . . . . 184 Janet Gregory Write Code As If You Had to Support It for the Rest of Your Life . . . . . . . . . . . . . . . . . . . . . . . 186 Yuriy Zubarev Write Small Functions Using Examples . . . . . . . . . . . . . 188 Keith Braithwaite Write Tests for People . . . . . . . . . . . . . . . . . . . . . . . . 190 Gerard Meszaros You Gotta Care About the Code . . . . . . . . . . . . . . . . . 192 Pete Goodliffe Your Customers Do Not Mean What They Say . . . . . . . . 194 Nate Jackson Contributors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
(This page has no text content)
xv Contributions by Category Bugs and Fixes Check Your Code First Before Looking to Blame Others . . . . . . . 18 Don’t Touch That Code! . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 How to Use a Bug Tracker . . . . . . . . . . . . . . . . . . . . . . . . . . 76 Two Wrongs Can Make a Right (and Are Difficult to Fix) . . . . . 172 Build and Deployment Deploy Early and Often . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 Don’t Touch That Code! . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 Install Me . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80 Keep the Build Clean . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 Let Your Project Speak for Itself . . . . . . . . . . . . . . . . . . . . . 104 One Binary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 Own (and Refactor) the Build . . . . . . . . . . . . . . . . . . . . . . . 126 Coding Guidelines and Code Layout Automate Your Coding Standard . . . . . . . . . . . . . . . . . . . . . . 8 Code Layout Matters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 Code Reviews . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 A Comment on Comments . . . . . . . . . . . . . . . . . . . . . . . . . . 32 Comment Only What the Code Cannot Say . . . . . . . . . . . . . . . 34 Take Advantage of Code Analysis Tools . . . . . . . . . . . . . . . . 158
xvi Contents Design Principles and Coding Techniques Apply Functional Programming Principles . . . . . . . . . . . . . . . . 4 Ask, “What Would the User Do?” (You Are Not the User) . . . . . . 6 Beauty Is in Simplicity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Choose Your Tools with Care . . . . . . . . . . . . . . . . . . . . . . . . 20 Code in the Language of the Domain . . . . . . . . . . . . . . . . . . . 22 Code Is Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 Coding with Reason . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 Convenience Is Not an -ility . . . . . . . . . . . . . . . . . . . . . . . . . 38 Distinguish Business Exceptions from Technical . . . . . . . . . . . . 42 Don’t Repeat Yourself . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 Encapsulate Behavior, Not Just State . . . . . . . . . . . . . . . . . . . 64 The Golden Rule of API Design . . . . . . . . . . . . . . . . . . . . . . . 70 Interprocess Communication Affects Application Response Time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 Make Interfaces Easy to Use Correctly and Hard to Use Incorrectly . . . . . . . . . . . . . . . . . . . . . . . . 110 Message Passing Leads to Better Scalability in Parallel Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114 Missing Opportunities for Polymorphism . . . . . . . . . . . . . . . . 118 Only the Code Tells the Truth . . . . . . . . . . . . . . . . . . . . . . . 124 Prefer Domain-Specific Types to Primitive Types . . . . . . . . . . 130 Prevent Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132 Resist the Temptation of the Singleton Pattern . . . . . . . . . . . . 146 The Single Responsibility Principle . . . . . . . . . . . . . . . . . . . . 152 Thinking in States . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168 WET Dilutes Performance Bottlenecks . . . . . . . . . . . . . . . . . 182 Domain Thinking Code in the Language of the Domain . . . . . . . . . . . . . . . . . . . 22 Domain-Specific Languages . . . . . . . . . . . . . . . . . . . . . . . . . 46 Learn Foreign Languages . . . . . . . . . . . . . . . . . . . . . . . . . . . 98 Prefer Domain-Specific Types to Primitive Types . . . . . . . . . . 130
xviiContents Read the Humanities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142 Thinking in States . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168 Write Small Functions Using Examples . . . . . . . . . . . . . . . . . 188 Errors, Error Handling, and Exceptions Distinguish Business Exceptions from Technical . . . . . . . . . . . . 42 Don’t Ignore That Error! . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 Don’t Nail Your Program into the Upright Position . . . . . . . . . . . 56 Prevent Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132 Verbose Logging Will Disturb Your Sleep . . . . . . . . . . . . . . . 180 Learning, Skills, and Expertise Continuous Learning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 Do Lots of Deliberate Practice. . . . . . . . . . . . . . . . . . . . . . . 44 Don’t Just Learn the Language, Understand Its Culture . . . . . . . 54 Fulfill Your Ambitions with Open Source . . . . . . . . . . . . . . . . . 68 The Guru Myth . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 Hard Work Does Not Pay Off . . . . . . . . . . . . . . . . . . . . . . . . 74 Read Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140 Read the Humanities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142 Reinvent the Wheel Often . . . . . . . . . . . . . . . . . . . . . . . . . 144 Nocturnal or Magical Don’t Rely on “Magic Happens Here” . . . . . . . . . . . . . . . . . . . 58 Don’t Touch That Code! . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 The Guru Myth . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 Know How to Use Command-Line Tools . . . . . . . . . . . . . . . . . 86 The Linker Is Not a Magical Program . . . . . . . . . . . . . . . . . . 106 Test While You Sleep (and over Weekends) . . . . . . . . . . . . . . 164 Verbose Logging Will Disturb Your Sleep . . . . . . . . . . . . . . . 180 Write Code As If You Had to Support It for the Rest of Your Life . . . . . . . . . . . . . . . . . . . . . . . . . . . 186
xviii Contents Performance, Optimization, and Representation Apply Functional Programming Principles . . . . . . . . . . . . . . . . 4 Floating-Point Numbers Aren’t Real . . . . . . . . . . . . . . . . . . . . 66 Improve Code by Removing It . . . . . . . . . . . . . . . . . . . . . . . . 78 Interprocess Communication Affects Application Response Time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 Know Your Limits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 Large, Interconnected Data Belongs to a Database . . . . . . . . . . 96 Message Passing Leads to Better Scalability in Parallel Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114 The Road to Performance Is Littered with Dirty Code Bombs . . 148 Use the Right Algorithm and Data Structure . . . . . . . . . . . . . 178 WET Dilutes Performance Bottlenecks . . . . . . . . . . . . . . . . . 182 Professionalism, Mindset, and Attitude Continuous Learning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 Do Lots of Deliberate Practice. . . . . . . . . . . . . . . . . . . . . . . 44 Hard Work Does Not Pay Off . . . . . . . . . . . . . . . . . . . . . . . . 74 The Longevity of Interim Solutions . . . . . . . . . . . . . . . . . . . . 108 The Professional Programmer . . . . . . . . . . . . . . . . . . . . . . . 134 Put the Mouse Down and Step Away from the Keyboard . . . . . 138 Testing Is the Engineering Rigor of Software Development . . . . 166 Write Code As If You Had to Support It for the Rest of Your Life . . . . . . . . . . . . . . . . . . . . . . . . . . . 186 You Gotta Care About the Code . . . . . . . . . . . . . . . . . . . . . 192 Programming Languages and Paradigms Apply Functional Programming Principles . . . . . . . . . . . . . . . . 4 Domain-Specific Languages . . . . . . . . . . . . . . . . . . . . . . . . . 46 Don’t Just Learn the Language, Understand Its Culture . . . . . . . 54 Know Well More Than Two Programming Languages . . . . . . . . . 88 Learn Foreign Languages . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
Comments 0
Loading comments...
Reply to Comment
Edit Comment