6

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?

6 Answers 6

12

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

6
  • 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. Commented Feb 6, 2012 at 22:05
  • @Sean: it's not always possible, but returning a default object is possible in many more situations than most people expect. Commented Feb 6, 2012 at 22:12
  • 4
    @Sean: en.wikipedia.org/wiki/Null_Object_pattern Commented Feb 6, 2012 at 22:14
  • @Kevin: you are right, I will look for a way to return a default object. Commented Feb 6, 2012 at 22:43
  • The Null Object pattern sucks. There are some places where you can reasonably default, but in most cases, you can't. It's not a good thing to fall back on and it's not a general answer to the problem. Commented Feb 6, 2012 at 22:59
6

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.

1
  • Oh, you are right, I didn't realise that you don't need to write if (prefs !== null) explicitly. Thanks. Commented Feb 6, 2012 at 22:09
4

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] } } 
3

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/)

2

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.

2

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"; } 

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.