Section 9.19 Which is Better?
Anything that can be done with modifiers can also be done with pure functions. In fact, some programming languages only allow pure functions. There is some evidence that programs that use pure functions are faster to develop and less error-prone than programs that use modifiers. For example, if a programmer accidentally calls the following function twice, the original array will be raised to the fourth power, not squared:
On the other hand, the following pure function ensures that, no matter how many times you call the
square_list
function, your original array will never change:Nevertheless, modifiers can be convenient at times, and in some cases, functional programs are less efficient.
In general, we recommend that you write pure functions whenever it is reasonable to do so and resort to modifiers only if there is a compelling advantage. This approach might be called a functional programming style.