Practice question · True or false
A function that writes to a global variable is as easy to test as one that only returns a value, provided both are correct.
Hints
- What must a test do before and after the impure one?
- Hidden state has to be arranged and restored.
Show the answer
False
Why
False. The pure function needs no fixture and no teardown, and its tests can run in any order. The one touching a global forces every test to set that state up and reset it, and makes tests order-dependent, which is why side effects get pushed to the edges and the middle of a program kept pure.
Practise State, Mutation, and Side Effects
The app has 7 more questions on this lesson, and keeps your place in the course. Computer Science I is free to start.
More questions on State, Mutation, and Side Effects
- Order these events so that every stated outcome is true at the moment it is written.
- A function is called twice with identical arguments and returns a different answer the second time. Select…
- A function sorts the list it is given rather than returning a sorted copy. Why is that worth documenting…
- Sort each statement by whether it concerns the identity of an object or merely its value.