mercredi 25 octobre 2017

Return NULL in case of success

I just had an intense discussion with a colleague of mine about a question of design, and would appreciate some opinion.

We have a method that takes an argument and that:

  • can fail, returning NULL
  • can succeed in two forms:
    • a solution is not found "immediately" (what's behind this is not important) and the method returns a modified version of the argument
    • a solution is found "immediately" and the argument needs not be modified.

In that last case, I would return the unmodified object. So in case of failure, the method returns NULL, and in case of success, it always returns an object (we have a mean to test if it has been modified or not), that has to be destructed by the caller (we are in C++).

My colleague recommends to return NULL in the case of the method succeeded without modifying the object, to avoid a copy and a useless destruction.

But a method that returns NULL in case of success in some case sounds like a terrible design to me.

What are the recommendations in this case?

Aucun commentaire:

Enregistrer un commentaire