DEV Community

Cover image for The difference between code and magic πŸ§™β€β™€οΈπŸ§™β€β™‚οΈ
Ben Halpern
Ben Halpern

Posted on

The difference between code and magic πŸ§™β€β™€οΈπŸ§™β€β™‚οΈ

I've been listening to the Harry Potter audiobooks, as one does.

There is a plot point in Harry Potter and the Deathly Hallows where Harry is counting down the days until he turns 17 when he is no longer under surveillance for use of under-age magic. It's a spell the magical community places on young magicians to maintain law and order, and perhaps a bit of Orwellian control.

There is a moment where Ron wishes Harry "happy birthday" early in the morning, and it immediately clicks to Harry that he can now use magic without detection! (They are currently on the run from Death Eaters so this is important).

Harry immediately casts a spell in celebration.

Me, the software developer, not being able to help myself, gets nervous. I literally think to myself "What if the curse is time zone sensitive, what if it's based on UTC, what if Harry was born later in the day and he hasn't actually hit 17 yet? What about leap years? Why doesn't Harry leave himself a buffer of certainty before casting a needless spell!"

And then I think to myself "that's not how magic works at all, that's how computers work." Technology and magic are not too different a lot of the time, but this is a significant distinction: Magic works as intended. Technology works as built.

In general, if Harry is 17, for all intents and purposes, the spell will be lifted. That's just how the universe works. When a spell doesn't work as intended, it's likely because the castor isn't capable of expressing their intentions effectively. This is why focus and nuance are such an important thing when casting. But when a wise witch or wizard casts a spell, it's going to work to their intentions.

Code just works how it works, we have off-by-one errors, we have time zones, we have string interpolation, we race conditions. Code does what it was built to do, but it's not magic, it doesn't just work as intended. No amount of wisdom makes code work as intended unless it is meticulously crafted to work this way. The closer it is to work as intended without the burden of edge cases, the closer it is to magic, but our code will never have the kind of guarantees of effectiveness that magic does. That's the deal.

Happy coding πŸ§™β€β™€οΈπŸ§™β€β™‚οΈ

Top comments (28)

robole profile image
Rob OLeary • Edited

"Any sufficiently advanced technology is indistinguishable from magic." by Arthur C Clarke. The addendum is "unless you are an engineer" πŸ˜‰

maxart2501 profile image
Massimo Artizzu

"Any sufficiently analyzed magic is indistinguishable from science!" - Girl Genius

markoshiva profile image
Marko Shiva

Lets simplify there is no magic. If it appears as magic to you you just do not know the data and the process of how it works. :)

dwd profile image
Dave Cridland

"When a spell doesn't work as intended, it's likely because the castor isn't capable of expressing their intentions effectively."

Yeah, like code.

ben profile image
Ben Halpern

I think no matter how hard you intend, you're still fighting the nature of complexity.

Magic exists in a universe where complexity is kind of a solved problem.

It's a declarative universe we can only dream of.

dmfay profile image
Dian Fay • Edited

Magic as conceived in a particular children's book exists in a universe where complexity is kind of a solved problem. Magic as conceived and practiced over the course of human history is itself an attempt to grapple with complexities beyond ordinary understanding and control: health, weather, politics, closeness with the divine.

Aleister Crowley defined magick-with-a-k as 'the Science and Art of causing Change to occur in conformity with Will', and explicitly included such arcane works as banking and potato-growing. Programming is not so different.

dwd profile image
Dave Cridland

By mentioning complexity and programming in the same context, I'm now forced by ancient tradition to mention No Silver Bullet and ask if you've read it?

chrisachard profile image
Chris Achard

I've thought a lot about code being magic while watching Harry Potter over the years

Both take intense study and learning to master.
Both require you to say just the right thing to get it to work
Both make the impossible become possible
Both have edge cases and unintended side effects

... I could go on and on πŸ˜†

It's definitely an interesting thought that magic works "as intended", and code works "as built"... but it's still the closest thing to magic we have!

fkhan698 profile image
Faizan Khan

Computers and technology are like a wizarding world. Hackers are like Dark Wizards and learning cybersecurity is like Defense Against the Dark Arts

integerman profile image
Matt Eland

This has been on my mind recently as I became a bootcamp instructor a month ago. Pretty sure I'm a Ravenclaw, but what does Vue, JavaScript, and REST translate to? Please don't be divination...

codemouse92 profile image
Jason C. McDonald • Edited

One of my perennial favorite cartoons is My Little Pony: Friendship is Magic, and I always find it humorous how much unicorn magic in that show does behave like computer code.

  • A spell must be written down in at least one place before it can be cast, although if this "source" exists, it can be cast from memory. (Episode 65, 116/117, 167)

  • Destroying the last copy of the "source" means the spell can no longer be cast. (Episode 116/117)

  • Running a spell without verifying what it will do can have unexpected results. (Episode 65)

  • Unicorn magic is exactly as literal as computer code, and can have unintended results; outcomes have nothing to do with the intentions, only with the technique. (Episode 29, 55, 138, 145)

  • An object (relic) can contain specific magical properties or spells, but those magical properties can only be used in specific ways. You can't make a relic do something it was never intended to do. This is not unlike "compiled code". (Episode 118/119, 194/195, 209)

  • Unicorn magic "just works", although it must be powered either individually by a unicorn, stored energy in a relic, or another energy source. It's not impossible for non-unicorns to use unicorn magic, but that magic will behave the same no matter who is using it.

lbeul profile image
Louis • Edited

@ben little bit off-opic, but is it your first time listening to the story?
Read all of the HP books for the first time a couple of months ago. I'm 22 years old. How was I even able to live without them for so long?

slashgear_ profile image
Antoine Caron • Edited


kspeakman profile image
Kasey Speakman • Edited

I thought this post was going a different direction. As in code that is "magic" versus code that is not. The difference being that the former is perhaps inscrutable to the user of it. They just pray that they invoked it correctly with the right incantations (configuration, parameters) and that it doesn't cause any unexpected casualties when run. Non-magic code is obvious what will happen and perhaps boring, even if intricate.

nickytonline profile image
Nick Taylor

When my code doesn't work, I just scream "Expecto patronum!" and that fixes all my bugs. πŸ˜†

highcenburg profile image
Vicente Antonio G. Reyes

I've never understood software development from magic. Thank you Ben.

bhavaniravi profile image
Bhavani Ravi

A production software running for more than 10 years without much of maintenance or bug fixes by the developer. That's magic. No it's not a dream. I know a person who wrote such a software

bugmagnet profile image
Bruce Axtens

We want DWIM (Do What I Mean.) We're stuck with DIY (Do It Yourself) and DWYS (Did Wut Yu Sed)

wellicanjosh profile image

It is like doing Math vs English.

itsjzt profile image
Saurabh Sharma

Magic works as intended. Technology works as built.


lukegarrigan profile image
Luke Garrigan

Haha loved this!

lbonanomi profile image
lbonanomi • Edited

Charles Stross got some books out of this idea; I recommend them highly.