This question is a clearer version of a question I posted on SO.
I have a C++ Planner object with a method that computes a Route from a start point to a destination point. Planner is the owner of the computed Route pointer.
Now, I have a second method (call it TestRoute), that takes the route object and tells if the destination is reachable from the start point, and if not, tries to find a route that passes through fuelling stations.
The original implementation of TestRoute was to return a Route pointer when the trip is feasible (with or without refuelling), and NULL in case of failure:
- if a refuelling is necessary,
TestRoutereturns a pointer to the new Route; - if no refuelling is necessary, it returns a pointer to a copy of the original Route, since its owner is
Planner.
In these both cases, it is up to the caller of TestRouteto destruct the returned Route object.
Well, now, a colleague of mine wants the method returns NULL when the route is feasible without refuelling, to avoid useless copy and destruction.
So the method would return:
- NULL in case of failure
- a pointer to a
Routein case of success when a newRouteis computed - NULL in case of success (!!) when no refuelling is necessary.
It sounds to me like a terrible design. May I have some opinion?
Planner... – Murphy Oct 25 '17 at 13:22LastErrorCodeis a function, this is a terrible design. You should avoid requiring callers to call yet another function to see if the previous function succeeded. If it's just a return value, that's far less objectionable. – user1118321 Nov 04 '17 at 03:09