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.

Jim-Y
  • 1,637
  • 4
  • 16
  • 31
  • I think, neither of them. But thanks for the suggestions. ;) – Jim-Y 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. – Jim-Y Sep 09 '14 at 13:18
  • @Jim-Y Thanks for clarifying :) – chridam Sep 09 '14 at 13:21
  • 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 :) – Jim-Y 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
  • 18,650
  • 4
  • 46
  • 64
  • Thanks, yea i know, thats why i edited the original question with an example. I'm more and more convinced that my memory is malfunctioning at this one :) – Jim-Y Sep 09 '14 at 13:39
  • hmm.. so you knew about the Null object pattern too ? – BatScream Sep 09 '14 at 13:40
  • Nope, i didn't, but i knew the side effects of my example, what you mentioned in the first sentence. – Jim-Y Sep 09 '14 at 13:41
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
  • 572,313
  • 128
  • 898
  • 1,281