Click here to Skip to main content
15,918,041 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)


 
GeneralIdeally, such function would return an option type. Pin
Nemanja Trifunovic24-Aug-09 3:56
Nemanja Trifunovic24-Aug-09 3:56 
GeneralRe: Ideally, such function would return an option type. Pin
Chris Maunder24-Aug-09 6:09
cofounderChris Maunder24-Aug-09 6:09 
GeneralRe: Ideally, such function would return an option type. Pin
Nemanja Trifunovic24-Aug-09 7:03
Nemanja Trifunovic24-Aug-09 7:03 
GeneralRe: Ideally, such function would return an option type. Pin
Jörgen Sigvardsson29-Aug-09 10:21
Jörgen Sigvardsson29-Aug-09 10:21 
GeneralYou guys are too trusting Pin
Chris Maunder24-Aug-09 1:50
cofounderChris Maunder24-Aug-09 1:50 
GeneralRe: You guys are too trusting Pin
Nish Nishant24-Aug-09 2:26
sitebuilderNish Nishant24-Aug-09 2:26 
AnswerWas this a trick question? Pin
etkid8424-Aug-09 3:54
etkid8424-Aug-09 3:54 
GeneralRe: You guys are too trusting Pin
Kevin McFarlane24-Aug-09 4:12
Kevin McFarlane24-Aug-09 4:12 
Returning null is problematic unless it's documented what returning null means. When consuming Microsoft's classes this is usually the case. In a typical "Find" algorithm it's quite usual for a negative result to be valid behaviour. Returning a null object is perfectly fine in this case. However, I've often maintained code where, due to lack of documentation, it's not clear whether returning null from the method you were consuming was possibly valid behaviour or not.

The upshot is that for my own methods, in those cases where I may return null, I document what this means. Design by Contract in .NET 4.0+ will also be useful going forward if developers start using it.

Kevin

AnswerRe: You guys are too trusting Pin
Vikram A Punathambekar24-Aug-09 7:10
Vikram A Punathambekar24-Aug-09 7:10 
GeneralRe: You guys are too trusting Pin
Ernest Laurentin24-Aug-09 9:02
Ernest Laurentin24-Aug-09 9:02 
GeneralRe: You guys are too trusting Pin
Nemanja Trifunovic24-Aug-09 4:08
Nemanja Trifunovic24-Aug-09 4:08 
GeneralRe: You guys are too trusting Pin
Kevin McFarlane24-Aug-09 4:24
Kevin McFarlane24-Aug-09 4:24 
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 
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 

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.