The Tragedy of Magic Code

escape artistThere are many software platforms that offer ‘magic’ like ways to accomplish things. Ruby on Rails has a lot magic going on – pass in a string argument and it gets automatically pluralized, converted from snake case to elephant case, inferred as a class name in the global namespace, instantiated and connected to set of URL routes.  Magic code is dangerous because it’s easy to use without needing to understand how it really works, and leads to a false sense of confidence that you know what’s going on.

Imagine a professional escape artist.  He has 10 years of experience performing in front of large international audiences, then one day he watches a new magician’s escape trick.  He thinks he knows how it was done so he tries to replicate it.  He chains himself up, puts on a straight jacket, takes a deep breath and locks himself in a glass box filled with water.  Moments later he realizes his understanding of how the trick was done is incomplete… Now he’s in real trouble.

That is the danger of code that works like magic.  You think you know how something works; you gain confidence that you understand it, and then it fails to do what you expected at a critical point.

What about the beginner magician?

The beginner magician can watch a card trick performed 100 times and may not be able to figure how how it works.  If they try to perform the trick themselves it’s almost guaranteed to fail.  You can not easily learn how the magic trick is done by just by watching someone else do it.

Web frameworks can be like a bag of tricks where you get to tell the professional what trick to do.  “Make this form work with AJAX” can be like asking a magician to guess which card you’re thinking of.  You have no idea how it either of those tricks work.  However, you can become very adept at wielding your bag of tricks and never learn enough to create your own.


Posted

in

by

Tags: