Skip to content

Commit 5f3c644

Browse files
Add Best Practice to prefer extensions to Any
PiperOrigin-RevId: 882640183
1 parent fcb43e2 commit 5f3c644

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

content/best-practices/dos-donts.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,23 @@ in its headers such as `math.h`, may cause compilation errors if the `#include`
129129
statement for one of those headers appears before the one for `.proto.h`. Avoid
130130
using macro constants such as "`NULL`," "`NAN`," and "`DOMAIN`" as enum values.
131131

132+
<a id="dont-use-any">
133+
134+
## **Do** prefer extensions over `Any` where possible {#avoid-any}
135+
136+
If you plan to use `Any`, first double-check if extensions can satisfy the use
137+
case instead. If yes, prefer using extensions.
138+
139+
`Any` was created in Proto3 to replace extensions. However, it has a number of
140+
design flaws. For most use cases, prefer using extensions.
141+
142+
The main use case that `Any` fulfills is the uncommon scenario in which
143+
horizontal infrastructure needs to propagate completely arbitrary messages, and
144+
declaring an extension for each possible legal types would be infeasible.
145+
146+
In the future, Protobuf may introduce a new concept that would satisfy the `Any`
147+
use cases without the design flaws, but we have no concrete plans at this time.
148+
132149
<a id="do-use-well-known-types-and-common-types"></a>
133150

134151
## **Do** Use Well-Known Types and Common Types {#well-known-common}

0 commit comments

Comments
 (0)