Preface

Welcome to The Rules of Programming, a set of easy-to-remember and easy-to-apply Rules that will help you write better code. Programming is hard, but following the Rules makes it a little bit easier.

Here are some tips on reading the book:

  • All of the Rules stand on their own. If you see an interesting-looking Rule in the table of contents and want to jump straight into the middle of the book, feel free. That reading pattern is fully supported.

  • That said, I’d suggest starting off with Rule 1, “As Simple as Possible, but No Simpler”. It’s a good setup for the rest of the Rules.

  • The examples in the book are all written in C++. If you’re a Python or JavaScript programmer, you’ll be happier if you read Appendix A, “Reading C++ for Python Programmers”, or Appendix B, “Reading C++ for JavaScript Programmers”, before getting too far into the Rules. The two appendices act as Rosetta Stones to translate that C++ into the concepts you’re used to. If your experience is with some other language and you find the C++ examples hard to follow, then I suggest the phenomenal website Rosetta Code.

  • If you’re a C++ programmer, note that I’ve simplified a few things in the code examples to make them easier to read for non-C++ programmers. For example, the examples use signed integers in a few places where unsigned integers would be more typical for a C++ program, and I disabled warnings about implicit conversion between signed and unsigned values. I also compiled the examples with an implicit “using std” to avoid a boatload of distracting “std::” references.

  • And finally, I’m capitalizing Rule when I refer to an actual Rule in the book. If you see rule, it’s just a regular old rule, not an officially sanctioned one. The distinction between the two senses of the word was confusing without the capitalization; I hope that excuses me.

I hope you enjoy what follows! I think you’ll discover a few useful thoughts that help you sharpen your programming skills.

Girls Who Code

All royalties from this book go to Girls Who Code, an organization working hard to help young women discover just how rewarding programming can be. When I graduated from college, over a third of computer science graduates were women; these days, it’s more like a fifth. I think we’d all be better off with a more representative gender balance. You probably do, too. And supporting Girls Who Code through donations or volunteering is a step toward making that hope a reality.

Conventions Used in This Book

The following typographical conventions are used in this book:

Italic

Indicates new terms, URLs, email addresses, filenames, and file extensions.

Constant width

Used for program listings, as well as within paragraphs to refer to program elements such as variable or function names, databases, data types, environment variables, statements, and keywords.

Using Code Examples

Supplemental material (code examples, exercises, etc.) is available for download at https://oreil.ly/rules-of-programming-code.

If you have a technical question or a problem using the code examples, please send email to .

This book is here to help you get your job done. In general, if example code is offered with this book, you may use it 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 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 generally do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN. For example: “The Rules of Programming by Chris Zimmerman (O’Reilly). Copyright 2023 Chris Zimmerman, 978-1-098-13311-5.”

If you feel your use of code examples falls outside fair use or the permission given above, feel free to contact us at .

O’Reilly Online Learning

Note

For more than 40 years, O’Reilly Media has provided technology and business training, knowledge, and insight to help companies succeed.

Our unique network of experts and innovators share their knowledge and expertise through books, articles, and our online learning platform. O’Reilly’s online learning platform gives you on-demand access to live training courses, in-depth learning paths, interactive coding environments, and a vast collection of text and video from O’Reilly and 200+ other publishers. For more information, visit https://oreilly.com.

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 https://oreil.ly/rules-of-programming.

If you have reactions, comments, or questions you’d like to share with the author, see The Rules of Programming website for pointers. You can also email to comment or ask technical questions about this book.

For news and information about our books and courses, visit https://oreilly.com.

Find us on LinkedIn: https://linkedin.com/company/oreilly-media

Follow us on Twitter: https://twitter.com/oreillymedia

Watch us on YouTube: https://youtube.com/oreillymedia

Acknowledgments

First off, thanks to my lovely and talented wife Laura, who encouraged me to spend time writing this book instead of doing all the other useful things I could have been doing.

A big round of thanks to all of the people who helped develop the Rules in this book. That includes all Sucker Punch coders past and present, since you all contributed whether you intended to or not, but especially Apoorva Bansal, Chris Heidorn, David Meyer, Eric Black, Evan Christensen, James McNeill, Jasmin Patry, Nate Slottow, Matt Durasoff, Mike Gaffney, Ranjith Rajagopalan, Rob McDaniel, Sam Holley, Sean Smith, Wes Grandmont, and William Rossiter.

And thanks to the non–Sucker Punchers who provided a view from outside the forest: Adam Barr, Andreas Fredriksson, Colin Bryar, David Oliver, Max Schubert, Mike Gutmann, and Seth Fine.

Extra special thanks to the intrepid readers who made it through every single one of the Rules: Adrian Bentley, Bill Rockenbeck, Jan Miksovsky, and Julien Merceron. I officially owe you all a favor.

And finally, thanks to everyone on Team O’Reilly who patiently coached me through my fumbling attempts to write this book: Charles Roumeliotis, Gregory Hyman, Libby James, Mary Treseler, Sara Hunter, Suzanne Huston, and very especially Sarah Grey, who did the rest of you a massive favor by filtering out the least funny of the jokes I kept insisting on adding.

Get The Rules of Programming 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.