1

Since proto3, the protobuf spec got rid of the optional and required fields. Now the only way to find out if a field in the message is optional only by looking at the docs, which is not very convenient. How about we use optional_ as the prefix on the field name for optional fields.

Without optional_ prefix:

Protobuf
message User {
    string user_id = 1;
    string name = 2;
    string email = 3;
}
Code
# Don't know which fields are optional, we have to check the docs:
user.user_id = 1
user.name = "bob"
user.email = "bob@gmail.com"

With optional_ prefix:

Protobuf
message User {
    string user_id = 1;
    string optional_name = 2;
    string optional_email = 3;
}
Code
# it is more informative but doesn't read naturally in English.
user.user_id = 1
user.optional_name = "bob"
user.optional_email = "bob@gmail.com"

I have not usually seen optional fields named like that. It was used in one of my past teams and it was very convenient. Do you have any particular opinion on this?

Deepak gupta
  • 109
  • 1
  • 5
  • Honestly, "I need to look at the docs to use an API for a remote service" sounds like a win to me... but: this is entirely subjective. Do what works for you and your team. – Marc Gravell Apr 09 '21 at 14:36
  • Looking at the docs to be able to understand semantics of the remote service call sounds great but this is more on just the field level where I have to go check for each field if it's optional. Imagine a protobuf with tens of fields. Not only the author but also the reader of the code who is reading on Github. – Deepak gupta Apr 09 '21 at 14:49
  • well, obviously they can just point to their working integration tests that show the service working on the test account... – Marc Gravell Apr 09 '21 at 14:51
  • Do you expect all authors (while modifying the code) and readers (trying to understand the code) of the consumer code to read integration tests of the remote service? – Deepak gupta Apr 09 '21 at 15:49
  • nope; but I *would* expect the integration tests to *get run at some point*, whether that is manual or via CI, to indicate unanticipated failures; the same as anything else – Marc Gravell Apr 09 '21 at 15:58

0 Answers0