Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why don't generators transpile well?

Tags:

We're following the airbnb eslint guide and in it they say recommend not using generators

  • 11.2 Don't use generators for now.

    Why? They don't transpile well to ES5.

I don't seem to be able to find any explanation as to what they mean by not transpiling well (not just in this document, but on Google). We're using babel and there are polyfills to do this. Is there something I'm missing?

like image 433
Ian Avatar asked Jan 31 '17 13:01

Ian


1 Answers

They are simply wrong (or the docs is seriously outdated). Transpilers create a closure based state machine from generators and async functions. They are not nice but work pretty fast. The only downside is that it is harder to debug (even with sourcemaps).

On the other hand, not using generators will result in awkward workarounds in some situations, where generators would provide a clean solution. Always write code for clarity first.

EDIT

We developers learnt in the real life that some programming challanges can best be solved with state-machines. Generators and async functions give you a powerful tool to express most of those state-machines.

This is how languages evolve: we find a repeatedly occuring programming problem which has a solution scheme, so people create a new programming languages with new syntax to have a shorter solution for that problem. This is how we got basic datastructures, functions, closures, classes, first class functions, GC, RTTI, reflection, etc... Today it's considered a matter of choice which language you use for your projects. You can write machine code directly, or use some high-level managed language. The argument is usually about execution speed (assembly ought to be faster right?), portabiliy, and the learning curve of the language syntax used (why would I learn lambdas and yield and async/await when I am and always was able to solve any problem without using any of those?). I personally prefer to use expressive languages, and I believe that high level/managed programs will not be slower than native programs forever.

So let me emphasize what you lose by not using generators: you end up writing the same hundred-line state-machines (probably disguised as a collection of objects and functions) that could be generated from a short program using a sensible and familiar syntax.

like image 147
Tamas Hegedus Avatar answered Sep 21 '22 11:09

Tamas Hegedus