DEV Community

Cover image for Where do you stand on "magic" within languages and frameworks?
Ben Halpern
Ben Halpern

Posted on

Where do you stand on "magic" within languages and frameworks?

Let's discuss the concept of magic — what are your opinions?

In the context of computer programming, magic is an informal term for abstraction; it is used to describe code that handles complex tasks while hiding that complexity to present a simple interface. The term is somewhat tongue-in-cheek, and often carries bad connotations, implying that the true behavior of the code is not immediately apparent. For example, Perl's polymorphic typing and closure mechanisms are often called "magic". The term implies that the hidden complexity is at least in principle understandable, in contrast to black magic and deep magic, which describe arcane techniques that are deliberately hidden or extremely difficult to understand. However, the term can also be applied endearingly, suggesting a "charm" about the code. The action of such abstractions is described as being done "automagically", a portmanteau of "automatically" and "magically".

Latest comments (40)

Collapse
 
ifarmgolems profile image
Patrik Jajcay

Magic without documentation = good luck, have fun

Collapse
 
htho profile image
Hauke T.

There is a word by the German Author/Poet Rainer Maria Rilke "There is magic in every beginning" ("Jedem Anfang wohnt ein Zauber inne").

When you start to learn a new Framework everything seems to be magic. It took me a long moment understand how module mocking works in jest. (the moment is not over yet).

I don't like magic if I am not able to understand whats behind it. I am glad that I first learned JS Promises and then async/await. New JS Programmers need to learn Promises but will not use them much. It will always feel awkward for them. For me async/await actually felt like "syntactical sugar". For them its a language feature, that will feel as magic once they want to understand why something happens when.

Frameworks take this to a whole new level, where you apply some attributes to your html code and you have half an SPA - and a hard time to understand how it works.

I like VanillaJS because there is no magic (as long as you dont do it).

Collapse
 
qm3ster profile image
Mihail Malo

Hating magic is a form of Survivorship/Availability bias.
Yeah you remember all the abstractions that got in your way, either by not letting you know the underlying details or requiring a lot of upfront reading to grok.
But you are forgetting all the good ones that you understood intuitively and/or saved you lifetimes of work.

Collapse
 
stephen_tran profile image
Stephen Tran

I remember when Microsoft first created Excel, their motto was something like "Kill all the dependencies".

Collapse
 
n1ckdm profile image
Nick

Is magic not just misdirection? Which is kind of what you're trying to do when creating an API that encapsulates a bunch of complex logic, but presents a simple interface to a user.

I don't see a problem with using the term.

Collapse
 
hackape profile image
hackape

Magic/abstraction is a spectrum. There’s good magic and there’s bad one.

My criteria is, if a magic feels nature, almost close to language primitives, and doesn’t come with easy footgun, it’s good magic.

So the two points for good magic are:

  1. Ergonomic API
  2. No/rare funny unexpected behavior.

Bad magic often goes wrong easily, and is always difficult to debug. The ones that stand in the middle, I have mix feeling and would approach with caution.

Like for example, MobX’s reactivity is very powerful. But when they allow implicit circular dependency, one can get into a dead loop. Upside is they’re aware of the problem and make it relatively easy to debug.

I would personally use MobX, avoid circular dep with caution (cus that’s usually a sign of bad code structure on your part). But I wouldn’t feel comfy to use it in production codebase maintained by a team of devs of varied levels.

Collapse
 
amiamigo profile image
Ami Amigo

To me...Magic is like having prebuilt functions like Sort. As opposed to manually have to come up with an algorithm to sort things myself. Abstraction is good...But you can't convince me that frameworks like React makes since easier...they become so complex that they defeat the whole purpose.

Collapse
 
amiamigo profile image
Ami Amigo

To me...Magic is like having pre-built functions like Sort. As opposed to manually have to come up with an algorithm to sort things myself. Abstraction is good...But you can't convince me that frameworks like React makes since easier...they become so complex that they defeat the whole purpose.

Collapse
 
mistval profile image
Randall

This kind of magic can offer us a lot in terms of productivity gains, but we should never be dependent on it, and should always use low-magic solutions until we understand the underlying technologies well.

What I mean for example is that you shouldn't try to use React before learning how to build a site with plain HTML, CSS, and JS.

You shouldn't use Nest.js before learning Express (or maybe even the raw http library).

You shouldn't use ORMs without learning SQL.

If you skip learning the fundamentals, and depend on magic, you'll be in trouble when the going gets tough.

Of course, I'm not saying we have to dig silicon out of the ground and build CPUs with our bare hands before learning to program (though I'm sure that would be pretty enlightening).

But any time we use a framework or library, we should try to sense when it might be obscuring the fundamentals and preventing us from learning something important. Not always easy, but I think it's something you can get a sense for.

Collapse
 
progrium profile image
Jeff Lindsay

Magic is ok if you aren't building on it. Magic is less ok if it's infrastructure. Magic is for sprinkling on top, as a treat.

Collapse
 
henryong92 profile image
Dumb Down Demistifying Dev

What are you talking about?
We perform "magic" all the time when we meet managers with unrealistic timelines 🤷🤷‍♀️🤷‍♂️

Collapse
 
manuartero profile image
Manuel Artero Anguita 🟨

it's like salt in food. A touch of magic/salt is needed. It makes our meal more tasty. But too much.... turns food inedible.

Reaching the point of "no idea who/when/why is executing this" would be the equivalent to too salty meals.

Thing is, the more you use it, the more you get to it right? (until the doctor warns you about using salt in excess)

Collapse
 
gnsp profile image
Ganesh Prasad

I go full Vernon Dursley in this context.

Collapse
 
tbroyer profile image
Thomas Broyer • Edited

The way I use the term "magic" is whenever there's implicit behavior (generally based on naming rules, but sometimes on introspection/reflection, scanning, etc.) that generally cause "action at a distance"; it can probably be generalized as "too much abstractions, more than you can comprehend". "Convention over configuration" is different from "magic"… until there are too many such conventions.

As long as it works, "magic" feels great, boosts your productivity, etc. The moment it breaks, good luck finding why it broke and then how to make it work the way you want/expect. And of course, just like a framework, the moment you need to do differently and "escape the magic", you're likely going to hack around and start depending on internals/implementation details ; a bit like any kind of framework.

That's one reason I don't like frameworks: you built a blog in 10 minutes and 30 lines of code? great, but did you realize you're using a gazillion lines of code you didn't write? Yet you're liable for them once you push them to production. Write a bit more code you fully understand, to glue together fewer dependencies (easier to debug, easier to update, smaller, probably faster too), and always be explicit (don't write "magic" yourself).

Collapse
 
mordechairoth profile image
mordechairoth

Well said.

Collapse
 
manuartero profile image
Manuel Artero Anguita 🟨

can't agree more actually

Collapse
 
bugmagnet profile image
Bruce Axtens

As the team works through putting COBOL onto Exercism I'm reminded about some of the declarative magic that COBOL provides. The thing I'm thinking about is 88 levels which can simplify code dramatically.