|
1 | 1 | +++
|
2 |
| -title = "1-1-1 Rule" |
| 2 | +title = "1-1-1 Best Practice" |
3 | 3 | weight = 105
|
4 |
| -linkTitle = "1-1-1 Rule" |
| 4 | +linkTitle = "1-1-1 Best Practice" |
5 | 5 | description = "All proto definitions should have one top-level element and build target per file."
|
6 | 6 | type = "docs"
|
7 | 7 | +++
|
8 | 8 |
|
9 |
| -The 1-1-1 rule has the following elements: |
| 9 | +The 1-1-1 best practice is to keep every proto_library and .proto file as small |
| 10 | +as is reasonable, with the ideal being: |
10 | 11 |
|
11 |
| -* One `proto_library` rule |
| 12 | +* One `proto_library` build rule |
12 | 13 | * One source `.proto` file
|
13 | 14 | * One top-level entity (message, enum, or extension)
|
14 | 15 |
|
15 |
| -When defining a proto schema, you should have a single message, enum, extension, |
16 |
| -service, or group of cyclic dependencies per file. This makes refactoring |
17 |
| -easier. Moving files when they're separated is much easier than extracting |
18 |
| -messages from a file with other messages. Following this practice also helps to |
19 |
| -keep the proto schema files smaller, which enhances maintainability. |
| 16 | +Having the fewest number of message, enum, extension, and services as you |
| 17 | +reasonably can makes refactoring easier. Moving files when they're separated is |
| 18 | +much easier than extracting messages from a file with other messages. |
| 19 | + |
| 20 | +Following this practice can help build times and binary size by reducing the |
| 21 | +size of your transitive dependencies in practice: when some code only needs to |
| 22 | +use one enum, under a 1-1-1 design it can depend just on the .proto file that |
| 23 | +defines that enum and avoid incidentally pulling in a large set of transitive |
| 24 | +dependencies that may only be used by another message defined in the same file. |
| 25 | + |
| 26 | +There are cases where the 1-1-1 ideal is not possible (circular dependencies), |
| 27 | +not ideal (extremely conceptually coupled messages which have readability |
| 28 | +benefits by being co-located), or where the some of the downsides don't apply |
| 29 | +(when a .proto file has no imports, then there are no technical concerns about |
| 30 | +the size of transitive dependencies). As with any best practice, use good |
| 31 | +judgment for when to diverge from the guideline. |
20 | 32 |
|
21 | 33 | One place that modularity of proto schema files is important is when creating
|
22 | 34 | gRPC
|
|
0 commit comments