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".

Top comments (40)

Collapse
 
jmfayard profile image
Jean-Michel 🕵🏻‍♂️ Fayard • Edited

My view is that abstractions are a good thing, the bread and butter of programming. You don't generate your HTML in C with printf(), do you? But we also need to be aware that they can be leaky.

Twenty years ago: The Law of Leaky Abstractions

Collapse
 
ravavyr profile image
Ravavyr • Edited

I'd say you shouldn't be using C to build websites at all, that there are much simpler ways to write your html these days :)

But you're right, abstractions are good thing, but not when someone creates an abstraction layer for HTML tags to where your write _br() instead of < br > literally typing more characters to use an abstraction because some old timey dev was utterly insane. [true story, old tag library i had to use years ago]

Collapse
 
besworks profile image
Besworks

I like to use the term automagically especially when describing things like Web Components to people. It's not really automatic which I feel implies no effort, but rather it's so advanced as to seem like magic to someone who doesn't get the how the abstraction works.

Image description

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
 
cerchie profile image
Lucia Cerchie

I don't like the term. Reducing complexity is hard work, not magic. It also removes all the developers from the idea that they can control the outcomes.

Collapse
 
jeremyf profile image
Jeremy Friesen

I find that the most important feature of "magic" is that the tool needs powerful introspection. Emacs. has bonkers amount of magic available, and the introspection tools are top-notch. Similarly, Ruby has the ability to craft powerful magic (see Rails). Ruby also has powerful introspection.

So, if the tool uses magic, understand how to navigate the incantations. Those "magic macros" will save a lot of time.

Collapse
 
jmfayard profile image
Jean-Michel 🕵🏻‍♂️ Fayard

A possible issue is that the "magic macros" can confuse the hell out of the developer tools from the IDE.

For example Scala is more powerful than Java or Kotlin, but its IDE support is worse because of that.

TL:DR developer tooling matters

Collapse
 
brunoj profile image
Bruno

I hate it when I'm new to a framework and love it when I'm experienced with it. 😅

Collapse
 
katafrakt profile image
Paweł Świątkowski

Interestingly, I could say exact opposite. When I'm new, I just love all the things being done that I don't have to do myself. When I start to work in larger codebases, it causes a lot of problems with refactoring and general headache that I'm not in control.

Collapse
 
gklijs profile image
Gerard Klijs

It's an double edged sword. With Spring Boot in the Java ecosysteem, a lot can be auto configured. Just add a dependency, and a lot of classes wil be created and started for you. Once you need to change something, it can be hard to know where to change it. And when something goes wrong on startup it might not always be obvious.

There are a lot more types of magic, like macros or synthetic sugar, for which more or less the same counts. It's easy when it works, but it can be hard when it doesn't.

Collapse
 
fish1 profile image
Jacob Enders

I tried using .NET core MVC, (for a day). The framework is able to detect the classes you defined, link them to the HTML, magically create routes, and probably much more that I never was exposed to.

Coming from a C/C++ and NodeJS background, this level of abstraction is just uncomfortable to me. I like defining my Classes, instantiating my Object and then passing it to the framework to be used.

Just being able to define a class and have a website popup feels like I'm creating configuration files, not programming.

But too be fair, I do tend to write a lot of the same boiler plate setup code on every Node application I build.

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
 
armousness profile image
Sean Williams

I only like magic when it's fully expressible within the language itself. For example, Rust has a println! macro that lets you put terms in the format string, and the terms are typechecked. However, Rust's macro system is available to the programmer, so you could reimplement println! if you were so inclined.

I guess the broader point here is, languages should focus more on allowing robust metaprogramming. Nearly all magic is metaprogramming, but this means in most languages you're stuck with the magic the language developers/maintainers thought you deserve.

Collapse
 
rgfaber profile image
R.G. Lefever • Edited

If you want to achieve software manufacturing -as in a repeatable and scalable industrial process- relying on relatively unskilled workers (aka juniors) to cut the cost, while still achieving high velocity or even some automation through code scaffolding, some "magic" is unavoidable. If you prefer artisan software development, resulting in an inconsistent code base, only understandable by the magicians themselves, by all means go ahead, but watch "The Sorcerer's Apprentice" first....

Collapse
 
stephen_tran profile image
Stephen Tran

If your application or website rely a lot on frameworks or libraries then perhaps low code or no code would be the safer and faster way to go. Something such as WordPress allows user to use boilerplate templates and customize them.

Collapse
 
rgfaber profile image
R.G. Lefever

Thank you for your feedback. As architects, we have a number of responsibilities: guarding non functional requirements, offering guarantees in the quality level of the output, providing tools that turn software development into software manufacturing, just to name a few. This is not limited to the front-end. In fact, the front-end is often the least of our concerns (cutting corners here, i know) and sure why not pick the path of least resistance there. But as for back-ends that handle huge amounts of events/data and/or need serious concurrency, core code that is tested through and through with minimal specific implementation is probably preferable.

Collapse
 
stephen_tran profile image
Stephen Tran

This is the dilemma of software engineering.

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
 
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.

Collapse
 
polterguy profile image
Thomas Hansen • Edited

The less you need to know about the code's internals the better encapsulation you've got. More encapsulation is a good thing since it allows us to move further up and do more with less. I think it was Paul Graham who said; "Always use the highest possible level of abstraction, since it allows you to deliver more with less". I insanely agree with this thinking, to the point where I arguably built my entire career around it, in implementing Magic - Literally ...

Magic is a good thing, always, think about it - All the best languages have high levels of magic at its core; SQL, HTLML, CSS, etc. The reason why these languages are great, is because you don't need to understand how they do what they do, you can just trust them to always doing it ...

Collapse
 
jzombie profile image
jzombie

It's hard to put a lot of faith in something that doesn't provide unit tests and code coverage. What may work right now may not work tomorrow after it gets hacked on.

Collapse
 
baenencalin profile image
Calin Baenen

It's fine because it can enable really cool behavior.
Þink Rust's interior mutability pattern Rc/RefCell.

Collapse
 
wjplatformer profile image
Wj

thorn!

Collapse
 
baenencalin profile image
Calin Baenen

Correct.
I also you ðe highly underrated eð. (Ðough it doesn't look good wiþ ðe low resolution of ðe font DEV uses.)