This isn’t a very math-y book. Sure, numbers pop up in a few of the Rules (like Rule 4, “Generalization Takes Three Examples” , and Rule 11, “Is It Twice as Good?” ), but the Rules are more concepts than equations.

It’s sort of surprising that there isn’t more math in computer programming. Computers are just number processing machines, after all. Everything is reduced to numbers for processing—words are sequences of characters represented as numbers, bitmaps are pixels represented by colors represented by numbers, music is a pair of waveforms represented as a series of numbers. You’d think that some of that would leak through—that, as a programmer, you’d be figuring equations at some point. That doesn’t happen very often, though.

Most of the decisions a programmer makes are squidgy. Deciding whether the clarity a lengthy comment would add is worth complicating the flow of logic, say. Choosing between getPriority or calculatePriority as the name for a function. 1 Identifying the right time to switch over to a new version of some system.

It’s easy to fall into thinking that all decisions are squidgy, not just most decisions. Some decisions boil down to simple math, though, and you need to recognize them when they pop up. If you don’t, if you forge ahead without doing the simple math, you may be in for a painful realization later on. You may discover that the approach you’ve followed was never going to work, and that you could have saved yourself a lot of time by just doing the math. This will make you sad; better to do the math up front.

To Automate or Not to Automate

Here’s a common scenario. You’ve got some process you’ve been doing by hand, and you’re thinking about automating it. Is the automation work worth doing?

That’s just a math problem! If you’ll spend less time writing the code than you’ll spend repeating the task by hand, then it’s worth doing. If not, then it isn’t.

This may seem obvious, and it is, but that doesn’t mean the math gets done.

I’ve seen this math get skipped way more times than I’ve seen it done. Let me spin a typical counter-example—a programmer gets annoyed by some manual process, immediately dives into a two-day project to automate it, then congratulates themselves every time they run the resulting macro. Which they do maybe once a week, saving 15 seconds each time.

The two-day automation project may have been fun, but it wasn’t justifiable, and doing the math before starting would have made this clear. Look, we’re programmers because we like to program. We’re going to have a bias toward solving problems by programming—but programming isn’t the right solution to every problem.

Deciding whether to automate some task is an optimization problem—you’re just optimizing the work process rather than the program execution. You apply the same steps as with any optimization, including the absolute need to measure the process before trying to optimize it.

Let’s look at a concrete automation scenario. Imagine you’re writing a book about programming. You’re using Visual Studio to edit all of your coding examples, but you’re using Word to write the book. The code examples are indented in the source file, but shouldn’t be indented in the book. 2 Your manual process is simple and pretty fast:

  1. Select a code block in your editor.

  2. Unindent it.

  3. Copy it to the clipboard.

  4. Undo the unindentation.

  5. Switch to Word.

  6. Create a paragraph with the right style.

  7. Paste the code example into the paragraph.

Is this worth automating? Will you save time overall by automating this operation? Time to do the math.

There are two sides to the math here: the cost side and the benefit side. The cost is how much work will be required to implement the automation. The benefit is how much time you’ll save once the task is automated.

In this concrete scenario, some of the steps will still remain even after automation. You’ll still switch to your code editor to select the code block, and you’ll still switch back to Word to paste it in. When you’re doing the math for the scenario you can ignore the steps that don’t change, since you’re only interested in the time difference pre- and post-automation. Everything else can be automated, and once automated will take effectively zero time.

You can’t do the math without numbers. Where possible, use real numbers rather than estimates. That means measuring the things that can be measured—in this case, how long the manual process takes. So time it—let’s say it takes 6 seconds. 3 You look at the chapters you’ve written and they average 8 code samples, so 8 code samples per chapter goes into the math. Your publishing contract calls for 20 or so chapters, so that’s the number to use. You’ve also noted that it’s common to revise code samples, and this means cutting and pasting them more than once. You think that on average each example is pasted 3 times; that’s the one estimate in all of this.

That’s enough to do the benefit side of the math:

6 seconds (per copy operation) ×

8 (code samples per chapter) ×

20 (chapters) ×

3 (revisions of each sample) = 48 minutes

OK, so that’s the benefit side of things. On to the cost side.

How long will it take to automate the process? It’s not easy to automate things in Visual Studio, at least as it comes out of the box, but Word is surprisingly automatable. If you’ve written Word macros before, and especially if you’ve written code that manipulates the clipboard, then you’ve got the basics covered. There’s just some text cleanup to do on top of the clipboard manipulation.

And that cleanup doesn’t seem bad. Pull the contents of the clipboard into an array of strings, one per line of text. Detect the minimum amount of indentation in any string, then rebuild the array subtracting out that indentation. You’ll probably need to think about how blank lines affect things, and also should consider that spaces and tabs look pretty much the same in the text editor but pretty different in Word. And after cleaning up the lines you have to reassemble them into a text block and insert it in the document, then bind your new macro to a hot key.

Let’s say your estimate is an hour to get this all working correctly. 4

An hour is more than 48 minutes, so the math tells us to not do the automation. But it seems close; maybe the estimates on the benefit side were a little off. Maybe it’s an average of 4 revisions per sample instead of 3. That would be enough to push the math positive—if it’s 4 revisions per sample instead of 3, the math says go ahead. And the fact is, it is really annoying to do the steps by hand, even if it’s only taking 6 seconds a pop.

Hold on there, cowpoke. Which do you think is more likely—that you were a little too pessimistic on your estimate of the benefits, or a little too optimistic on how much time it was going to take to get the code working? You’re a programmer, you know the answer to this question. You’re much more likely to miss your estimate on coding time .

If the cost-benefit math for automation looks like a toss-up, then don’t do it.

Look for Hard Limits

If you’ve got hard limits in your problem space or on your solution, then you should respect them from the start of your design process.

One of the nice things about creating games for video game consoles is that they present plenty of hard limits. Like the amount of memory in a console—that’s fixed. The number of bytes you can pack on a Blu-ray disk is fixed. The size of a UDP networking packet is fixed. Each frame is a sixtieth of a second, full stop.

Our team will also invent hard limits to clarify our technical design process. Take available network bandwidth, for example. It varies from customer to customer, and it can be unpredictable, but we have pretty reliable numbers on measured network bandwidth for customers around the world. We can invent a hard limit on network bandwidth that covers nearly all of our customers; if we stay within that invented hard limit, the game will run well for almost everyone. 5

It may seem strange to rejoice in hard caps like this. Why would having hard limits be something to celebrate?

Take the hard limit on memory that we work with when doing console programming. This seems like a bad thing—wouldn’t virtual memory make programming easier? The answer is yes, of course, but it’s at the cost of turning a hard limit on memory into a softer limit. If you overflow available physical memory, virtual memory swaps pages out to disk, trading time for space. That’s a problem for a video game. Updating the screen once every couple of seconds when virtual memory starts thrashing isn’t acceptable; we have a hard limit of a sixtieth of a second for each frame. In the end it’s simpler to accept a hard limit on memory.

So we identify the hard limits that exist, and invent hard limits out of softer limits to simplify our design decisions. That’s true for the coding team, but even more so for all of the Sucker Punchers who aren’t coders. Trade-offs and soft limits are really hard for people to wrap their heads around. Hard limits are easier—they turn some parts of the design process into simple math, and that’s easy to do. 6

Consider a network protocol design example. The basic networking design is fixed—you’re writing a peer-to-peer game, so every connected machine is communicating directly with the other connected machines. Each machine is the “authority” for a subset of the characters in the game, and is responsible for broadcasting to the other machines the state of those characters. The hard limits you need to respect are 1 Mbps network bandwidth received and 256 Kbps bandwidth sent—if you stay within those bounds, nearly all players will get good performance. You need to support four connected players.

The design you’re considering is for each machine to broadcast in UDP packets the position and orientation of each character it has authority over each frame, plus enough information to reconstruct the animations currently applied to that character. In combination, that’s enough to position and pose the character on the other machines. If packets get dropped, it’s not that big a deal, since you’re sending information about each character 60 times a second.

This is just another math problem! You’ve got a hard limit on network bandwidth that has to be respected, so you need to figure out how much data your design would send each second. That means measuring wherever possible, and estimating where a measurement can’t be made.

In the simplest possible version of this design, you’d use the native representation for the things you’re going to network. Internally, character positions are a vector of three 32-bit floating point numbers, and the character orientation can be boiled down to a compass heading also represented in floating point. That covers position and orientation, leaving the information necessary to reconstruct animations in the remote machine.

Luckily, you’ve got a single-player version of the game that you can use for measurement, and you discover that on average each character blends together the effects of six animations. You’ll need to send an animation count, plus enough data to reconstruct each of the active animations. That means identifying the animation—internally, you do that with a unique 8-byte identifier. You’ll also need to capture where you are in the timeline for the animation, internally a 4-byte floating-point number, and any factor used to blend between the results of two animations, also represented in floating point.

The math for each character is clear now, in this simple version of the design. Your floating-point values are all stored in 4 bytes, and you use 4-byte integers for counts by default.

Position is 12 bytes, plus 4 bytes for orientation, plus 4 bytes for an animation count. Each animation is an 8-byte identifier, then two 4-byte floating-point values for the timeline and blend factor. That’s 12 + 4 + 4 + 6 × (8 + 4 + 4) = 116 bytes per character, which doesn’t seem too bad.

There’s more math, though. You broadcast information about each character once per frame, so you need to multiply by 60 to calculate how many bytes per second of bandwidth you’re using.

Your peer-to-peer architecture means that you send out three copies of the character’s data each frame, too—one copy to each of the three peers. You also receive data from each of the three peers about the characters they have authority over. The worst case with this design is when a machine ends up with authority over all the characters—that machine then sends out three copies for all characters, and receives copies for none.

You’ve got one more fixed point—the number of characters you need to handle, which your game design team has decided is 30. Now you’ve got enough numbers to do the math:

30 (frames per second) ×

3 (copies to our peers) ×

30 (characters) ×

116 (bytes per character) ×

8 (bits per byte) = 2.5 Mbps

Uh oh. That’s 10 times your available bandwidth on bits sent. Doing the math let you know that the simple design you had in mind couldn’t work. Actually, the dangerous part is that the design will work fine on your internal 1G network, where it won’t even cause a ripple in available bandwidth. You’ll only discover the problem when you deploy to the field. Yikes.

It’s a little bit tough to salvage this simple design.

There’s lots of room to compress the data sent about each character, so that’s a place to start. Maybe 16 bits is enough for each of the coordinates, given that multiplayer areas are small, and 8 bits should be enough detail for heading. Creating a table of all networkable animation names would mean that 10 bits would be enough to identify an animation, and each animation could write its own networked state, which is more compressible than the raw blend weights and time values you sent. Throwing all of these compression tricks at the problem, you squeeze the bytes per character down to 16 instead of 116.

The math still doesn’t work out:

30 (frames per second) ×

3 (copies to our peers) ×

30 (characters) ×

16 (bytes per character) ×

8 (bits per byte) = 345 Kbps

Much closer, but still above the hard limit. Something’s going to have to give—maybe the design team can be persuaded that 24 characters is enough. On the technical side, maybe you could get away with sending data about characters every other frame, rather than every frame. Either of these changes would get the design safely under the hard cap. 7

Crucially, the math happens before implementation starts. The math told you that the initial design couldn’t work. It’s a lot easier to switch to the design where the math pencils out before all the code is written. Once all the multiplayer content is built, it’s going to be a lot harder convincing the design team to reduce the maximum character count to 24!

It’s important to note that doing the math is designed to identify solutions that won’t work, not to necessarily verify that a solution will work. This simple network design could fall apart for any number of other reasons—but at least it won’t fail due to basic math .

When the Math Changes

Let’s go back to the first example, where you had to decide whether to automate a manual process to cut and paste code samples from Visual Studio into Word. The process focused on normalizing the indentation of the code samples, and the math told you that automation wasn’t worth doing.

Imagine, then, that your first understanding of the problem was incomplete. Normalizing the indentation isn’t enough. All of the tabs in the code samples need to be converted to spaces, too, because that’s how the publisher lays out books.

Does the original math still apply? Not anymore—because the manual process you measured doesn’t match the new requirement. You’ll need to tweak the manual process—say, by finding a Visual Studio plug-in that converts tabs to spaces, 8 then adding an extra step to trigger this plug-in on the selection, then adding an extra undo step—then remeasure.

That tweak affects both halves of the math. The extra two steps—a tabs-to-spaces conversion, and an extra undo—will slow down the manual process. Maybe each run through the cut-and-paste process takes 10 seconds now instead of 6, and that increases the benefit side of the equation.

The tweak also affected the cost side of the manual process, too, because now you’re spending time to find and install the right extension, then spending time experimenting to understand exactly how it works. Your process depends mightily on how the new plug-in interacts with the undo stack, for instance. It’s only fair to add this to the cost side of the math, since the time you spend finagling the extension could have been spent on your automation effort instead.

If you do the math again with these two adjustments and some new estimates, the balance changes, first for the manual process:

10 seconds (per copy operation) ×

8 (code samples per chapter) ×

20 (chapters) ×

3 (revisions of each sample) = 80 minutes

If you add in 45 minutes to research an appropriate tabs-to-spaces plug-in, including installation and experimentation, and bump up your estimate of automation to 90 minutes to include the extra work of doing tabs-to-spaces conversion, the math changes:

80 minutes + 45 minutes (manual process) > 90 minutes (automated process)

Now the math tells you to do the automation. You could still cut and paste code samples with a manual process, but the process will be slower and will take time to figure out . Better to just automate it. 9

When the Math Problem Changes Back into a Word Problem

If you take this chapter to heart, you’ll do a better job identifying problems that imply a little bit of math. Quantifiable constraints and measurable solutions are the cue—when you see both of them, you should do the math to help identify solutions that will never work.

But be wary that there aren’t qualitative problems lurking in all of the quantitative analysis! Take task automation: it’s not always as simple as just doing the math.

Your primary goal when automating a task is to reduce the total time spent…but that may not be your only goal. The manual process might be error-prone, for instance. Maybe you could quantify how often errors occur, and how much time they take to fix, but these are tough things to be crisp about.

Or maybe a manual task that should be completed daily is so annoying that it’s only done weekly. It doesn’t make sense to focus solely on the time spent on the task. If automation ensures that the task is done daily, then it might be worth doing, despite the amount of time saved being small.

And it’s not unreasonable to consider team sanity as a soft goal. A manual task may not be all that time consuming, but if it’s a constant irritant and it can be fixed relatively easily, it might be worth doing even if the math doesn’t quite work out. Don’t be afraid to spend a day now and then just to make everyone’s lives more pleasant—especially when the math is a close call anyhow.

On the flip side, if you don’t deeply understand a task, then be wary of automating it even if the numbers look good! In the preceding examples, I was automating one of my own tasks. I knew all of its ins and outs. If it had been someone else’s job to cut and paste code examples into this book, things would have been a lot fuzzier. I wouldn’t have been sure I knew what the right approach to automation was, much less that I’d got the math right .

Fundamentally, though, trust the numbers. If there’s quick arithmetic you can do that verifies the basic sanity of the problem-solving approach you’re considering, then do the math.

1 It will not surprise you to learn that we have a convention for this at Sucker Punch: “get” implies that no (or very little) calculation is done, while “calculate” or something similar implies that work is involved to produce the value. It’s a nice head start on understanding what a function does.

2 Ahem. This is not a hard scenario for me to imagine.

3 Because that’s how long the manual process took when I timed it.

4 An optimistic estimate, in my opinion.

5 Not everywhere, though. McMurdo Station in Antarctica shares 25 Mbps of bandwidth between a thousand people in the summer. That’s less than our hard cap; sorry, scientists. Also it might make sense to cut back on the Netflix, scientists; sorry about that too. Keep up the good work, though.

6 Well, it’s easy for the coders to do the math, at least. And easy for the coders to explain to anyone who doesn’t want to do the math.

7 Actually the easiest fix is to miss the ship date for your game by a few years and hope that your customers’ internet connections get faster. You’d be surprised at how often this turns out to be the eventual fix for a performance problem.

8 Because the alternative of switching from using tabs to using spaces in your code is completely unacceptable, naturally. We all have our foibles.

9 I had an excellent time writing the resulting Word macro, by the way. Word macros are written in Visual Basic for Applications, and Basic was the first programming language I learned. Good times.