The Rules of Programming

How to Write Better Code

The Story of the Rules

The Rules of Programming were born of exasperation.

I’d spent about a decade running programming teams at Microsoft, then cofounded the video game company Sucker Punch in 1997. Both companies have been successful­—in large part because of their ability to recruit and develop top-notch programming teams.

At Sucker Punch, that’s led to a 25-year run of successful games. The three Sly Cooper games, which let kids of all ages experience the thrilling life of the master raccoon thief Sly Cooper and his pals. The five inFamous games, which gave gamers superpowers and the choice to use them for good or evil. And our magnum opus, Ghost of Tsushima, where gamers play a lone samurai fighting back against the 1274 invasion of Japan.

A big part of the recruiting strategy at both Microsoft and Sucker Punch has been hiring smart young programmers, then training them in the ways of professional developers. This practice has been undeniably successful, but it also leads to a particular flavor of frustration.

I kept running into one particular problem over and over again. We’d bring a new programmer onto the team, often someone fresh out of college. I’d review some new feature they planned to introduce into the code, usually to solve a very simple problem—only to discover that they’d written code that attempted to solve a much bigger problem, one that included the very simple and concrete problem as a small subcase.

Aargh! We didn’t need that bigger problem solved! And we certainly didn’t need it solved right now! Invariably, the solution to the bigger problem was a mediocre solution to the simple problem we did have—more complicated to use, more complicated to understand, and capable of hiding a lot more bugs. But just saying that in the code review—that we didn’t need the bigger problem solved, that they should only try to solve problems they understand—was ineffective. New programmers kept doing it.

Out of frustration, I put my foot down. “OK,” I said. “Here’s the new rule. Until you have three examples of a problem, you’re not allowed to write a general solution.”

To my surprise and delight, this actually worked! Turning the general philosophy into a specific rule with specific criteria was an effective way of getting the message across. Sure, most of our new programmers made the premature generalization mistake once, but the rule helped them avoid making it again. It also helped them recognize when it was time to generalize. Fewer than three examples? Don’t generalize. Three or more? Start looking for opportunities.

The Rule worked because it was easy to remember, and the situations where it applied were easy to recognize. When coders could see when they started moving past the bounds of the well-defined problem at hand, they could take a step back, count the number of concrete examples of that sort of problem they’d run into, and make a better decision about whether to generalize or not. They wrote better code.

Over time, we found other important bits of Sucker Punch philosophy that could be distilled into easy-to-remember phrases—aphorisms.

There’s a long history of aphorisms—short, pithy statements that capture some essential truth. I bet you can rattle off many aphorisms from memory. Shoot, I could limit you to bird-related aphorisms, and you’d still be able to come up with at least two! I’ll offer a few:

  • Don’t count your chickens before they’re hatched.

  • A bird in the hand is worth two in the bush.

  • The early bird catches the worm.

  • Don’t put all your eggs in one basket.

Aphorisms stick around because they work. They’re viral, in the modern sense—aphorisms have been “infecting” people with bits of wisdom for thousands of years. It’s not surprising that they’re an effective way of infecting new team members with the Sucker Punch coding philosophy.

So, bit by bit, what was once a single rule grew into a list of rules: the Rules of Programming. They represent many of the most important aspects of Sucker Punch engineering culture: the things that we believe have led to our success, the ideas that new coders on the team need to absorb to be effective. The things that even senior coders like me need reminding of at least once in a while!

And so I wrote this book to bring the Rules to a broader audience. I think you’ll find the book interesting and entertaining, and I hope it will help you on your own journey to write better code.

Sly Cooper and the Thievius Raccoonus box cover

The word aphorism itself was coined by Hippocrates around 400 BCE. Well, strictly speaking, the word he coined was Ἀφορισμός. That was the title of his book of rules for medical diagnosis and treatment, some of which are still spot on millennia later, like aphorism 13 from section 6—“a sneezing attack will cure a case of hiccups.” So true.

In its original form, from Thomas Howell’s New Sonnets and Pretty Pamphlets (1570): “Count not they chickens that unhatched be….” A small demonstration of the staying power of a good aphorism.

The Story of the Rules

The Rules of Programming were born of exasperation.

I’d spent about a decade running programming teams at Microsoft, then cofounded the video game company Sucker Punch in 1997. Both companies have been successful­—in large part because of their ability to recruit and develop top-notch programming teams.

At Sucker Punch, that’s led to a 25-year run of successful games. The three Sly Cooper games, which let kids of all ages experience the thrilling life of the master raccoon thief Sly Cooper and his pals. The five inFamous games, which gave gamers superpowers and the choice to use them for good or evil. And our magnum opus, Ghost of Tsushima, where gamers play a lone samurai fighting back against the 1274 invasion of Japan.

A big part of the recruiting strategy at both Microsoft and Sucker Punch has been hiring smart young programmers, then training them in the ways of professional developers. This practice has been undeniably successful, but it also leads to a particular flavor of frustration.

I kept running into one particular problem over and over again. We’d bring a new programmer onto the team, often someone fresh out of college. I’d review some new feature they planned to introduce into the code, usually to solve a very simple problem—only to discover that they’d written code that attempted to solve a much bigger problem, one that included the very simple and concrete problem as a small subcase.

Aargh! We didn’t need that bigger problem solved! And we certainly didn’t need it solved right now! Invariably, the solution to the bigger problem was a mediocre solution to the simple problem we did have—more complicated to use, more complicated to understand, and capable of hiding a lot more bugs. But just saying that in the code review—that we didn’t need the bigger problem solved, that they should only try to solve problems they understand—was ineffective. New programmers kept doing it.

Out of frustration, I put my foot down. “OK,” I said. “Here’s the new rule. Until you have three examples of a problem, you’re not allowed to write a general solution.”

To my surprise and delight, this actually worked! Turning the general philosophy into a specific rule with specific criteria was an effective way of getting the message across. Sure, most of our new programmers made the premature generalization mistake once, but the rule helped them avoid making it again. It also helped them recognize when it was time to generalize. Fewer than three examples? Don’t generalize. Three or more? Start looking for opportunities.

The Rule worked because it was easy to remember, and the situations where it applied were easy to recognize. When coders could see when they started moving past the bounds of the well-defined problem at hand, they could take a step back, count the number of concrete examples of that sort of problem they’d run into, and make a better decision about whether to generalize or not. They wrote better code.

Over time, we found other important bits of Sucker Punch philosophy that could be distilled into easy-to-remember phrases—aphorisms.

There’s a long history of aphorisms—short, pithy statements that capture some essential truth. I bet you can rattle off many aphorisms from memory. Shoot, I could limit you to bird-related aphorisms, and you’d still be able to come up with at least two! I’ll offer a few:

  • Don’t count your chickens before they’re hatched.

  • A bird in the hand is worth two in the bush.

  • The early bird catches the worm.

  • Don’t put all your eggs in one basket.

Aphorisms stick around because they work. They’re viral, in the modern sense—aphorisms have been “infecting” people with bits of wisdom for thousands of years. It’s not surprising that they’re an effective way of infecting new team members with the Sucker Punch coding philosophy.

So, bit by bit, what was once a single rule grew into a list of rules: the Rules of Programming. They represent many of the most important aspects of Sucker Punch engineering culture: the things that we believe have led to our success, the ideas that new coders on the team need to absorb to be effective. The things that even senior coders like me need reminding of at least once in a while!

And so I wrote this book to bring the Rules to a broader audience. I think you’ll find the book interesting and entertaining, and I hope it will help you on your own journey to write better code.

quotes from real people who’ve read the book

The Rules of Programming provides great insights for both new and experienced coders. Zimmerman’s style makes it an entertaining read, and the 21 rules are an important contribution to better software at a time when technology is pervasive in every part of business and society.
Paul Daugherty
Group Chief Executive of Technology and CTO, Accenture

Reading this book gives you a sense of what it’s like to work on that rarest of treats: the large, well-functioning engineering team. Each chapter presents hard-won lessons illustrated with code and leavened with interesting anecdotes from some very successful projects. Above all, Zimmerman’s book inculcates a proper sense of developer humility — working in a collective manner that acknowledges, and is resilient to, normal human fallibility and the gravity-like pull of complexity in growing projects. Reading this as a group would be a great team exercise.
Jan Miksovsky
graphorigami.org

The Rules of Programming fills in the gaps that most programming books and CS departments leave out—the practices that differentiate a novice programmer from a seasoned professional.
Jasmin Patry
Rendering Lead, Sucker Punch

On the one hand, Chris takes a certain pleasure putting his fingers on controversial opinions. And thanks to the precise examples being brought to the readers, challenging your own way of thinking is made a lot easier. On the other hand, Chris has put in words things that some of us are doing intuitively. Chris just made training way easier. A must read!
Julien Merceron
Chief Technology Officer, Bandai Namco

This book belongs in every professional software developer's toolkit. It is full of techniques that are sure to increase your productivity and your team's, reduce bugs, and result in more easily maintainable and extensible software systems.
Chris Heidorn
Senior Staff Engineer, Sucker Punch

The Rules of Programming combines great guidance for beginners with subtle lessons that may teach even the experts. Zimmerman keeps it fun, too—proving that it’s possible to be both entertaining and instructive.
Mark Cerny
Lead System Architect, PlayStation 4 and 5

The Rules of Programming is full of pragmatic rules of thumb which any Software Engineer can use to level up their skills. I love the mix of pithy sayings and detailed concrete examples–such as "Generalization Takes Three Examples" with many code samples putting that rule to practice. I was fortunate to learn these lessons directly from Chris early in my career, and have successfully applied them across a wide variety of software disciplines. With this book, you have the opportunity to do the same.
Chris Bentzel
Director of Software, Boston Dynamics

The Rules of Programming is a fun read and provides some insight into practical game engine development. The stories and guidelines presented should provide interesting food for thought for experienced programmers and valuable starting points for the less experienced.
Adrian Bentley
Development Manager, Sucker Punch

I really appreciate the set of helpful tools Chris provides that can be used for those critical 52/48 design/coding decisions that come up so often.
David Oliver
Principal Group Engineering Manager, Microsoft

The Rules of Programming deftly articulates a lifetime of real-world engineering lessons into a fantastic handbook of core concepts that are both helpful reminders to experienced developers and a guidepost to those just starting their journey as software engineers. If you’re looking for practical ways to ensure the success of your project, you need to add this book to your library.
Wes Grandmont III
Lead Technical Art Director, Sucker Punch

For Readers Only

If you’ve read the book, you may have questions. Maybe some explanation doesn’t make sense to you. Or you might be curious how the villainous example used in Rule 4 is then used as an example in later Rules with no mention of its earlier villainy. Or you might find a bug in the code examples and wonder how to let me know and/or taunt me about my clumsiness. Or you might wonder whether the 21 Rules in the book are the full set, and if not what Rules were omitted.

If so, there’s an official place to ask these questions. I’ve created a space on Quora created for exactly that purpose:

https://therulesofprogramming.quora.com/

No promises on how quickly I’ll get back to you, but it shouldn’t take too long. I look forward to hearing which Rules you think are useful and (even more interestingly) which ones you’re going to chuck overboard!

Girls Who Code

I decided that writing a book would be way more fun if it benefited a good cause. And I couldn't think of a better organization to support than Girls Who Code, the direct recipient for all royalties from this book. 
 
Girls Who Code is 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. That's ridiculous. 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.
 
Or even the small act of buying this book. Learn to write better code; support a good cause. Everybody wins.