Arrow functions have been a part of JavaScript since ES6. They are typically supported where you run JavaScript, except in Internet Explorer. To be clear, arrow functions are:
(a,b) => a+b
instead of
function(a,b) { return a+b }
I like to make things simple, and
- my code sometimes run on Internet Explorer
- arrow functions offers shorter and simplified syntax in some cases, but fundamentally you can write the same code with function
- I like to not have a build step (babel, webpack and friends) for a language that really does and should not need one
so, until now I have simply avoided them (and kind of banned them, along with other ES6 features) in code and software I am responsible for.
However
- arrow functions (as part of ES6) are here to stay
- they offer some advantages
- Internet Explorer will go away.
so, it makes sense to have a strategy for when to use arrow functions.
What I find on the Internet
The Internet is full of sources telling you how you can use arrow functions, how to write them, what are the pros, cons and pitfalls, and what they cannot do.
- The key difference is how arrow functions work with this.
- The syntax is shorter especially for single argument (needs no parenthesis), single statement (needs no return), functions.
- Arrow functions don’t work well with Object oriented things (as constructors and prototype function)
In short, there are some cases where you can’t use arrow functions, some cases where they offer some real advantages, but in most cases it makes little real difference.
Arrow functions allow you to chain sort().filter().map() in very compact ways. With simple single statement arrow functions it is quite nice. But if the arrow functions become multiple lines I think it is poor programming.
What I don’t really find on the Internet
I don’t really find good advice on when to use arrow functions and when not to use arrow functions. I mean, when I program, I make decisions all the time:
- Should I break this code out into a function?
- Should this be an object (prototype style) or just data?
- Should I break this code into its own module?
- Should I write tests for this?
- Should I allow a simple, slower algorithm, or should I add effort and complexity to write my code faster?
- What should be the scope of these variables?
- Should this be a parameter or can it be hard coded?
- Can I make good use of map/reduce/every and friends, or is it better I just use a loop?
- Naming everything…
- …and so on…
Using, or not using, an arrow function is also a choice. How do I make that choice to ensure my code is good? I don’t really find very clear guidelines or style guides on this.
Lambda functions in other languages
Other languages have lambda functions. Those are special case anonymous functions. The thing I find peculiar about the use of arrow functions in JavaScript is that they are often used instead of function, when a standard function – not a lambda – would have been the obvious choice in other languages.
Intention
For practical purposes most often function and () => {} are interchangeable. And I guess you can write any JavaScript program using only arrow functions.
When you write code, it mostly does not matter what you use.
When you read code, it comes down to understanding the intention of the writer.
So I think good use of arrow functions is a way that makes the intention of the code as clear as possible. I want clear and consistent guidelines.
Using arrow functions in well defined cases shows more intention and contributes to more clear code than never using them.
I tend to read arrow functions as being a strong marker for functional programming. I find it confusing and when arrow functions are used in code that breaks other good core principles of functional programming.
The strongest cases
The strongest cases for arrow functions I can see:
Minimal syntax (no () or {} required), and never worth breaking such function out.
names = stuffs.map(stuff => stuff.name);
Callback: the arguments (error, data) are already given by openFile and the callback function cannot have a meaningful this. Also, for most practical purposes, the callback needs to use closure to access data in the parent scope, so it can not be a named function declared elsewhere.
openFile('myFile', (error, data) => { ... implementation });
When it makes little difference
For a regular function it makes no difference:
const swapNames = (a,b) => { let tmp = a.name; a.name = b.name; b.name = tmp; }
The function alternative would be:
function swapNames(a,b) {
and is actually shorter. However, I can appreciate with arrows that it is completely clear from the beginning that a binding of this can never happen, that it can not be used as a constructor and that there can be no hidden arguments (accessed via arguments).
Confused with comparison
There are cases when arrow functions can be confused with comparison.
// The intent is not clear var x = a => 1 ? 2 : 3; // Did the author mean this var x = function (a) { return 1 ? 2 : 3 }; // Or this var x = a <= 1 ? 2 : 3;
Obfuscate with higher order functions
Higher order functions (map, reduce, filter, sort) are nice and can improve your code. But, carelessly used they can be confusing and obfuscating.
These are not the fault of () => {} in itself. But it is a consequence of making higher order functions with arrow functions too popular.
I have seen for example (things like):
myArray.map(x => x.print())
map() should not have a side effect. It is outright obfuscating to feed a function that has a side effect into map(). And side effects have nothing to do with functional programming in the first place.
I have also seen reduce() and filter() being used when every(), some() or find() would have been the right choice. It is obfuscating, it is expensive, and it produces more code than necessary.
The use of arrow functions with higher order functions is only appropriate when the correct higher order function is used.
The abusive cases
Anonymous functions that are non-trivial and could clearly be named and reused (and testable) is clearly bad code:
myStuff.sort((a,b) => { if ( a.name < b.name ) return -1; if ( a.name > b.name ) return 1; if ( a.id < b.id ) return -1; if ( a.id > b.id ) return 1; return 0; });
especially when the code is duplicated or the parent function is large.
An arrow-friendly policy
Admittedly, after doing my research I feel happier with arrow functions than I thought I would.
I suggest (as long as your runtime supports it) to use arrow functions as the default function. The reason for this is that they do less. I think the standard behavior of arguments, this and of OOP-concepts (prototype and constructors) should be optional and require explicit use (of function).
Just as one-line if-statements and if-statements without {} should be used carefully (I tend to abuse it myself) I think the same applies to arrow functions.
I think this is excellent:
names = stuffs.map(stuff => stuff.name);
but apart from those common simple cases I think think the full syntax should be used for clarity:
const compareItems = (a,b) => { if ( a.name < b.name ) return -1; if ( a.name > b.name ) return 1; if ( a.id < b.id ) return -1; if ( a.id > b.id ) return 1; return 0; };
(dont try to be clever by omitting (), {}, or return).
The use of function should be reserved for
- constructors
- prototype functions
- functions that need the standard behavior of this
- functions that do things with arguments
- source files where function is used exclusively since before
Basic good functional programming practices should be especially respected when using arrow functions:
- Dont duplicate code: break out anonymous functions to named functions when appropriate
- Dont write long functions: break out anonymous functions to named functions when appropriate
- Avoid side effects and global variables
- Use the correct higher order function for the job
Also, obviously, take advantage of OOP and function when appropriate!
Callback functions
I think anonymous callback functions should generally be kept short.
const doStuff = () => { readFile('myFile', (error, data) => { if ( error ) console.log('readFile failed: ' + e); else doStuffWithData(data); }); }; const doStuffWithData = (data) => { ... };
Performance
In principle, I see no reason why arrow functions should not be at least as fast as regular function. In practice, the current state of JavaScript engines could be disappointing – I don’t know.
However, a named static function is typically faster than an anonymous inline function. The JIT typically can optimize a function the more it is run so named and reusable functions are preferred.
I have made no benchmarks on arrow functions.
Feedback
I will start using arrow functions when I write new code and I feel enthusiastic about it. I will probably come across things I have not thought about. Do you have any thoughts on this? Let me know!
0 Comments.