0

Consider

class Service { methodA(input: string[]): number { const resB = this.methodB(input); return resB * 2; } methodB(input: string[]): number { return input.length; } } 

If MethodB is a pure function, can MethodA also be considered a pure function?

What about the case, when MethodB is a pure function from an injected class?

class Service { constructor(helper:Helper){} methodA(input: string[]): number { const resB = this.helper.methodB(input); return resB * 2; } } 

In both cases I argued yes in my team, because both cases no side effects takes place and the input>output is deterministic.

On the other side, let alone the the fact, that MethodA was using this. inside its function body was enough for other to say no.

9
  • 2
    The litmus test for purity is "it shouldn't cause observable side-effects and it should work the same each invocation". The method fulfils both. Then again, I am not sure why that would matter that much. So what if it's pure? Or impure? Commented Mar 29, 2022 at 13:35
  • In addition to VLAZ' statement ... let's consider we deal with pure functions only. Now the functions are nailed to a namespace (other than the global object, ... let's say a module) which technically makes them "pure methods". Now, what is the advantage of implementing such methods which do neither access nor compute/mutate any this context as non/prototypal (and/or static) class methods in comparison to the module implementation? Commented Mar 29, 2022 at 13:46
  • 1
    @PeterSeliger this isn't really special. It can be treated as an argument to the function. Because it is. Especially if you use .call() or .apply() where you actually pass it in as argument. And a pure function should produce the same result given the same arguments - whether you do getProp({name: "Fred"}, "name") (which returns "Fred") or getProp.call({name: "Fred"}, "name") the result is the same. It's referentially transparent, too: first might be implemented as obj[prop] the second as this[prop] but both can be substituted with ({name: "Fred"})["name"]. Commented Mar 29, 2022 at 13:59
  • 1
    @deceze Purity is not just a tool for the compiler, but an aid in reasoning about and understanding how a function works. And people ignore bizarre edge cases for that. Commented Mar 29, 2022 at 14:58
  • 1
    "the the fact, that MethodA was using this. inside its function body was enough for other to say no" - they're wrong. Only if this is mutated (its properties are written to), the function is no longer pure. Commented Mar 29, 2022 at 15:00

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.