News for hackers. Stuff that matters.
Re:Magic is often used to describe confusing code by jerf on Friday March 25, 2016 @07:15PM (#11363466)
I tend to use the word magic for those situations when you can not easily examine the source code and follow the control flow. For instance, you are in some part of the code and you have a User object and you see the code calls the .babble() method on the object. You go to the User definition, and there's no babble method. You follow up the superclasses, and there's no babble method. Where did the babble method come from? Where is it going? Who created it? Where can I find the documentation for it (if any)? Even if I do find something that seems to implement it, can I be sure that some other code didn't fiddle with the babble method in another way?

Languages permit varying levels of this sort of magic, ranging from the Ruby extreme of dynamically rewriting classes and inheritance hierarchies on the fly, to the pretty-much-no-magic of C. (C qua C anyhow. Preprocessor can get fun.)

I think this is one of the worst sorts of magic, and I've generally tried to arrange my code so that even when I'm using a dash of magic, the pathway back to where everything came from is still fairly clear. (Usually this can be done just by dealing with the fact you may need to use a few more keystrokes to leave the breadcrumb trail back to the implementation. Sometimes simplifying a bit of functionality down to zero bytes in the source code is going a step too far.)

I'll also observe that having used a number of both high-magic and low-magic languages, in terms of costs/benefits, I am generally underwhelmed by the benefits of the magic, and find the cost generally go up more quickly than most people realize as source code size goes up. I generally prefer low-magic languages. With just a bit of thought, you can still get a huge amount of power without the costs. (For instance, believe it or not, Haskell is a nearly-zero-magic language by this definition. For all the crazy stuff it seems to be able to do, you can always see what's going on just by following back the symbol definitions, with the exception of just a finite bit of syntax sugar implemented by the compiler, the most important of which is generally tutorial-level.)

Re:Magic is often used to describe confusing code by s_kilk on Saturday March 26, 2016 @08:06AM (#11365348)
> you see the code calls the .babble() method on the object... Where did the babble method come from?

I call it "grep-resistant programming"

Re:Magic is often used to describe confusing code by beachstartup on Friday March 25, 2016 @08:57PM (#11363862)
the problem is when you use magic, you're deliberately being clever, and the only proper way to be clever is if you thoroughly document your cleverness with comments and diagrams and examples. the person you befuddle with how clever you are is probably yourself, 2 years from now.
Re:Magic is often used to describe confusing code by SixSigma on Saturday March 26, 2016 @07:50AM (#11365316)
see - Lions' Commentary on UNIX 6th Edition, with Source Code [1]

[1] https://en.wikipedia.org/wiki/Lions%27_Commentary_on_UNIX_6t...

        /*
         * If the new process paused because it was
         * swapped out, set the stack level to the last call
         * to savu(u_ssav).  This means that the return
         * which is executed immediately after the call to aretu
         * actually returns from the last routine which did
         * the savu.
         *
         * You are not expected to understand this.
         */
        if(rp->p_flag&SSWAP) {
                rp->p_flag =& ~SSWAP;
                aretu(u.u_ssav);
        }
Re:Magic is often used to describe confusing code by StavrosK on Friday March 25, 2016 @11:08PM (#11364261)
Eh, try a month.
Re:Magic is often used to describe confusing code by MaulingMonkey on Saturday March 26, 2016 @05:51AM (#11365063)
Later today: Wait, what was I doing again?
Re:Magic is often used to describe confusing code by StavrosK on Saturday March 26, 2016 @07:04AM (#11365226)
What is this in reply to?
Re:Magic is often used to describe confusing code by andrewflnr on Saturday March 26, 2016 @12:22PM (#11366198)
I think they were raising your "try a month" to "try 6 hours".
Re:Magic is often used to describe confusing code by StavrosK on Sunday March 27, 2016 @10:36AM (#11370063)
Yes, and I was re-raising it to "five minutes" :( Apparently the joke wasn't very obvious.
Re:Magic is often used to describe confusing code by MaulingMonkey on Saturday March 26, 2016 @02:31PM (#11366791)
Correct. Thanks!
Re:Magic is often used to describe confusing code by braythwayt on Saturday March 26, 2016 @09:35AM (#11365599)
> I tend to use the word magic for those situations when you can not easily examine the source code and follow the control flow.

Reminds me of the quote “In Smalltalk, everything happens somewhere else “—Adele Goldberg.

I call these kinds of things, “action-at-a-distance,” or if I’m feeling snide, “COMEFROM code.” The thing is, action-at-a-distance is exactly what I want when I’m trying to separate concerns.

In Rails, for example, you can decorate a controller method using `before` at the top of a controller. To the person unfamiliar with the spell, this makes perfect sense:

  def index
    authenticate_current_user
    
    render something_or_other
  end
But this is magic:

  before authenticate_current_user, only: [:index]

  # ...lots of code...

  def index
    render something_or_other
  end
That is perfectly grep-able if you know to grep a method name, and if you have a small controller class it ought to stand out, &c. &c., so it rarely offends. But it’s still magic.

It’s a win if we want to make our index method as simple as possible.

And that goes for all of Rails, really. The very idea of “convention over configuration” is a wholesale embrace of magic. In Ember:

  userService: service(),
How is it that if you write `service()` without naming a service, the function defaults to the name of the property it is bound to? That makes absolutely zero sense in JavaScript, and the more you know about JavaScript, the more magical this becomes.

There is, in fact, no way to find the code that does this unless somebody whispers in your ear that Ember does compile-time chicanery, and you aren’t really programming in JavaScript, you’re programming in Ember, a language that has a lot in common with JavaScript, and that compiles to JavaScript.

You can decide for yourself if this is a win. Lots of people like it.

Re:Magic is often used to describe confusing code by Justsignedup on Saturday March 26, 2016 @11:06AM (#11365886)
Exactly. I use the term a lot with fully dynamic code without extremely strong documentation every step of the way. With 10 layers of dynamic invocation (typical in Ruby on Rails) it is almost impossible to know without a debugger what code path your code will take.
Re:Magic is often used to describe confusing code by rasur on Saturday March 26, 2016 @08:56AM (#11365487)
Smalltalk would help with this kind of magic. The Smalltalk Method finder is (British-English School-yard definition of) Magic ;)