2

Mutable data types cause side effects, but what side effects specifically and how can they be grouped? So far I've found two sorts of effects:

  • race conditions (due to single threaded JS in the context of asynchronous computations/event loop)
  • erroneously expected idempotence

The first point is self-explanatory. However, the second one needs clarification:

const append = xs => ys =>
  (xs.unshift(ys), xs);

const empty = [];

const fold = f => acc => ([x, ...xs]) =>
  x === undefined
    ? acc
    : f(fold(f) (acc) (xs)) (x);
  
const xs = [1,2,3];

const main = fold(append) (empty);

main(xs);
console.log(main(xs)); // [1,2,3,1,2,3]

The expected result is [1,2,3], but the operation is non-idempotent.

Are there any other sorts of side effects caused by mutable data types? Since such side effects cause so much trouble I think it important to know, which side effects you are likely to face.

  • I could be missing something, but to my understanding this is too broad of a question to answer on Stack Overflow. – Patrick Roberts Apr 13 '20 at 19:34
  • 1
    I'm not so sure about your terminology here. "Side effect" is fairly broad, and the very fact that a function mutates data available outside its scope is itself a side-effect. While you might be able to describe those side-effects as caused by an unexpected non-idempotence (??), it's often those effects which are most important, and not that lack of idempotence. `if (fold (append) (empty) (x).length > xs .length) launchTheMissiles()`. It's probably the missiles we worry about, not the contents of `empty`. – Scott Sauyet Apr 13 '20 at 19:37
  • @ScottSauyet You are right, I think. I am rahter looking for error classes, which arise as a consequence of the mutation side effect, right? –  Apr 13 '20 at 19:43
  • 1
    Maybe, but I'm afraid you might be chasing after a chimera. – Scott Sauyet Apr 13 '20 at 19:53

0 Answers0