Can Javascript Class methods still be considered "pure", if they call other "pure" class methods?

183 Views Asked by At

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.

0

There are 0 best solutions below