Search code examples
javascriptside-effects

What is the point of no side effects if I need 3 loops instead of one to do it?


Imagine I have a list of objects that are Questions. I have to find if they are visible and if they are visible I have to change their answer to visible as well. (The example may not be practical it is just an example) If this is no side effect way (is it?):

questions.filter(function(question) {
    return question.isVisible;
})
.map(function(visibleQuestion) {
    return getAnswer(visibleQuestion);
})
.map(function(answer) {
    answer.isVisible = true;
});

and this is side effect way:

questions.forEach(function(question) {
    if (question.isVisible) {
        var answer = getAnswer(visibleQuestion);
        answer.isVisible = true;
    }
});

Why choose the no side effect way if you have to iterate 3 times to do your job?

PS. I could be wrong of what is side effect and what is not and what really would be the two ways of handling this.


Solution

  • The argument against callbacks and side effects is about maintainability and reclaiming the call stack so we can use keywords like throw and return.

    The example you provided certainly makes it seem like an easy engineering trade off between readability and efficiency. That being said using a promise library you can achieve the same results without a callback/side effect all in a single loop.

    promise.all(questions.map(function(question) {
            if (question.isVisible) {
                resolve(getAnswer(question));
            }
        }).then(function (arResults) {
            arResults.forEach(function(answer) {
                answer.isVisible = true;
                console.log(answer)
            });
        });