I like ES6 but I miss the word function
I look at code pretty much all day. There is a certain delight I feel when looking at clear, concise code that makes me feel comfortable about my understanding of what’s going on. for example
const numbers = [1,2,3,4,5,6];
function getAllEven(numbers) {
return numbers.map(num => num%2 === 0);
}Contrived example, yes, but my point is that this kind of code is easy to look at. It uses the elegance of es6 to write cleaner more readable code.
However, there is one syntax I can’t seem to embrace…
const ugly = a => b => a + b
I just don’t like it. It’s ugly because it makes my brain try harder than it should to understand what is going on. In particular, because it’s a function that returns a function that makes it hard to read. Syntax should be obvious so I can use those precious brain cells to better understand the business logic (which will most likely be even more unclear and confusing). Here is the same javascript in ES5.
var pretty = function(a) {
return function(b) {
return a + b;
}
}Ah, tranquility and serenity. Very obvious without any extra effort. pretty is a function that takes a parameter and returns a function that takes a parameter and returns the sum of the two parameters. Maybe I’ll get used to the es6 version, but for now I say don’t just abandon the word function because es6 lets you do it. Consider the role of that word and how it might help make your code more readable. Thank you in advance.