Book Deep-Read · Software Engineering at Google · Chapter 8

Style Guides and Rules

Software Engineering at Google · Ch 8 · Style Guides and Rules · Shaindel Schwartz · 2020

中文 →

What is this chapter about?

Google has tens of thousands of engineers, nearly all writing code in one giant shared repository. Everyone has habits: how far to indent, how to name things, which fancy tricks to reach for. This chapter is about how Google sets the "house rules" for writing code — and who those rules actually serve.

An analogy

Think of a city's traffic code. "Drive on the right" is not smarter than "drive on the left" — the British manage fine. What matters is that everyone in the city does the same thing, so nobody crashes. Other rules really do prevent danger, like a lower speed limit outside schools. And some are just advice, like "turn on your headlights in the rain": no fine, but experienced drivers do it anyway.

A strange thing first

Many of Google's rules forbid a "cleverer" way of writing something, for one reason only: the reader won't follow it. The writer wants a neat shortcut; the rule says no — because code is written once but read by dozens of people for years.

How it works, intuitively

What it buys you / how to choose

Should your team have coding rules? Look at how many people there are and how long the code must live. A handful of people on a small project can get by with a verbal agreement; dozens of people maintaining code for years should write the rules down and let tools enforce as much as possible. The honest cost: rules always flatten some personal preferences and locally better choices, and too many of them turns into bureaucracy.

Remember it in one line

Rules aren't about looking pretty — they make code written by thousands of people read as if one person wrote it.

Want the five principles for making rules, the three kinds of rules, where rules end and guidance begins, how gofmt ended formatting wars, and the real reason Google's C++ avoids exceptions? → Switch to the deep read