The Big Gains of Functional-Style Code
Functional-style code has a higher signal-to-noise ratio; we write fewer lines of code, but each line or expression achieves more. We gained quite a bit from the functional-style version, compared with the imperative-style version:
-
We avoided explicit mutation or reassignment of variables, which are often sources of bugs and result in code that’s hard to make concurrent. In the imperative version we repeatedly set the totalOfDiscountedPrices variable within the loop. In the functional-style version, there is no explicit mutation in our code. Fewer mutations leads to fewer errors in code.
-
The functional version can easily be parallelized. If the computation was time consuming, we can easily run it concurrently ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access