October 27, 2010

Java_Script demons

Hi,
 I'm programmer and I'm writing JavaScript.
As any addicted person I have perfectly legal explanation for my behavior and here I'm going to show three things:

  • what motivate me doing this
  • why I think that it isn't addiction
  • how to quit

One

When I tried it first time it feel OK and looks harmless- just a toy, stripped down Java.
But it was naive - there always was monster inside. In fact two monsters and you already been warned about this by authorities such as Wikipedia.

The key design principles within JavaScript are taken from the Self and Scheme programming languages.[8]

Ha! It helps, if only you posses ability to read in between lines and see the future. I wasn't able then but I can help you now.

It is true, JavaScript is LISP and we all know how contagious LISP is. JavaScript hides it's true semantic behind M-expressions but you can convert any "HelloWorld.js" to "good-by-world-p" simple translating to S-expressions syntax and then you will see it true inner.

Second monster comes from Self and disgust in object. Many experienced folks lost there by creating loops with Object.prototype. They were thinking that JavaScript has class'es and that was their last mistake.

One and the only hero was Luca Cardelli, he saw it clearly, he fight it splitting object creature in pieces:

prototype class object
^
real class object
+
trait class object


Two

In the end of the day, I think that problem is more in writing but not JavaScript language. Ones you start programming it, it instantly become Zen. Programs creates programs, objects extended and contract in magical way, even functions not save in that world. It is meta programming and can not be controlled by human.

// Mix in composition helper
Object.extend = function(destination, source) {
  for (property in source) {
    destination[property] = source[property];
  }
  return destination;
}
Object.extend(Array.prototype, Enumerable);

// curry
Function.prototype.curry = function(object) {
    var method = this,
        copy = Array.prototype.slice,
        args = copy.call(arguments, 1);
    return function() {
        var newArgs = copy.call(arguments);
        return method.apply(object == null ? this : object, args.concat(newArgs));
    };
};
Function.prototype.$ = Function.prototype.curry;
function hello(a, b, c) {
    repl.print("I am " + this + " and got: " +
          [a, b, c].join(", "));
};
var f = hello.$("foo", 1, 2);
f(4);

The consequence that you have to face is really horrible. You freezing in time, in space, and in mental state.

Three

The way out is to take control back before coding aimlessly. Learn discipline, limit yourself. You can start today: Learn You a Haskell for Great Good! or do some Scala if you weak.

No comments:

Post a Comment