3

I know there's a 'methodology' where the developer should write such functions, that the return value is always of the same type. So, say we have a function what tends to return an array, and something unwanted happens, like the argument is invalid, in such cases we won't return null, instead we return an empty array []. Then the user of the method can be sure, that the returned value will be an array.

I can't remember to the name of the principle, can you help me out please?

Thanks

Example

Instead:

function(arg) {
  var res;
  if (arg) {
    return ['example'];
  }
  return res;
}

We must set a default value according to the principle:

function(arg) {
  var res = [];
  if (arg) {
    res = ['example'];
  }
  return res;
}

Note, that in the first case, there's a race condition in the return value (undefined/array), while in the latter case, we return with an array.

Attila Kling
  • 1,717
  • 4
  • 18
  • 32
  • I think, neither of them. But thanks for the suggestions. ;) – Attila Kling Sep 09 '14 at 13:13
  • Do you mean [Duck Typing](http://stackoverflow.com/questions/3379529/duck-typing-in-javascript)? – chridam Sep 09 '14 at 13:14
  • Looks like method chaining, except there methods return `this`, not an arbitrary object of the right type - http://en.wikipedia.org/wiki/Method_chaining – joews Sep 09 '14 at 13:14
  • 2
    You could also be describing (one part of) a [monad](http://en.wikipedia.org/wiki/Monad_(functional_programming)). – joews Sep 09 '14 at 13:18
  • @chridam nope, duck typing means if something quacks, and have feathers and etc.. then it can be considered as a duck. My 'problem' is different than this. – Attila Kling Sep 09 '14 at 13:18
  • See also [Design by Contract](http://en.wikipedia.org/wiki/Design_by_contract). – Eric Sep 09 '14 at 13:29
  • Here's some advice that refers to this idiom as "avoiding in band error indicators": http://www.informit.com/articles/article.aspx?p=2133373&seqNum=5. I don't know if I've seen anything more precise than that, and I've seen this practice recommended by people whom I would expect to know the term if there were one. – Steve Ruble Sep 09 '14 at 13:29
  • @Eric i was not looking for this, but thanks for the link;) Btw i had the opportunity to learn about DbC at the univ. and wrote some Eiffel code back then, and i found it very interesting, good stuff :) – Attila Kling Sep 09 '14 at 13:37
  • 1
    _"Note, that in the first case, there's a race condition"_ - No there isn't. That's not what ["race condition"](http://en.wikipedia.org/wiki/Race_condition) means. – nnnnnn Sep 09 '14 at 23:18

2 Answers2

1

Basically, the below, is done to avoid performing null checks in the calling method. This is done not only for array types but for all the object types to handle null/undefined returns.

function(arg) {
  var res = []; // initialization of an object
  if (arg) {
    res = ['example'];
  }
  return res;
}

For example,

when you call the above function, there is no possibility that the returned value would be null or undefined. This eliminates adding a

if((var x = function(arg)) != null) check in the calling method.

The closest Design pattern you can relate to is the Null Object pattern in java.

http://www.tutorialspoint.com/design_pattern/null_object_pattern.htm

BatScream
  • 19,260
  • 4
  • 52
  • 68
0

Then the user of the method can be sure, that the returned value will be an array

Sounds like type safety. Although, even in a typesafe language you could easily return null if you document the return type to be nullable.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375