protocolbuffers / protobuf

Protocol Buffers - Google's data interchange format
http://protobuf.dev
Other
64.55k stars 15.37k forks source link

Add best practice/guidance around the usage of optional for proto3 #17226

Open julianskartor opened 2 weeks ago

julianskartor commented 2 weeks ago

Please let me know if I've missed something, but I can't find any clear and decisive guidance around the usage of optional since it was added to proto3.

The relevant stackoverflow I have found is not clear (one answer says it's fine and the other says use only when needed) and in general seems to have had little activity.

Google has some guidelines around this but is not very good at explaining their rationale and it is unclear if it applies in general (i.e. not only at "Google level" of scale).

zhangskz commented 1 week ago

Protobuf team's stance is that most proto3 users should prefer to use the optional keyword. Editions 2023 uses EXPLICIT presence (what you get in proto3 with the optional keyword) as the default which unifies proto2 + proto3. We do have some documentation for this feature for editions in https://protobuf.dev/editions/features/#field_presence.

@Logofile We may want to document our recommendation in https://protobuf.dev/programming-guides/field_presence/#presence-in-proto3-apis as well.

There has been opposing guidance from Cloud in the past to prefer to avoid optional unless needed. We've chatted with them a while back on whether this should be updated, which I can follow up on.

julianskartor commented 1 week ago

Thank you for the reply @zhangskz!