Click here to Skip to main content
15,905,612 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 
Apologies for the length of the post, I just wanted to be thorough in showing how I occasionally do things. Smile | :)

namespace Questionable
{
    using System;

    /// <summary>
    /// Providesfor providing error reporting for function calls, without using exceptions.
    /// </summary>
    /// <typeparam name="TResult">The type of the result.</typeparam>
    /// <typeparam name="TError">The type of the error. This is typically an enumeration or a class with more error data.</typeparam>
    public class QuestionableReturn<TResult, TError>
    {
        private TResult result;

        /// <summary>
        /// Initializes a new instance of the <see cref="QuestionableReturn&lt;TResult, TError&gt;"/> class.
        /// </summary>
        /// <param name="result">The result.</param>
        public QuestionableReturn(TResult result)
        {
            this.HasResult = true;

            this.Result = result;
            this.Error = default(TError);
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="QuestionableReturn&lt;TResult, TError&gt;"/> class.
        /// </summary>
        /// <param name="error">The error.</param>
        public QuestionableReturn(TError error)
        {
            this.HasResult = false;

            this.Result = default(TResult);
            this.Error = error;
        }

        /// <summary>
        /// Gets a value indicating whether this instance has a result, as opposed to an error.
        /// </summary>
        /// <value>
        ///     <see langword="true"/> if this instance has a result, as opposed to an error; otherwise, <see langword="false"/>.
        /// </value>
        public bool HasResult
        {
            get;
            private set;
        }

        /// <summary>
        /// Gets the result.
        /// </summary>
        /// <value>The result.</value>
        public TResult Result
        {
            get
            {
                if (this.HasResult)
                {
                    return this.result;
                }
                else
                {
                    throw new InvalidOperationException("A result is not available. Check the Error property for more information.");
                }
            }

            private set
            {
                this.result = value;
            }
        }

        /// <summary>
        /// Gets the error.
        /// </summary>
        /// <value>The error.</value>
        public TError Error
        {
            get;
            private set;
        }
    }

    /// <summary>
    /// A sample error enumeration.
    /// </summary>
    public enum SampleReturnError
    {
        /// <summary>
        /// The operation completed successfully.
        /// </summary>
        None = 0,

        /// <summary>
        /// The item was not found within the data.
        /// </summary>
        ItemNotFound,

        /// <summary>
        /// An internal error occurred.
        /// </summary>
        SomeOtherError
    }

    /// <summary>
    /// A sample class with a method demonstrating the questionable return model.
    /// </summary>
    public class SampleReturnClass
    {
        private static readonly string[] Data = { "FirstItem", "SecondItem", "BadItem" };

        /// <summary>
        /// Finds the string.
        /// </summary>
        /// <param name="index">The index of the string to find.</param>
        /// <returns>A questionable return value containing either a string result or an error.</returns>
        public QuestionableReturn<string, SampleReturnError> FindString(int index)
        {
            QuestionableReturn<string, SampleReturnError> returnValue;

            if (index >= Data.Length)
            {
                returnValue = new QuestionableReturn<string, SampleReturnError>(SampleReturnError.ItemNotFound);
            }
            else
            {
                string dataItem = Data[index];

                if (dataItem.Contains("Bad"))
                {
                    returnValue = new QuestionableReturn<string, SampleReturnError>(SampleReturnError.SomeOtherError);
                }
                else
                {
                    returnValue = new QuestionableReturn<string, SampleReturnError>(dataItem);
                }
            }

            return returnValue;
        }
    }
}


Adam Maras | Software Developer
Microsoft Certified Professional Developer

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 Pin
Chris Maunder24-Aug-09 1:50
cofounderChris Maunder24-Aug-09 1:50 

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.