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.
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
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.
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.
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.
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.
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
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.
Get The Art of Readable Code now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.