DEV Community

Discussion on: Clean Code with Currying (3 min recap)

Collapse
 
imjb87 profile image
John Bell

I don't understand how this is anymore semantic than the original example you posted of a function that takes two parameters. In many countries tax is a constant aswell and not a variable.

Collapse
 
sgoulas profile image
sgoulas

Generally speaking currying is good when you have a complex function that can be broken up in smaller ones. If you curry it as a series of functions it is easier to make changes to the subfunctions rather to the large one. Also, it allows you to use the newly created subfunctions in other parts of the code, so instead of a big function that explicitly calls smaller functions you have a pipe of functions that the result of the one gets fed into the other. This also means you can remove or add new functions in the pipe much more easily than refactoring the big function.

Collapse
 
ziskand profile image
Yigal Ziskand

Good point, thanks @sgoulas !

Collapse
 
ziskand profile image
Yigal Ziskand

Hi John! Thanks for the comment!

Refactoring the initial function f(x,y) into f(x)(y) can be beneficial in multiple factors - if "x" parameter is constant (as you mentioned that tax in many countries is constant), then there is no need to repeat the procedure of providing it over and over as an input..
you can benefit by applying currying technique and simply create constant function of f_with_x() and apply it on the other parameter which is most likely dynamic f_with_x(y)

I believe it can be very beneficial if you maintain some OSS project or maybe some public library [let consumers of your code make less mistakes...]

Imagine - as if priceWithTax(100, 10) can be mistakenly written as priceWithTax(100, 100) while priceWithTax10(100) hides the details of tax input and tax calculation.
In that particular case we gain readability, code reuse, hide unrequested implementation details.

Hope i made it a bit cleaner :)