Click here to Skip to main content
15,915,172 members

Survey Free Text Answers

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

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?

Text entry answers for sorted by frequency of occurrance.

(5) it depends
(4) CListCtrl
(3) Depends
(1) Depends on the circumstances.
(1) Depends on the context
(1) depends on the context; but normally NO error code
(1) Depends on the language and the existing infrastructure
(1) Depends on the method.
(1) depends on the object; exception and log most faulty returns
(1) depends on the situation
(1) depends on the situation.
(1) depends on what the method does. but it's commented using xml comments so you know what its going to return.
(1) Depends, there is no single solution that can be applied to all scenarios
(1) either null or ampty/default object according to situation on hand
(1) empty for strings and null for all else
(1) erik.se@gmail.com
(1) ExitProcess()
(1) I don't return.
(1) I panic, it was there a minute ago and keep looking!
(1) i return empty collection
(1) I return 'the' empty object
(1) I return the instance self
(1) I throw an exception!
(1) I try to use TryGetValue(string name, out someValue)
(1) I'd return Nothing when possible, but most of my callers want that as little as possible.
(1) In C++ - boost::optional - in general, use types to express what you really meant, people!
(1) It depends on the method
(1) It depends on the situation
(1) It depends on the situation.
(1) It depends on time and place, as everything
(1) It depends.
(1) It varies by circumstance , sometimes null, sometimes empty object
(1) Not enough information - depends on what the program requires.
(1) not essential and checked later: null / essential: exception / used in formatting or part of configuration: default object
(1) null if returning a pointer, or boost::optional for objects/values
(1) obot
(1) Return null or throw an exception depending on context
(1) That all depends on the desired results
(1) This is surely a no-brainer, the answer is in the question, I quote 'fails to return the object requested'. The method has failed, so the correct cause of action is to throw an exception.
(1) throw
(1) Unless it's a list. Then I return an empty list.
(1) Unless it's a list. Then I return an empty list.
(1) collection: empty, individual object: null
(1) Context dependent
(1) CPianoCtrl
(1) #f
(1) &CListCtrl()
(1) 0
(1) All
(1) All of the above based upon the object and the desired behavior
(1) all the above except an empty object
(1) Boost optional type