Share E-Book

The Art of Readable Code Simple and Practical Techniques for Writing Better Code (Dustin Boswell Trevor Foucher) (z-library.sk, 1lib.sk, z-lib.sk)

Author

,

Rating 4.0 AI initial rating

Log in to rate

Code
Language English

As programmers, we've all seen source code that’s so ugly and buggy it makes our brain ache. Over the last five years, authors Dustin Boswell and Trevor Foucher have analyzed hundreds of examples of "bad code" (much of it their own) to determine why they're bad and how they could be improved. Their conclusion? You need to write code that minimizes the time it would take someone else to understand it—even if that someone else is you. This book focuses on basic principles and practical techniques you can apply every time you write code. Using easy-to-digest code examples from different languages, each chapter dives into a different aspect of coding, and demonstrates how you can make your code easy to understand. • Simplify naming, commenting, and formatting with tips that apply to every line of code • Refine your program’s loops, logic, and variables to reduce complexity and confusion • Attack problems at the function level, such as reorganizing blocks of code to do one task at a time • Write effective test code that is thorough and concise—as well as readable Dustin Boswell graduated from CalTech, and worked at Google for five years on web crawling infrastructure and ad programs. He's built several websites and enjoys working on big data and machine learning. Trevor Foucher has spent the past decade at Microsoft as an engineer, manager, and tech lead on Windows and security products, and at Google working on ad programs and search infrastructure.

Format PDF
Size 24.9 MB
(First 20 pages)

Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

Page 1
(This page has no text content)
Page 2
(This page has no text content)
Page 3
The Art of Readable Code Dustin Boswell and Trevor Foucher Beijing • Cambridge • Farnham • Köln • Sebastopol • Tokyo
Page 4
The Art of Readable Code by Dustin Boswell and Trevor Foucher Copyright © 2012 Dustin Boswell and Trevor Foucher. 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: Mary Treseler Production Editor: Teresa Elsey Copyeditor: Nancy Wolfe Kotary Proofreader: Teresa Elsey Indexer: Potomac Indexing, LLC Cover Designer: Susan Thompson Interior Designer: David Futato Illustrators: Dave Allred and Robert Romano November 2011: First Edition. Revision History for the First Edition: 2011-11-01 First release See http://oreilly.com/catalog/errata.csp?isbn=9780596802295 for release details. Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly Media, Inc. The Art of Readable Code, the image of sheet music, 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 claimed 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 authors assume no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein. ISBN: 978-0-596-80229-5 [LSI] 1320175254
Page 5
C O N T E N T S PREFACE vii 1 CODE SHOULD BE EASY TO UNDERSTAND 1 What Makes Code “Better”? 2 The Fundamental Theorem of Readability 3 Is Smaller Always Better? 3 Does Time-Till-Understanding Conflict with Other Goals? 4 The Hard Part 4 Part One SURFACE-LEVEL IMPROVEMENTS 2 PACKING INFORMATION INTO NAMES 7 Choose Specific Words 8 Avoid Generic Names Like tmp and retval 10 Prefer Concrete Names over Abstract Names 13 Attaching Extra Information to a Name 15 How Long Should a Name Be? 18 Use Name Formatting to Convey Meaning 20 Summary 21 3 NAMES THAT CAN’T BE MISCONSTRUED 23 Example: Filter() 24 Example: Clip(text, length) 24 Prefer min and max for (Inclusive) Limits 25 Prefer first and last for Inclusive Ranges 26 Prefer begin and end for Inclusive/Exclusive Ranges 26 Naming Booleans 27 Matching Expectations of Users 27 Example: Evaluating Multiple Name Candidates 29 Summary 31 4 AESTHETICS 33 Why Do Aesthetics Matter? 34 Rearrange Line Breaks to Be Consistent and Compact 35 Use Methods to Clean Up Irregularity 37 Use Column Alignment When Helpful 38 Pick a Meaningful Order, and Use It Consistently 39 Organize Declarations into Blocks 40 Break Code into “Paragraphs” 41 Personal Style versus Consistency 42 Summary 43 iii
Page 6
5 KNOWING WHAT TO COMMENT 45 What NOT to Comment 47 Recording Your Thoughts 49 Put Yourself in the Reader’s Shoes 51 Final Thoughts—Getting Over Writer’s Block 56 Summary 57 6 MAKING COMMENTS PRECISE AND COMPACT 59 Keep Comments Compact 60 Avoid Ambiguous Pronouns 60 Polish Sloppy Sentences 61 Describe Function Behavior Precisely 61 Use Input/Output Examples That Illustrate Corner Cases 61 State the Intent of Your Code 62 “Named Function Parameter” Comments 63 Use Information-Dense Words 64 Summary 65 Part Two SIMPLIFYING LOOPS AND LOGIC 7 MAKING CONTROL FLOW EASY TO READ 69 The Order of Arguments in Conditionals 70 The Order of if/else Blocks 71 The ?: Conditional Expression (a.k.a. “Ternary Operator”) 73 Avoid do/while Loops 74 Returning Early from a Function 75 The Infamous goto 76 Minimize Nesting 77 Can You Follow the Flow of Execution? 79 Summary 80 8 BREAKING DOWN GIANT EXPRESSIONS 83 Explaining Variables 84 Summary Variables 84 Using De Morgan’s Laws 85 Abusing Short-Circuit Logic 86 Example: Wrestling with Complicated Logic 86 Breaking Down Giant Statements 89 Another Creative Way to Simplify Expressions 90 Summary 90 9 VARIABLES AND READABILITY 93 Eliminating Variables 94 Shrink the Scope of Your Variables 97 Prefer Write-Once Variables 103 A Final Example 104 Summary 106 iv C O N T E N T S
Page 7
Part Three REORGANIZING YOUR CODE 10 EXTRACTING UNRELATED SUBPROBLEMS 109 Introductory Example: findClosestLocation() 110 Pure Utility Code 111 Other General-Purpose Code 112 Create a Lot of General-Purpose Code 114 Project-Specific Functionality 115 Simplifying an Existing Interface 116 Reshaping an Interface to Your Needs 117 Taking Things Too Far 117 Summary 118 11 ONE TASK AT A TIME 121 Tasks Can Be Small 123 Extracting Values from an Object 124 A Larger Example 128 Summary 130 12 TURNING THOUGHTS INTO CODE 131 Describing Logic Clearly 132 Knowing Your Libraries Helps 133 Applying This Method to Larger Problems 134 Summary 137 13 WRITING LESS CODE 139 Don’t Bother Implementing That Feature—You Won’t Need It 140 Question and Break Down Your Requirements 140 Keeping Your Codebase Small 142 Be Familiar with the Libraries Around You 143 Example: Using Unix Tools Instead of Coding 144 Summary 145 Part Four SELECTED TOPICS 14 TESTING AND READABILITY 149 Make Tests Easy to Read and Maintain 150 What’s Wrong with This Test? 150 Making This Test More Readable 151 Making Error Messages Readable 154 Choosing Good Test Inputs 156 Naming Test Functions 158 What Was Wrong with That Test? 159 Test-Friendly Development 160 Going Too Far 162 Summary 162 15 DESIGNING AND IMPLEMENTING A “MINUTE/HOUR COUNTER” 165 The Problem 166 Defining the Class Interface 166 C O N T E N T S v
Page 8
Attempt 1: A Naive Solution 169 Attempt 2: Conveyor Belt Design 171 Attempt 3: A Time-Bucketed Design 174 Comparing the Three Solutions 179 Summary 179 A FURTHER READING 181 INDEX 185 vi C O N T E N T S
Page 9
P R E F A C E vii
Page 10
We’ve worked at highly successful software companies, with outstanding engineers, and the code we encounter still has plenty of room for improvement. In fact, we’ve seen some really ugly code, and you probably have too. But when we see beautifully written code, it’s inspiring. Good code can teach you what’s going on very quickly. It’s fun to use, and it motivates you to make your own code better. The goal of this book is help you make your code better. And when we say “code,” we literally mean the lines of code you are staring at in your editor. We’re not talking about the overall architecture of your project, or your choice of design patterns. Those are certainly important, but in our experience most of our day-to-day lives as programmers are spent on the “basic” stuff, like naming variables, writing loops, and attacking problems down at the function level. And a big part of this is reading and editing the code that’s already there. We hope you’ll find this book so helpful to your day-to-day programming that you’ll recommend it to everyone on your team. What This Book Is About This book is about how to write code that’s highly readable. The key idea in this book is that code should be easy to understand. Specifically, your goal should be to minimize the time it takes someone else to understand your code. This book explains this idea and illustrates it with lots of examples from different languages, including C++, Python, JavaScript, and Java. We’ve avoided any advanced language features, so even if you don’t know all these languages, it should still be easy to follow along. (In our experience, the concepts of readability are mostly language-independent, anyhow.) Each chapter dives into a different aspect of coding and how to make it “easy to understand.” The book is divided into four parts: Surface-level improvements Naming, commenting, and aesthetics—simple tips that apply to every line of your codebase Simplifying loops and logic Ways to refine the loops, logic, and variables in your program to make them easier to understand Reorganizing your code Higher-level ways to organize large blocks of code and attack problems at the function level Selected topics Applying “easy to understand” to testing and to a larger data structure coding example viii P R E F A C E
Page 11
How to Read This Book Our book is intended to be a fun, casual read. We hope most readers will read the whole book in a week or two. The chapters are ordered by “difficulty”: basic topics are at the beginning, and more advanced topics are at the end. However, each chapter is self-contained and can be read in isolation. So feel free to skip around if you’d like. Using Code Examples This book is here to help you get your job done. In general, you may use the code in this book in your programs and documentation. You do not need to contact us for permission unless you’re reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from this book does not require permission. Selling or distributing a CD-ROM of examples from O’Reilly books does require permission. Answering a question by citing this book and quoting example code does not require permission. Incorporating a significant amount of example code from this book into your product’s documentation does require permission. We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN. For example: “The Art of Readable Code by Dustin Boswell and Trevor Foucher. Copyright 2012 Dustin Boswell and Trevor Foucher, 978-0-596-80229-5.” If you feel your use of code examples falls outside fair use or the permission given above, feel free to contact us at permissions@oreilly.com. Safari® Books Online Safari Books Online is an on-demand digital library that lets you easily search over 7,500 technology and creative reference books and videos to find the answers you need quickly. With a subscription, you can read any page and watch any video from our library online. Read books on your cell phone and mobile devices. Access new titles before they are available for print, and get exclusive access to manuscripts in development and post feedback for the authors. Copy and paste code samples, organize your favorites, download chapters, bookmark key sections, create notes, print out pages, and benefit from tons of other time-saving features. O’Reilly Media has uploaded this book to the Safari Books Online service. To have full digital access to this book and others on similar topics from O’Reilly and other publishers, sign up for free at http://my.safaribooksonline.com. P R E F A C E ix
Page 12
How to Contact Us Please address comments and questions concerning this book to the publisher: O’Reilly Media, Inc. 1005 Gravenstein Highway North Sebastopol, CA 95472 800-998-9938 (in the United States or Canada) 707-829-0515 (international or local) 707-829-0104 (fax) We have a web page for this book, where we list errata, examples, and any additional information. You can access this page at: http://shop.oreilly.com/product/9780596802301.do To comment or ask technical questions about this book, send email to: bookquestions@oreilly.com For more information about our books, courses, conferences, and news, see our website at http://www.oreilly.com. Find us on Facebook: http://facebook.com/oreilly Follow us on Twitter: http://twitter.com/oreillymedia Watch us on YouTube: http://www.youtube.com/oreillymedia Acknowledgments We’d like to thank our colleagues who donated their time to review our entire manuscript, including Alan Davidson, Josh Ehrlich, Rob Konigsberg, Archie Russell, Gabe W., and Asaph Zemach. Any errors in the book are entirely their fault (just kidding). We're grateful to the many reviewers who gave us detailed feedback on various drafts of our book, including Michael Hunger, George Heineman, and Chuck Hudson. We also got numerous ideas and feedback from John Blackburn, Tim Dasilva, Dennis Geels, Steve Gerding, Chris Harris, Josh Hyman, Joel Ingram, Erik Mavrinac, Greg Miller, Anatole Paine, and Nick White. Thanks to the numerous online commenters who reviewed our draft on O’Reilly’s OFPS system. Thanks to the team at O’Reilly for their endless patience and support, specifically Mary Treseler (editor), Teresa Elsey (production editor), Nancy Kotary (copyeditor), Rob Romano (illustrator), Jessica Hosman (tools), and Abby Fox (tools). And also to our cartoonist, Dave Allred, who made our crazy cartoon ideas come to life. Lastly, we’d like to thank Melissa and Suzanne, for encouraging us along the way and putting up with incessant programming conversations. x P R E F A C E
Page 13
C H A P T E R O N E Code Should Be Easy to Understand 1
Page 14
Over the past five years, we have collected hundreds of examples of “bad code” (much of it our own), and analyzed what made it bad, and what principles/techniques were used to make it better. What we noticed is that all of the principles stem from a single theme. K E Y I D E A Code should be easy to understand. We believe this is the most important guiding principle you can use when deciding how to write your code. Throughout the book, we’ll show how to apply this principle to different aspects of your day-to-day coding. But before we begin, we’ll elaborate on this principle and justify why it’s so important. What Makes Code “Better”? Most programmers (including the authors) make programming decisions based on gut feel and intuition. We all know that code like this: for (Node* node = list->head; node != NULL; node = node->next) Print(node->data); is better than code like this: Node* node = list->head; if (node == NULL) return; while (node->next != NULL) { Print(node->data); node = node->next; } if (node != NULL) Print(node->data); (even though both examples behave exactly the same). But a lot of times, it’s a tougher choice. For example, is this code: return exponent >= 0 ? mantissa * (1 << exponent) : mantissa / (1 << -exponent); better or worse than: if (exponent >= 0) { return mantissa * (1 << exponent); } else { return mantissa / (1 << -exponent); } The first version is more compact, but the second version is less intimidating. Which criterion is more important? In general, how do you decide which way to code something? 2 C H A P T E R O N E
Page 15
The Fundamental Theorem of Readability After studying many code examples like this, we came to the conclusion that there is one metric for readability that is more important than any other. It’s so important that we call it “The Fundamental Theorem of Readability.” K E Y I D E A Code should be written to minimize the time it would take for someone else to understand it. What do we mean by this? Quite literally, if you were to take a typical colleague of yours, and measure how much time it took him to read through your code and understand it, this “time- till-understanding” is the theoretical metric you want to minimize. And when we say “understand,” we have a very high bar for this word. For someone to fully understand your code, they should be able to make changes to it, spot bugs, and understand how it interacts with the rest of your code. Now, you might be thinking, Who cares if someone else can understand it? I'm the only one using the code! Even if you’re on a one-man project, it’s worth pursuing this goal. That “someone else” might be you six months later, when your own code looks unfamiliar to you. And you never know—someone might join your project, or your “throwaway code” might get reused for another project. Is Smaller Always Better? Generally speaking, the less code you write to solve a problem, the better (see Chapter 13, Writing Less Code). It probably takes less time to understand a 2000-line class than a 5000-line class. But fewer lines isn’t always better! There are plenty of times when a one-line expression like: assert((!(bucket = FindBucket(key))) || !bucket->IsOccupied()); takes more time to understand than if it were two lines: bucket = FindBucket(key); if (bucket != NULL) assert(!bucket->IsOccupied()); Similarly, a comment can make you understand the code more quickly, even though it “adds code” to the file: // Fast version of "hash = (65599 * hash) + c" hash = (hash << 6) + (hash << 16) - hash + c; So even though having fewer lines of code is a good goal, minimizing the time-till- understanding is an even better goal. C O D E S H O U L D B E E A S Y T O U N D E R S T A N D 3
Page 16
Does Time-Till-Understanding Conflict with Other Goals? You might be thinking, What about other constraints, like making code efficient, or well-architected, or easy to test, and so on? Don’t these sometimes conflict with wanting to make code easy to understand? We’ve found that these other goals don't interfere much at all. Even in the realm of highly optimized code, there are still ways to make it highly readable as well. And making your code easy to understand often leads to code that is well architected and easy to test. The rest of the book discusses how to apply “easy to read” in different circumstances. But remember, when in doubt, the Fundamental Theorem of Readability trumps any other rule or principle in this book. Also, some programmers have a compulsive need to fix any code that isn’t perfectly factored. It’s always important to step back and ask, Is this code easy to understand? If so, it’s probably fine to move on to other code. The Hard Part Yes, it requires extra work to constantly think about whether an imaginary outsider would find your code easy to understand. Doing so requires turning on a part of your brain that might not have been on while coding before. But if you adopt this goal (as we have), we're certain you will become a better coder, have fewer bugs, take more pride in your work, and produce code that everyone around you will love to use. So let’s get started! 4 C H A P T E R O N E
Page 17
P A R T I Surface-Level Improvements We begin our tour of readability with what we consider “surface-level” improvements: picking good names, writing good comments, and formatting your code neatly. These types of changes are easy to apply. You can make them “in place,” without having to refactor your code or change how the program runs. You can also make them incrementally, without a huge time investment. These topics are very important because they affect every line of code in your codebase. Although each change may seem small, in aggregate they can make a huge improvement to a codebase. If your code has great names, well-written comments, and clean use of whitespace, your code will be much easier to read. Of course, there’s a lot more beneath the surface level when it comes to readability (and we’ll cover that in later parts of the book). But the material in this part is so widely applicable, for so little effort, that it’s worth covering first.
Page 18
(This page has no text content)
Page 19
C H A P T E R T W O Packing Information into Names 7
Page 20
Whether you’re naming a variable, a function, or a class, a lot of the same principles apply. We like to think of a name as a tiny comment. Even though there isn’t much room, you can convey a lot of information by choosing a good name. K E Y I D E A Pack information into your names. A lot of the names we see in programs are vague, like tmp. Even words that may seem reasonable, such as size or get, don’t pack much information. This chapter shows you how to pick names that do. This chapter is organized into six specific topics: • Choosing specific words • Avoiding generic names (or knowing when to use them) • Using concrete names instead of abstract names • Attaching extra information to a name, by using a suffix or prefix • Deciding how long a name should be • Using name formatting to pack extra information Choose Specific Words Part of “packing information into names” is choosing words that are very specific and avoiding “empty” words. For example, the word “get” is very unspecific, as in this example: def GetPage(url): The word “get” doesn’t really say much. Does this method get a page from a local cache, from a database, or from the Internet? If it’s from the Internet, a more specific name might be FetchPage() or DownloadPage(). Here’s an example of a BinaryTree class: class BinaryTree { int Size(); What would you expect the Size() method to return? The height of the tree, the number of nodes, or the memory footprint of the tree? The problem is that Size() doesn’t convey much information. A more specific name would be Height(), NumNodes(), or MemoryBytes(). 8 C H A P T E R T W O
The above is a preview of the first 20 pages. Register to read the complete e-book.

Recommended for You

Loading recommended books...
Failed to load, please try again later

Tip the Site

Scan the WeChat Pay or Alipay code to tip. No login required.

WeChat Pay
Alipay

Every 10 yuan is 1 year of VIP; the remainder counts as months (5 yuan is 5 months, 100 yuan is 10 years). Signed-in users are enabled immediately and both you and the admin get an email. Guests receive an ID and are sent to register.

← Back to List