4

Usually I see default value of a function to be a "static" or "constant" value and is not generated by another function call.

Example of what I mean:

function test(num = 10) {
  // code ...
}

I have the following example. Is this ok or bad design?

function test(num = randomNumber()) {
  // code ...
}

Alternative would be like this.

function test(num) {
  if (num === undefined) {
    num = randomNumber()
  }
  // code ...
}
  • 1
    As long as you have control over the `randomNumber()`, I think it will be fine since you can cover every case for that input. – holydragon Apr 25 '19 at 08:59
  • Another method for default values I often see is `num = num || randomNumber()` even though it is not exactly the same as the other examples given by you. – Mokuyobi Apr 25 '19 at 09:05
  • 2
    Asking if something is good or bad design is opinion based and will get this question closed. You should clarify what you mean by 'is this ok...' What is the issue? Please read [ask]. – Rafael Apr 25 '19 at 09:06
  • 1
    @Mokuyobi This will not work if num is any falsy value(`0`,`null`,or `''` etc) – Maheer Ali Apr 25 '19 at 09:07
  • @Maheer Ali I know that is what I meant by writing "even though it is not exactly the same as the other examples given by you". Most times it does not make any difference, but one must use it with caution if this edge cases matters. – Mokuyobi Apr 25 '19 at 09:08
  • If the default parameter is an impure function, then...all that means is that your function is impure because if you call `test()` twice, you won't (necessarily) get the same result. But whether that's "good" or "bad" depends on how you use this. If you want a random number generator then `function* random(seed = randomNumber())` is probably fine. If instead you have `checkBankAccount(accountNumber = randomNumber())` then that's probably very much not what you want to happen. – VLAZ Apr 25 '19 at 09:54

2 Answers2

0
function test(num = randomNumber()) {
  // code ...
}

and

function test(num) {
  if (num === undefined) {
    num = randomNumber()
  }
  // code ...
}

are not equal even though they produce the same result, because the first one gives the concept of method overloading like you can call test() as well as test(5) in your case test() would only process randomNumber(). Rest depends on your choice of pure and impure function as in either case.

  • The two *are* equal. `num = 1` is a shorthand to save you doing `if(num === undefined) num = 1` yourself. The latter is simply the ES6 syntax, the former is how you'd do it in previous versions of JS. There is no concept of "overloading" in JS because two functions with the same name cannot exist. Also overloading *isn't* giving a default value to a parameter. It is just about having multiple procedures with the same name but different parameters (and possibly return values). – VLAZ Apr 25 '19 at 09:49
  • @VLAZ you are right! as I can see this post (https://stackoverflow.com/questions/456177/function-overloading-in-javascript-best-practices) – Muahmmad Tahir Saleh Apr 25 '19 at 10:12
  • That's a difference in our way to perceive things, but objectively there is not much difference, besides the fact that avoiding a default parameter will work even in environments where ES6 is not supported. – Lajos Arpad Apr 25 '19 at 10:19
0

Entering the

function test(num = randomNumber()) {
  // code ...
}

code into BabelJS yields this result:

"use strict";

function test(num) {// code ...

  if (num === void 0) {
    num = randomNumber();
  }
}

So the initial code and the alternative you have given are pretty much equivalent. It's just a thing of styling if we assume proper implementation of ES6 in the browser. In some very old or very limited browsers parameter defaults will not work at all, but that's not a problem of using a function call as parameter default or not.

In extreme data savings mode ES6 is not supported at all, so, if you work for OperaMini, then you will need to make sure that you avoid ES6 features. Here you can see a compatibility table: https://kangax.github.io/compat-table/es6/

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175