@@ -10,23 +10,29 @@ try to update them at the same time. One or the
1010other may get rolled back. Don’t assume that you can make a breaking change and
1111it'll be okay because the client and server are in sync.
1212
13- ## ** Don't** Re-use a Tag Number
13+ <a id =" dont-re-use-a-tag-number " ></a >
14+
15+ ## ** Don't** Re-use a Tag Number {#reuse-number}
1416
1517Never re-use a tag number. It messes up deserialization. Even if you think no
1618one is using the field, don’t re-use a tag number. If the change was live ever,
1719there could be serialized versions of your proto in a log
1820somewhere. Or there could be old code in another server that will break.
1921
20- ## ** Don't** Change the Type of a Field
22+ <a id =" dont-change-the-type-of-a-field " ></a >
23+
24+ ## ** Don't** Change the Type of a Field {#change-type}
2125
2226Almost never change the type of a field; it'll mess up deserialization, same as
2327re-using a tag number. The
24- [ protobuf docs] ( /programming-guides/proto #updating )
28+ [ protobuf docs] ( /programming-guides/proto2 #updating )
2529outline a small number of cases that are okay (for example, going between
2630` int32 ` , ` uint32 ` , ` int64 ` and ` bool ` ). However, changing a field’s message type
2731** will break** unless the new message is a superset of the old one.
2832
29- ## ** Don't** Add a Required Field
33+ <a id =" dont-add-a-required-field " ></a >
34+
35+ ## ** Don't** Add a Required Field {#add-required}
3036
3137Never add a required field, instead add ` // required ` to document the API
3238contract. Required fields are considered harmful by so many they were
@@ -36,7 +42,9 @@ and whether someone will be forced to fill in your required field with an empty
3642string or zero in four years when it’s no longer logically required but the
3743proto still says it is.
3844
39- ## ** Don't** Make a Message with Lots of Fields
45+ <a id =" dont-make-a-message-with-lots-of-fields " ></a >
46+
47+ ## ** Don't** Make a Message with Lots of Fields {#lots-of-fields}
4048
4149Don't make a message with “lots” (think: hundreds) of fields. In C++ every field
4250adds roughly 65 bits to the in-memory object size whether it’s populated or not
@@ -46,7 +54,9 @@ grows too large, the generated code may not even compile (for example, in Java
4654there is a hard limit on the size of a method
4755).
4856
49- ## ** Do** Include an Unspecified Value in an Enum
57+ <a id =" do-include-an-unspecified-value-in-an-enum " ></a >
58+
59+ ## ** Do** Include an Unspecified Value in an Enum {#unspecified-enum}
5060
5161Enums should include a default ` FOO_UNSPECIFIED ` value as the first value in the
5262declaration . When new values
@@ -64,22 +74,28 @@ less code. Note that [proto enums][proto-enums] require the first value to be
6474zero and can round-trip (deserialize, serialize) an unknown enum value.
6575
6676[ example-unspecified ] : http://cs/#search/&q=file:proto%20%22_UNSPECIFIED%20=%200%22&type=cs
67- [ proto-enums ] : /programming-guides/proto #enum
77+ [ proto-enums ] : /programming-guides/proto2 #enum
6878
69- ## ** Don't** Use C/C++ Macro Constants for Enum Values
79+ <a id =" dont-use-cc-macro-constants-for-enum-values " ></a >
80+
81+ ## ** Don't** Use C/C++ Macro Constants for Enum Values {#macro-constants}
7082
7183Using words that have already been defined by the C++ language - specifically,
7284in its headers such as ` math.h ` , may cause compilation errors if the ` #include `
7385statement for one of those headers appears before the one for ` .proto.h ` . Avoid
7486using macro constants such as "` NULL ` ," "` NAN ` ," and "` DOMAIN ` " as enum values.
7587
76- ## ** Do** Use Well-Known Types and Common Types
88+ <a id =" do-use-well-known-types-and-common-types " ></a >
89+
90+ ## ** Do** Use Well-Known Types and Common Types {#well-known-common}
7791
7892Embedding the following common, shared types is strongly encouraged. Do not use
7993` int32 timestamp_seconds_since_epoch ` or ` int64 timeout_millis ` in your code
8094when a perfectly suitable common type already exists!
8195
82- ### Well-Known Types:
96+ <a id =" well-known-types " ></a >
97+
98+ ### Well-Known Types {#well-known}
8399
84100* [ duration] ( https://github.com/protocolbuffers/protobuf/blob/main/src/google/protobuf/duration.proto )
85101 is a signed, fixed-length span of time (for example, 42s).
@@ -89,7 +105,9 @@ when a perfectly suitable common type already exists!
89105* [ field_mask] ( https://github.com/protocolbuffers/protobuf/blob/main/src/google/protobuf/field_mask.proto )
90106 is a set of symbolic field paths (for example, f.b.d).
91107
92- ### Common Types:
108+ <a id =" common-types " ></a >
109+
110+ ### Common Types {#common}
93111
94112* [ interval] ( https://github.com/googleapis/googleapis/blob/master/google/type/interval.proto )
95113 is a time interval independent of time zone or calendar (for example,
@@ -113,36 +131,46 @@ when a perfectly suitable common type already exists!
113131* [ month] ( https://github.com/googleapis/googleapis/blob/master/google/type/month.proto )
114132 is a month of year (for example, April).
115133
116- ## ** Do** Define Widely-used Message Types in Separate Files
134+ <a id =" do-define-widely-used-message-types-in-separate-files " ></a >
135+
136+ ## ** Do** Define Widely-used Message Types in Separate Files {#separate-files}
117137
118138If you're defining message types or enums that you hope/fear/expect to be widely
119139used outside your immediate team, consider putting them in their own file with
120140no dependencies. Then it's easy for anyone to use those types without
121141introducing the transitive dependencies in your other proto files.
122142
123- ## ** Do** Reserve Tag Numbers for Deleted Fields
143+ <a id =" do-reserve-tag-numbers-for-deleted-fields " ></a >
144+
145+ ## ** Do** Reserve Tag Numbers for Deleted Fields {#reserve-tag-numbers}
124146
125147When you delete a field that's no longer used, reserve its tag number so that no
126148one accidentally re-uses it in the future. Just ` reserved 2, 3; ` is enough. No
127149type required (lets you trim dependencies!). You can also reserve names to avoid
128150recycling now-deleted field names: ` reserved "foo", "bar"; ` .
129151
130- ## ** Do** Reserve Numbers for Deleted Enum Values
152+ <a id =" do-reserve-numbers-for-deleted-enum-values " ></a >
153+
154+ ## ** Do** Reserve Numbers for Deleted Enum Values {#reserve-deleted-numbers}
131155
132156When you delete an enum value that's no longer used, reserve its number so that
133157no one accidentally re-uses it in the future. Just ` reserved 2, 3; ` is enough.
134158You can also reserve names to avoid recycling now-deleted value names: `reserved
135159"FOO", "BAR";`.
136160
137- ## ** Don't** Change the Default Value of a Field
161+ <a id =" dont-change-the-default-value-of-a-field " ></a >
162+
163+ ## ** Don't** Change the Default Value of a Field {#change-default-value}
138164
139165Almost never change the default value of a proto field. This causes version skew
140166between clients and servers. A client reading an unset value will see a
141167different result than a server reading the same unset value when their builds
142168straddle the proto change. Proto3 removed the ability to
143169set default values.
144170
145- ## ** Don't** Go from Repeated to Scalar
171+ <a id =" dont-go-from-repeated-to-scalar " ></a >
172+
173+ ## ** Don't** Go from Repeated to Scalar {#repeated-to-scalar}
146174
147175Although it won't cause crashes, you'll lose data. For JSON, a mismatch in
148176repeatedness will lose the whole * message* . For numeric proto3 fields and proto2
@@ -154,7 +182,9 @@ Going from scalar to repeated is OK in proto2 and in proto3 with
154182` [packed=false] ` because for binary serialization the scalar value becomes a
155183one-element list .
156184
157- ## ** Do** Follow the Style Guide for Generated Code
185+ <a id =" do-follow-the-style-guide-for-generated-code " ></a >
186+
187+ ## ** Do** Follow the Style Guide for Generated Code {#follow-style-guide}
158188
159189Proto generated code is referred to in normal code. Ensure that options in
160190` .proto ` file do not result in generation of code which violate the style guide.
@@ -171,7 +201,9 @@ For example:
171201 https://google.github.io/styleguide/cppguide.html#Namespace_Names .
172202 If these style guides conflict, use ` java_package ` for Java.
173203
174- ## ** Don't** use Text Format Messages for Interchange
204+ <a id =" never-use-text-format-messages-for-interchange " ></a >
205+
206+ ## ** Don't** use Text Format Messages for Interchange {#text-format-interchange}
175207
176208Text-based serialization formats like text format and
177209JSON represent fields and enum values as strings. As a result, deserialization
@@ -183,13 +215,17 @@ for human editing and debugging only.
183215If you use protos converted to JSON in your API or for storing data, you may not
184216be able to safely rename fields or enums at all.
185217
186- ## ** Never** Rely on Serialization Stability Across Builds
218+ <a id =" never-rely-on-serialization-stability-across-builds " ></a >
219+
220+ ## ** Never** Rely on Serialization Stability Across Builds {#serialization-stability}
187221
188222The stability of proto serialization is not guaranteed across binaries or across
189223builds of the same binary. Do not rely on it when, for example, building cache
190224keys.
191225
192- ## ** Don't** Generate Java Protos in the Same Java Package as Other Code
226+ <a id =" dont-generate-java-protos-in-the-same-java-package-as-other-code " ></a >
227+
228+ ## ** Don't** Generate Java Protos in the Same Java Package as Other Code {#generate-java-protos}
193229
194230Generate Java proto sources into a separate package from your hand-written Java
195231sources. The ` package ` , ` java_package ` and ` java_alt_api_package ` options
@@ -200,9 +236,9 @@ A common practice is to generate your protos into a `proto` subpackage in your
200236project that ** only** contains those protos (that is, no hand-written source
201237code).
202238
203- ## Appendix
239+ ## Appendix {#appendix}
204240
205- ### API Best Practices
241+ ### API Best Practices {#api-best-practices}
206242
207243This document lists only changes that are extremely likely to cause breakage.
208244For higher-level guidance on how to craft proto APIs that grow gracefully see
0 commit comments