Is magic bad?
Is it bad if a software component is “magic?” A recent blog featured on the front page of Hacker News captures an oft-repeated sentiment:
Magic Frameworks are bad…”Magic” is when a framework does something automatically, in a cool way.
Regardless of whether frameworks are a good idea or not, don’t we want our abstractions to do something “automatically?” Imagine a software component that provides no defaults. Now the consumer has to manually provide every configuration parameter, making the component more difficult to use. The extreme of not doing anything automatically is to do nothing at all — why bring a component into your application if it does little for you?
The core tenet of Ousterhout’s A Philosophy of Software Design is “modules should be deep.” In other words, “the best modules provide powerful functionality yet have simple interfaces.” In this understanding, the garbage collector is the best type of deep module since it has an interface that is completely invisible to the application programmer, yet it provides the powerful functionality of freeing objects. It does all this in a mysterious way. Should we disparage the GC because of such magic and return to manual memory management? Real abstractions do something magical, otherwise the abstraction hasn’t hidden anything profound from us.
I suspect that complaints about magic are, in fact, complaints about leaky abstractions. No abstraction is watertight. Consider the garbage collector, the best abstraction of all. Most of the time, the GC fulfills its promise of insulating from the worries of manual de-allocation. Occasionally the abstraction breaks down when we create a memory leak.
Memory leaks are frustrating, but the cost of the abstraction collapsing is one we bear willingly. Like many things in software engineering, it’s a question of economics. Does the abstraction leak 0.0001% of the time? Or 1% of the time? How harmful are the leaks caused? How harmful, compared to the powerful functionality the module should provide? Yes, if a leaky module constantly requires you to delve into its internals, then by all means abandon it. But don’t disparage magic.
Modules should be deep. Modules should be magic.
“Magic-like” is more of an emotional description — Dan Abramov (Tweet id 1659531075981328384 now deleted)