My nerd rage on the topic of Fibers, co-routines, and compiled-to-js languages in node.js comes from seeing a non-problem solved in a way that makes it worse.
There are two really dangerous rationalizations in software design:
- “We need to make it easier for newcomers.”
- “Only experts will use this, anyway.”
Tower of Babel of Cards
They’re dangerous because each seems to imply that the other can be disregarded. The worst approach is to vacillate between them: “We need to make (async IO, object cleanup, database queries, whatever) easier for new users, so we’ll add this magic that experts will use to make intuitive APIs.”
It’s a vicious spiral. You can justify adding complexity, because “this is for experts only”, and also exposing the functionality in an obscured way in the name of making it “intuitive” for newcomers. The result is inevitably a towering house of cards. On a somewhat visceral level, I get this gut reaction to a “handing down from the mountain” mentality that is only harmful in the long run for everyone.
A better goal is to make an API such that when an expert uses it expertly, a newcomer can still understand it thoroughly. It requires an API (and more importantly, a culture) that limits experts, and educates newcomers. I don’t know the best way to approach problems to get there, but I’ve found pretty good results from thinking, “I’m probably going to mess this up the next time I touch it, so I’d better make it really obvious.” I usually fail at it, and end up cursing myself later when I rewrite the damn thing yet again.
(Passing around function pointers in C is actually a little bit better in some ways, because you can’t define your functions inline, and all the captured data must be passed around explicitly. This is a bit like using named functions instead of nested closures, which is a common low-complexity approach to the alleged callback problem.)
We’re All Idiots
All of us, whether experts or newcomers. Well-meaning idiots, perhaps, but idiots. We all write bugs, all the time. That is the normal state of software. There is no abstraction so perfect that a human won’t fuck it up at the first opportunity. But we can try to be better, simpler, to make it easier for our future idiotic selves to figure out what we were thinking in our cleverly idiotic brains, and chop the problems into smaller pieces so that our future selves can have some clues to find the maddeningly idiotic bugs we stuffed in there.
If your code uses step or async or slide or Q, I can read it, and I see a function getting called and passed some arguments. I can look at how you got that function from a require() statement, pull up that module, and look at its definition. These are lightweight abstractions that are highly penetrable. I’ve suggested in the past that everyone should write their own flow control lib, because it’s the best way to really understand how they work, and it’s not that hard. Once you grasp the basic concepts, reading someone else’s flow control utility is pretty easy.
If your code uses a compiled fibers library, or something that transforms the code, then it’s much harder for me to figure out what the heck is going on. The introspection is gone. The languages change. Stuff happens that isn’t present in the code I’m looking at. This is fundamentally different from a library that just passes functions around.
Not everyone is the same, and that’s great!! If you find that Fibers solve a problem for you, and they make the code that you write easier for you to manage and design, then that’s awesome. Use them. Understand them. Drop the illusory dichotomy of “experts” and “newcomers”, and realize that they are both idiots, and both you.
Every choice that isn’t completely boring will alienate someone. Pleasing everyone is a recipe for blandness. That’s why we keep the node-core standard lib super small, and encourage variation and repetition and iteration in userland modules.
Programmers tend to think in absolutes, perhaps because computers are such absolute things, or maybe just because absolutes are easier and we’re all idiots. I’ve done it in this post, because I’m as lazy as anyone. Maybe you’re the exception, and you actually are smart enough to use Fibers or Streamline wisely and still know what’s going on when you read the code a year later. If the benefit is greater than the cost, then go for it. It’s your house, you decide whether to wear pants or not.
When we are discussing things like this, it’s important to realize that we’re artists discussing matters of taste. If everyone agreed, then it would mean it wasn’t interesting. Maybe we should be less interested in convincing people of stuff, and more interested in just writing programs that do interesting things.