Martin G
Martin G

Reputation: 18109

Can Message::GetDescriptor() return null?

I'm always checking the return value of Message::GetDescriptor() before using it, but when would it ever return null? Is it perhaps unnecessary to check the return value?

The docs:

https://developers.google.com/protocol-buffers/docs/reference/cpp/google.protobuf.message#Message.GetDescriptor.details

Declaration:

const Descriptor * 
    Message::GetDescriptor() const

Upvotes: 0

Views: 436

Answers (2)

Zakir
Zakir

Reputation: 2382

You should always check return type of possibly every API that you code invokes, and should never make any kind of assumption however reliable the API may be. API s fail for a variety of reasons beyond human control:-

  • Network condition fluctuations including PHY disruption (not applicable in this case)
  • System running the implementation of the API running Out of Resource like space
  • System overload (too busy with other processes)
  • Unreliable API implementation (bug) etc..

Since the API is from Google making a naive assumption that the 4th reason can never be true simply reduces the robustness of your software. For 99.99% of times it might just seem to be a redundant check or an over protective code - but for that 0.01% times when it fails you have unreliable behavior from your software

The costliest bugs that could have easily been avoided (if not fixed), from my experience over the years, are a result overlooking simple and basic error handling

Upvotes: 1

Or Davidi
Or Davidi

Reputation: 109

You don't have to check it, for each message you should get a non NULL pointer.

Upvotes: 0

Related Questions