The Rules of Programming

Book description

This philosophy-of-programming guide presents a unique and entertaining take on how to think about programming. A collection of 21 pragmatic rules, each presented in a standalone chapter, captures the essential wisdom that every freshly minted programmer needs to know and provides thought-provoking insights for more seasoned programmers.

Author Chris Zimmerman, cofounder of the video game studio Sucker Punch Productions, teaches basic truths of programming by wrapping them in memorable aphorisms and driving them home with examples drawn from real code. This practical guide also helps managers looking for ways to train new team members.

The rules in this book include:

  • As simple as possible, but no simpler
  • Let your code tell its own story
  • Localize complexity
  • Generalization takes three examples
  • Work backward from your result, not forward from your code
  • The first lesson of optimization is don't optimize
  • A good name is the best documentation
  • Bugs are contagious
  • Eliminate failure cases
  • Code that isn't running doesn't work
  • Sometimes you just need to hammer the nails

Publisher resources

View/Submit Errata

Table of contents

  1. Preface
    1. Girls Who Code
    2. Conventions Used in This Book
    3. Using Code Examples
    4. O’Reilly Online Learning
    5. How to Contact Us
    6. Acknowledgments
  2. The Story of the Rules
  3. How to Disagree with the Rules
  4. 1. As Simple as Possible, but No Simpler
    1. Measuring Simplicity
    2. …But No Simpler
    3. Sometimes It’s Better to Simplify the Problem Rather than the Solution
    4. Simple Algorithms
    5. Don’t Lose the Plot
    6. One Rule to Rule Them All
  5. 2. Bugs Are Contagious
    1. Don’t Count on Your Users
    2. Automated Testing Can Be Tricky
    3. Stateless Code Is Easier to Test
    4. Audit State You Can’t Eliminate
    5. Don’t Trust the Caller
    6. Keeping Your Code Healthy
  6. 3. A Good Name Is the Best Documentation
    1. Don’t Optimize for Minimal Keystrokes
    2. Don’t Mix and Match Conventions
    3. Don’t Shoot Yourself in the Foot
    4. Don’t Make Me Think
  7. 4. Generalization Takes Three Examples
    1. YAGNI
    2. An Obvious Objection to This Strategy, in Response to Which I Double Down
    3. It’s Actually Worse than YAGNI
    4. This Is Not What Success Looks Like
  8. 5. The First Lesson of Optimization Is Don’t Optimize
    1. The First Lesson of Optimization
    2. The Second Lesson of Optimization
    3. Putting the Second Lesson to the Test
      1. Step 1: Measure and Attribute Processor Time
      2. Step 2: Make Sure There’s Not a Bug
      3. Step 3: Measure Your Data
      4. Step 4: Plan and Prototype
      5. Step 5: Optimize and Repeat
    4. Applying the Five-Step Optimization Process
    5. There Is No Third Lesson of Optimization
  9. Interlude: In Which the Previous Chapter Is Criticized
  10. 6. Code Reviews Are Good for Three Reasons
    1. Code Reviews Are About Sharing Knowledge
    2. The Forbidden Code Review
    3. The True Value of the Code Review
    4. Code Reviews Are Inherently Social
  11. 7. Eliminate Failure Cases
    1. A Function That Makes It Easy to Shoot Myself in the Foot
    2. Shooting Myself in the Foot via a Ricochet
    3. Enlisting the Compiler’s Aid to Avoid Shooting My Foot
    4. Timing Is Everything
    5. A More Complicated Example
    6. Making Ordering Mistakes Impossible
    7. Using Templates Instead of Method Chaining
    8. Coordinated Control of State
    9. Detecting Mistakes Is Good, but Making Them Impossible to Express Is Better
  12. 8. Code That Isn’t Running Doesn’t Work
    1. Step 1: A Simple Beginning
    2. Step 2: Generalizing a Common Pattern
    3. Step 3: Adding Disguises
    4. Step 4: The Chickens Return Home to Roost
    5. Assigning Blame
    6. The Limits of Testing
  13. 9. Write Collapsible Code
    1. This Is What Failure Feels Like
    2. The Role of Short-Term Memory
    3. Where to Draw the Line
    4. The Cost of Abstraction
    5. Use Abstraction to Make Things Easier to Understand
    6. The Role of Long-Term Memory
    7. Common Knowledge Is Free; New Concepts Are Expensive
    8. Putting It All Together
  14. 10. Localize Complexity
    1. A Simple Example
    2. Hiding Internal Details
    3. Distributed State and Complexity
    4. Capacitated?
    5. Things Start to Get Foggy
    6. Rethinking the Approach
    7. Localized Complexity, Simple Interactions
  15. 11. Is It Twice as Good?
    1. Three Paths Forward: Ignore, Tweak, or Refactor
    2. Gradual Evolution Versus Continual Reinvention
    3. A Simple Rule of Thumb
    4. Dealing with Fuzzy Benefits
    5. Rework Is a Good Opportunity to Fix Small Problems
  16. 12. Big Teams Need Strong Conventions
    1. Formatting Conventions
    2. Language Usage Conventions
    3. Problem-Solving Conventions
    4. Effective Teams Think Alike
  17. 13. Find the Pebble That Started the Avalanche
    1. The Lifecycle of a Bug
    2. Minimizing State
    3. Dealing with Unavoidable State
    4. Dealing with Unavoidable Delay
  18. 14. Code Comes in Four Flavors
    1. Easy Problem, Simple Solution
    2. Easy Problem, Three Complicated Solutions
    3. The Cost of Complexity
    4. The Four (But Really Three) Kinds of Programmers
    5. Hard Problem, Somewhat Complicated Solutions That Don’t Work
    6. Hard Problem, Somewhat Complicated Solution
    7. Hard Problem, Simple Solution
  19. 15. Pull the Weeds
    1. Weed Identification
    2. How Code Gets Weedy
  20. 16. Work Backward from Your Result, Not Forward from Your Code
    1. An Example
    2. An Annoyance Appears
    3. Choosing a Side of the Gap
    4. Working Backward Instead
    5. And Now for Something Completely Different
    6. Working Forward and Working Backward
  21. 17. Sometimes the Bigger Problem Is Easier to Solve
    1. Jumping to Conclusions
    2. Finding a Clear Path Forward
    3. Recognizing the Opportunity
  22. 18. Let Your Code Tell Its Own Story
    1. Don’t Tell Stories That Aren’t True
    2. Make Sure There’s a Point to the Story
    3. Telling Good Stories
  23. 19. Rework in Parallel
    1. Bumps in the Road
    2. Build a Parallel System Instead
    3. A Concrete Example
    4. Stack Allocation in Practice
    5. A Cloud on the Horizon
    6. Making Stack Contexts a Little Smarter
    7. Migrating from Old Stack Contexts to New Ones
    8. Preparing to Migrate StackVector
    9. Time to Migrate
    10. Recognizing When Parallel Rework Is a Good Strategy
  24. 20. Do the Math
    1. To Automate or Not to Automate
    2. Look for Hard Limits
    3. When the Math Changes
    4. When the Math Problem Changes Back into a Word Problem
  25. 21. Sometimes You Just Need to Hammer the Nails
    1. A New Argument
    2. There’s Never Just One Bug
    3. The Siren Call of Automation
    4. Managing File Sizes
    5. There Are No Shortcuts
  26. Conclusion: Making the Rules Your Own
    1. Use Your Best Judgment
    2. Discuss Amongst Yourselves
    3. Signing Off
  27. A. Reading C++ for Python Programmers
    1. Types
    2. Formatting and Comments
      1. Comments
      2. Indentation and Split Lines
      3. Boolean Operations
      4. Lists
      5. Increment Operators
    3. Classes
    4. Visibility
    5. Declarations and Definitions
    6. Function Overloading
    7. Templates
    8. Pointers and References
  28. B. Reading C++ for JavaScript Programmers
    1. Types
    2. Arrays
    3. Classes
    4. Declarations and Definitions
    5. Function Overloading
    6. Templates
    7. Pointers and References
  29. Index
  30. About the Author

Product information

  • Title: The Rules of Programming
  • Author(s): Chris Zimmerman
  • Release date: December 2022
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9781098133115