Skip to main content
gamedev.net
🔒 Locked

Returning pointers

Started by -Datriot- Apr 13, 2009 at 4:35 AM 6 replies 1.4k views
Original Post
-Datriot-
-Datriot-
Let's say I have a method like GetShader() or GetObject() which return pointers to types Shader and Object. If I were to pass an ID into the method, but the method can't find an object with the given ID, should I return NULL? Returning NULL is pretty much asking the program to crash since the rest of the program has no way of knowing whether or not the pointer returned is valid. Any suggesstions on how to handle such a situation elegantly?
rip-off
rip-off
The program can easily test the pointer for validity: compare with NULL:
Shader *shader = object.GetShader();if(shader == NULL){    // error handling}else{    // use *shader}

Here is an important question: is failing to find an ID likely to happen, or is it an exceptional event?
DevFred
DevFred
Or even more concise:

if(Shader *shader = object.GetShader()){    // use *shader}else{    // error handling}
Hybrid666
Hybrid666
Hi,

If returning NULL is an acceptable output from the function, (e.g. you're trying to find something, but it's fine that it might fail sometimes) then you're going to have to handle NULL in the calling code.

However, if it's an error that it should fail to find something, it should still return NULL, but should probably assert or throw an exception when that happens, so you know something has gone wrong.

It's more up to you at that point whether you would prefer your program to crash and force you to fix some problem, or write extra code that attempts to handle the unexpected situation and carry on functioning in whatever way it can.

If it's really bad for it to fail, beware silently coping with it returning NULL - this may lead to you missing a really bad bug in your code [smile]
-Datriot-
-Datriot-
It is a perfectly acceptable situation not to find anything, so I guess checking for NULL pointers is really the only to go, I just thought there may have been a cleaner way. Thanks for the input. :)

PS: I never thought of using an if statement like that DevFred, interesting.
Sc4Freak
Sc4Freak
Quote:
Original post by DevFred
Or even more concise:

*** Source Snippet Removed ***


That code has a bad smell to it, if you ask me. I'd stick to rip-off's version - the loss of a single vertical line isn't particularly significant, but seeing an assignment in an if-statement is bound to set off some alarms for somebody later down the line.
ChaosEngine
ChaosEngine
Quote:
Original post by Sc4Freak
Quote:
Original post by DevFred
Or even more concise:

*** Source Snippet Removed ***


That code has a bad smell to it, if you ask me. I'd stick to rip-off's version - the loss of a single vertical line isn't particularly significant, but seeing an assignment in an if-statement is bound to set off some alarms for somebody later down the line.


What's wrong with it? If anything, I'd say DevFreds version is cleaner as it restricts the scope of the null object. It's a common C++ paradigm and unlikely to be confused with an equality test (which won't even compile).

if you think programming is like sex, you probably haven't done much of either.-------------- - capn_midnight

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.