In this tutorial, you will learn what is meant by the term currying. You will learn what currying is and how you can use it in order to make your code cleaner. Currying can only be applied to languages that support it, like JavaScript, e.g. languages that treat functions as first-class citizens. You can not apply currying techniques in languages like C#. The concept of currying is a key part of functional programming. The main aim of functional programming is to favour writing small, well-defined functions. Currying is the process of converting a function that takes multiple arguments and then converting it into one or more function that takes one argument at a time instead. When I first read about currying, I didn't really see the benefit and struggled to grasp why people used it. I decided to write this article to prevent people from going through the same confusion. The aim of this post is to be a practical guide as to when you would use currying. Let us start with a very basic comparison of what curried code looks like. This is a simple javascript function that is not curried. It takes two parameters and adds them together:
To use a curried style, you would refactor it like this:
If that doesn't impress you, then welcome to my world. The benefit of currying isn't really in the function definition, the benefit of currying comes in the area of code where that function is called. In the normal function definition code, you would call the function like this:
To call the curried function is done like this:
In the curried approach, I pass the first variable 10
as the first parameter. The import part is that the function is assigned to a new variable called add10
. After add10
is defined, I can omit passing 10
to add()
ever again. I can now simply use add10()
any time I want to add 10
to something. This approach adheres to the DRY principle, as it is eliminating unneeded duplication in your code, which is a software design approach you want to ensure your code follows 😊
If you only need to apply add10
once, then currying can be a bit of an overkill. Another benefit of currying is that your code can become easier to understand. As long as you follow good naming conventions, your code can be written in a more declarative style, e.g. add10()
has a more descriptive meaning compared to seeing lots of code like this add(10,5)
and add(10,10)
dotted around your codebase.
For a more real-world problem, let's say you have a web form and you have a helper function that creates field inputs for you, like a textbox. The website works in 20 different languages, each field needs to have different regions, validation and error messages depending on the user's location. In this example, I could end up with lots of code like this:
You can implement the same functionailty in less code using currying like this:
If you have 20 odd fields in your form, the second approach while contains more set-up code will eventually use less code than the first approach. The main benefit of currying is when you need to call the same functions with some of the same parameters a lot. In these situations, currying becomes a good technique to use as it will make your code easier to refactor. Currying also creates a more declarative code base, e.g. it's easier to read the code and understand what it's doing (Assuming good naming conventions are being enforced!)
Currying is not a set in stone coding style to use all the time. Currying is useful in certain situations. When you need to use the same function calls a lot, currying is a pretty easy concept to apply to your code. Chain your function arguments into multiple single calls and assigning those result into new functions allow for powerful things to be done! Happy Coding 🤘