Preface
Weve worked at highly successful software companies, with outstanding engineers, and the code we encounter still has plenty of room for improvement. In fact, weve seen some really ugly code, and you probably have too.
But when we see beautifully written code, its inspiring. Good code can teach you whats going on very quickly. Its 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. Were 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 thats already there. We hope youll find this book so helpful to your day-to-day programming that youll recommend it to everyone on your team.
What This Book Is About
This book is about how to write code thats 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. Weve avoided any advanced language features, so even if you dont 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 aestheticssimple 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
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 youd 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 youre 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 OReilly 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 products 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.
.
Safari Books Online
Note
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.
OReilly 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 OReilly and other publishers, sign up for free at http://my.safaribooksonline.com.
How to Contact Us
Please address comments and questions concerning this book to the publisher:
OReilly 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:
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
Wed 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.