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


 
GeneralOT: Tuples Pin
Jan Stetka29-Sep-09 13:38
Jan Stetka29-Sep-09 13:38 
Generala good offer to good programmers Pin
quinbee29-Aug-09 1:19
quinbee29-Aug-09 1:19 
GeneralRe: a good offer to good programmers Pin
Cristian Amarie5-Sep-09 21:11
Cristian Amarie5-Sep-09 21:11 
GeneralDo what SQL Server does Pin
Cristian Amarie28-Aug-09 7:20
Cristian Amarie28-Aug-09 7:20 
GeneralRe: Do what SQL Server does Pin
PIEBALDconsult30-Aug-09 4:52
mvePIEBALDconsult30-Aug-09 4:52 
GeneralRe: Do what SQL Server does Pin
Cristian Amarie1-Sep-09 4:01
Cristian Amarie1-Sep-09 4:01 
GeneralRe: Do what SQL Server does Pin
PIEBALDconsult1-Sep-09 4:18
mvePIEBALDconsult1-Sep-09 4:18 
GeneralRe: Do what SQL Server does Pin
Cristian Amarie1-Sep-09 18:30
Cristian Amarie1-Sep-09 18:30 
GeneralOne of my solutions (long post) Pin
Adam Maras27-Aug-09 20:51
Adam Maras27-Aug-09 20:51 
GeneralI like Windows Powershell approach Pin
Maruf Maniruzzaman26-Aug-09 16:21
Maruf Maniruzzaman26-Aug-09 16:21 
GeneralRe: I like Windows Powershell approach Pin
Md. Marufuzzaman27-Aug-09 10:59
professionalMd. Marufuzzaman27-Aug-09 10:59 
Generaldepends on the environment Pin
FlowerX25-Aug-09 22:23
FlowerX25-Aug-09 22:23 
GeneralRe: depends on the environment Pin
Cristian Amarie28-Aug-09 7:15
Cristian Amarie28-Aug-09 7:15 
GeneralNullables Pin
Abhishek Sur25-Aug-09 21:42
professionalAbhishek Sur25-Aug-09 21:42 
GeneralRe: Nullables Pin
FocusedWolf27-Aug-09 15:15
FocusedWolf27-Aug-09 15:15 
Generalsometimes empty sometimes null Pin
ecooke24-Aug-09 6:21
ecooke24-Aug-09 6:21 
GeneralA single answer for all cases? Pin
Stephen Hewitt24-Aug-09 5:20
Stephen Hewitt24-Aug-09 5:20 
GeneralRe: A single answer for all cases? Pin
Chris Maunder24-Aug-09 6:06
cofounderChris Maunder24-Aug-09 6:06 
GeneralRe: A single answer for all cases? Pin
PIEBALDconsult24-Aug-09 13:36
mvePIEBALDconsult24-Aug-09 13:36 
GeneralRe: A single answer for all cases? Pin
Myddyn28-Aug-09 2:45
Myddyn28-Aug-09 2:45 
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 PinPopular
Chris Maunder24-Aug-09 1:50
cofounderChris Maunder24-Aug-09 1:50 
So here's where I stand:

1. If I'm asking you to look up an object or return a list of objects matching some criteria then I do not expect an exception to be thrown. Not finding something, or having an empty list is not exceptional. Having a database fail isn't exceptional. Having all code always work perfectly with butterflies and roses is exceptional. Exceptions, in my book, are to be used to catch exceptional cases, not to control program execution flow.

2. I don't care what you return from a method. Return null. Return a default value. Return a pink giraffe. I will check your return value to ensure it's non-null and to ensure it's a sensible value. I will do this everytime at every point because I do not trust code. I don't trust the code someone else has written. I don't trust the code I wrote 2 years ago. What if we're using different conventions? What if I had too much sugar two years ago and decided I should return a valid object, everytime, always, then in deep, dark Toronto Winter I decided "screw it: you're getting a null".

So the convention I use is: use a common sense convention and be consistent about it but at the end of the day, when everyone's had their fun, I'm going to double check whatever it is you gave me because I'm crabby and cynical and computers hate segfaults[^].

cheers,
Chris Maunder

The Code Project Co-founder
Microsoft C++ MVP

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.