Click here to Skip to main content
15,892,005 members

Survey Results

Do you return null or an empty object when returning "no result"?   [Edit]

Survey period: 24 Aug 2009 to 31 Aug 2009

If you have a method that is meant to return an object but that method fails to return the object requested (eg. object not found) do you return the equivalent of null, or do you return an empty / default object?

OptionVotes% 
I return null94164.28
I return an empty / default object22415.30
I throw an exception17712.09
I return an error code684.64
Other543.69

View optional text answers (61 answers)


 
GeneralRe: You guys are too trusting [modified] Pin
Nemanja Trifunovic24-Aug-09 4:35
Nemanja Trifunovic24-Aug-09 4:35 
GeneralRe: You guys are too trusting Pin
Kevin McFarlane24-Aug-09 5:34
Kevin McFarlane24-Aug-09 5:34 
GeneralRe: You guys are too trusting Pin
Nemanja Trifunovic24-Aug-09 6:04
Nemanja Trifunovic24-Aug-09 6:04 
GeneralRe: You guys are too trusting Pin
Simone Serponi24-Aug-09 4:27
Simone Serponi24-Aug-09 4:27 
GeneralRe: You guys are too trusting Pin
Ware@Work24-Aug-09 5:40
Ware@Work24-Aug-09 5:40 
GeneralRe: You guys are too trusting Pin
PIEBALDconsult24-Aug-09 13:48
mvePIEBALDconsult24-Aug-09 13:48 
GeneralThrow an exception if object not found. Pin
Member 205300623-Aug-09 21:41
Member 205300623-Aug-09 21:41 
GeneralRe: Throw an exception if object not found. Pin
Simone Serponi24-Aug-09 4:40
Simone Serponi24-Aug-09 4:40 
Checking the what a function return is always a good idea: Using exceptions and a try/catch just to avoid check the result is only lazyness (IMO).

However I agree in general that you should provide a way to quick check the result of the operation result i.e. supposing you wrote a function that search one element in a collection I prefer something like the following:

// Search an object and return its position (if found) using pPositionWhereFound.
Bool searchObj ( Oject_t *pList, Oject_t* pObjToSearch, int *pPositionWhereFound );

instead of:

// Search an object and return its position (if found). -1 as return value
// means not found
int searchObj ( Oject_t *pList, Oject_t* pObjToSearch );

in both cases you have to check the returned value but doing:

if ( searchObj( &List, &ObjToSearch, &Position ) )
...

is nicer than:

int iPosition = searchObj( &List, &ObjToSearch );
if ( iPosition != -1 )
...

Bye By(t)e Wink | ;-)

GeneralRe: Throw an exception if object not found. Pin
Deflinek24-Aug-09 20:57
Deflinek24-Aug-09 20:57 
GeneralRe: Throw an exception if object not found. Pin
Simone Serponi24-Aug-09 23:49
Simone Serponi24-Aug-09 23:49 
GeneralRe: Throw an exception if object not found. Pin
shea-c424-Aug-09 6:43
shea-c424-Aug-09 6:43 
GeneralFail == error Pin
Johann Gerell23-Aug-09 20:40
Johann Gerell23-Aug-09 20:40 
GeneralRe: Fail == error Pin
Vikram A Punathambekar24-Aug-09 7:15
Vikram A Punathambekar24-Aug-09 7:15 
GeneralRe: Fail == error Pin
Johann Gerell24-Aug-09 12:28
Johann Gerell24-Aug-09 12:28 
GeneralRe: Fail == error Pin
Johann Gerell24-Aug-09 12:32
Johann Gerell24-Aug-09 12:32 
GeneralRe: Fail == error Pin
Locust200024-Aug-09 18:23
Locust200024-Aug-09 18:23 
GeneralI depends on the situation Pin
PIEBALDconsult23-Aug-09 19:47
mvePIEBALDconsult23-Aug-09 19:47 
GeneralRe: I depends on the situation Pin
tec-goblin23-Aug-09 20:09
tec-goblin23-Aug-09 20:09 
GeneralRe: I depends on the situation Pin
Herman Koornhof23-Aug-09 20:20
Herman Koornhof23-Aug-09 20:20 
GeneralRe: I depends on the situation Pin
Mycroft Holmes23-Aug-09 21:35
professionalMycroft Holmes23-Aug-09 21:35 
GeneralThis does depend on the type Pin
Schmuli23-Aug-09 19:42
Schmuli23-Aug-09 19:42 
GeneralRe: This does depend on the type Pin
Mohamed Meligy23-Aug-09 20:21
Mohamed Meligy23-Aug-09 20:21 
GeneralRe: This does depend on the type Pin
Sinisa Hajnal23-Aug-09 20:29
professionalSinisa Hajnal23-Aug-09 20:29 
GeneralRe: This does depend on the type Pin
W Balboos, GHB24-Aug-09 1:40
W Balboos, GHB24-Aug-09 1:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.