Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Consider the following JavaScript function:

function Foo() {
  function getPreferences() {
    if ([there is an existing preferences object]) {
      return preferences;
    }
    return false;
  }
}

The calling code is:

var foo = new Foo();
var prefs = foo.getPreferences();
if (prefs) {
  // do something with prefs
}
  • Is the pattern of returning an object if it exists, or false otherwise, seen as a good or bad practice? Other alternatives I can see are to return an empty object {} or null.
  • Does this pattern have a name?
share|improve this question

6 Answers 6

up vote 11 down vote accepted

False seems like a strange choice. null seems more natural. Best of all, return default preferences and eliminate if (prefs).

share|improve this answer
    
My fault: I meant more generally where a caller expects an object in return, but it might not exist. In some cases (like preferences) defaults would work, but not in others. –  Sean Feb 6 '12 at 22:05
    
@Sean: it's not always possible, but returning a default object is possible in many more situations than most people expect. –  kevin cline Feb 6 '12 at 22:12
4  
@Sean: en.wikipedia.org/wiki/Null_Object_pattern –  back2dos Feb 6 '12 at 22:14
    
@Kevin: you are right, I will look for a way to return a default object. –  Sean Feb 6 '12 at 22:43
    
@back2dos: thanks for the link, and name. –  Sean Feb 6 '12 at 22:44

I'd much rather see code that returns an object for success and null for not success. Then, it at least feels like the function is behaving consistently. It's return either an object or the standard way of saying no object null. The same code:

if (prefs) {
    // do something
} 

works either way, but returning null seems more consistent to me (as the lack of an object) than returning false.

share|improve this answer
    
Oh, you are right, I didn't realise that you don't need to write if (prefs !== null) explicitly. Thanks. –  Sean Feb 6 '12 at 22:09

You should either return a default value, or throw an exception . I like throwing exceptions for mandatory things, and delivering default values for optional things. Using null makes debugging a bit more icky, and precludes a key that has null as its value.

 function Foo() {
       function getParameter(name) {
               if ( [name does not exist] ) {
                    throw "no such key";
               }
               return [parameter value]
       }
 }
share|improve this answer

This answer differs from others because I've taken your specific use case into account. On any other day, I'd use the null value check.

In this specific case, I probably wouldn't use any of your methods. I'd most likely use an object for preferences with a number of default key/value pairs for any settings that I need. That way, there isn't any special casing needed in any of your code (checks for if(prefs) are not needed).

Consider the following code (how I think I'd go about implementing it anyway):

function MyClass(o) {
    this.prefs = {
        var1: 0,
        var2: 1,
        var3: 2
    };
    for(key in o) {
        this.prefs[key] = o[key];
    }
}

MyClass.prototype.doFunc = function() {
    console.log(this.prefs.var1);
    console.log(this.prefs.var2);
    console.log(this.prefs.var3);
}

var o = new MyClass();
o.doFunc();

var p = new MyClass({var2: "foo"});
p.doFunc();

(http://jsfiddle.net/utkdf/)

share|improve this answer

One sees lots of that pattern in javascript because of javascript's 'creative' handling of falsey values.

While it is possible to do nifty stuff in javascript, I'm not sure a language designed in the space of ten days is something you'd want to emulate.

share|improve this answer

Like other people said, it's null which must be returned, instead of false. Even with strongly typed languages, you may expect false from a method, which will have a different meaning than null. It's even more important in languages with no type checking. For example, PHP has a similar problem, perfectly illustrated when using readdir method (below, quote and source code from PHP manual):

Please note the fashion in which readdir()'s return value is checked in the examples below. We are explicitly testing whether the return value is identical to (equal to and of the same type as--see Comparison Operators for more information) FALSE since otherwise, any directory entry whose name evaluates to FALSE will stop the loop (e.g. a directory named "0").

/* This is the correct way to loop over the directory. */
while (false !== ($entry = readdir($handle))) {
    echo "$entry\n";
}

/* This is the WRONG way to loop over the directory. */
while ($entry = readdir($handle)) {
    echo "$entry\n";
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.