Protobuf and optional values

Just small note to all you guys if you are using google protobuf library for communicating with server.

Lets say you have simple protobuf message with one optional numeric field:

If you are using some dynamic language in your server backend, you can end up having code like this:

Now question, what value of limit variable will be, if message will not have limit field set? Answer is: 0. Not null.

This is happening because actual return value for getLimit() is primitive long, not object. I assume google guys did that for performance reasons.

You can easily solve it using ?: operator:

Summary: working with protobuf, always remember that it is using primitives for numeric types and you have to use has methods to avoid getting zeros instead of nulls.