Hi,
I'm designing API for my project and I've been thinking last hours which solution is better.
There'll be several methods where I'll need to be able to pass null pointer or return null pointer (maybe even reassign pointer). I could: 1)use pointers where I need them and use references in other cases. 2)use strictly only pointers everywhere to avoid make mess in API with mixture of pointers and references.
What would you prefer?
I personally prefer second solution but maybe my view is twisted.
You forgot option 3: Use only pointers and verify invalid parameters as necessary.
I'd go with that one.
However, what you could do if you prefer references is return bool from the functions that would otherwise return invalid pointers. The bool tells the user whether the output parameter is valid or invalid.
For example: bool divide(double &result,double a,double b);
As for the functions that can take null pointers, you'll have to either take advantage of function overloading, or give different names to the different versions when that's impossible.
My personal opinion is that as the user of your library, every function call I make into your library that can fail (
return a "failure condition", not crash) is an extra error case I have to think about at my application level. The
fewer error cases I have to deal with, the better, as it allows me to concentrate more on the success cases.
Therefore I'd go with #1. Why introduce error cases when there is no need?