r/javascript • • Sep 19 '16

Is your JavaScript function actually pure? Or does this function behaves like a Mathematical Function in all the situations I will encounter in my code?

http://staltz.com/is-your-javascript-function-actually-pure.html
0 Upvotes

2 comments sorted by

0

u/fagnerbrack Sep 19 '16

There is already a related discussion on this sub. I believe submitting it again will make it possible to reach those who might have missed it.

It's not necessary to cover all the corner cases as the OP say, good abstractions allow efficiently duck typing for the set of possible inputs given the context that abstraction is inserted in. If it's a third party code, it should state clearly the signature in the documentation. To prevent mistakes such as overriding valueOf, we created the term anti-pattern, and overriding valueOf can be considered an anti-pattern.

Verifying the correctness of software is hard, even functional languages can't ensure 100% correctness, so we use techniques, including human-based ones to workaround this limitation. Naming anti-patterns and documenting inputs are two of the many techniques out there (and using TypeScript is an additional benefit for certain attributes of the input, as stated in the OP).

1

u/g_b Sep 19 '16

This article would have been better received if instead of Math.random you would have used another impure function. Or if you highlighted the fact that Math.random is just an example of an impure function.