Is it a Module? Is it a Class? No it’s actually both…

It is possible with JavaScript to treat classes and modules as functions with dependencies using the function’s prototype. We can say that a class extends classes, but to make the similarities of modules and classes a little clearer I will say that a class depends on classes. We could also say that a module imports modules, or we could say that a module depends on modules.

Below is an example of what I’m going to call a “unit”, a unit can depend on other units and can be ran and used like a module, or used to construct objects like a class. This particular unit called `myUnit` has no dependencies and contains one method called `myMethod`.

(this.myUnit = function myUnit() {
    var self = {};
 
    self.myMethod = function () {
        return 'hello world';
    };
 
    return self;
}).prototype = {
    // Has no dependencies.
};
This second unit called `testMyUnit` is unsurprisingly used to test the unit defined above. This unit depends on `myUnit` and has one method called `run` which can be used to run the tests. The `run` method attempts to use `myUnit` as a module by executing `myUnit`and calling a method, it also attempts to use `myUnit` as a class and this can be done two ways, (1) using the `new` keyword, and (2) using `Object.create`. Calling this units `run` method should return 0 if all tests pass.
(this.testMyUnit = function testMyUnit() {
    var self = {};
    var myUnit = testMyUnit.prototype.myUnit;
 
    self.run = function () {
        var expectResult = 'hello world';
        var testModule = myUnit().myMethod() === expectResult;
        var testNew = (new myUnit).myMethod() === expectResult;
        var testCreate = Object.create(myUnit()).myMethod() === expectResult;
        
        // Returns the number of failed tests.
        return Number(!testModule) + Number(!testNew) + Number(!testCreate);
    };
 
    return self;
}).prototype = {
    // Depends on myUnit.
    myUnit: myUnit
};
 
testMyUnit().run();
I think this an interested pattern and could be of some use to JavaScript developers.

Future Code Completion

Just a short post about something I was thinking about 🙂

Wouldn’t it be great if you were writing some code and as you did, it not only gave you code completion suggestions from inside the file but if it could recognise that you were writing a function exactly like or similar to something you or someone else had already written before and allowed you to easily import that and use it?

I hope this is the future of code completion (or some similar feature) as I think it would save massive amounts of time. I think it would also change the way we developed code, I would hope it would popularise smaller modules that are well tested and documented.

Here is an example of what I mean https://gist.github.com/ryansmith94/6672194.

Frickin’ JS Modules

Writing modules in JavaScript that are compatible with the sea of JavaScript module patterns is just a pain in the ass. I’m sick of writing boilerplate in all my code, yeah I could make a library but then it’s another library trying to solve a language problem.

To fix the problems with JavaScript in general and move forward, I think we should fix the problems well and just screw the old browsers. If users start noticing that things aren’t working in their browser I think they will soon realise it’s time to upgrade and they will most likely upgrade to a browser that updates automatically. Let’s just move on already!

I’m not entirely sure how the problems with modules/global scope can be solved but I do know that…

  1. It would be nice if I could test both public and private functions in a module/file without injecting test code.
  2. I want to be able to use code that I have written in other modules/files.
  3. I don’t want to be writing boilerplate code or using lots of additional syntax.

Possible Solution

One possible solution could be running each file in it’s own scope and allowing other files to import that scope. If that was the case, to satisfy my three conditions above a project may look something like this…

add/src/core.js

var privateAdd = function (x, y) {
    return x + y;
};
var publicAdd = privateAdd;

add/test/core.js

var add = import '../src/core.js';
// Test add.privateAdd;
// Test add.publicAdd;

add/module.js

var {publicAdd} = import 'src/core.js'; // Using destructuring assignment.

src/main.js

var console = import 'console'; // Native.
var add = import '../add/module.js';
console.log(add(10, 11)); // Logs 21 to the console.

Notes

  1. I think URLs should be used because it eliminates the need to place restrictions on module names.

Get to know the Actor Model

I want to quickly discuss the actor model’s importance. No I’m not about to make a post about Jeremy Renner and Scarlett Johansson (for all those Avengers fans, just because I’m cool like that :P).

First things first, what is the actor model? Well other than being awesome, it’s a mathematical model of concurrent computation. The model consists of actors, and actors can do three things (known as the three axioms – love that word). The three axioms are that an actor can create more actors, send messages to actors that it knows, and designate what to do with the next message. That’s it… crazy right?

Now if you’re mind isn’t completely blown right now and you can still comprehend my awesome words then brace yourself. Computation is any type of calculation, which means… we can use it to model a crazy range of things. This includes networking (where computers are actors), programming (where functions might be actors), communication in real life (where people or groups of people might be actors), the list goes on. Think about it, you can now model a piece of code like a group of people. So good.

Want more? Of couse you do, so I’m going to give you some links, essentially a short bibliography for this post (no it probably doesn’t contain everything I’ve read or watched that have helped me write this post). Please check out the second link below, it will not disappoint, they even talk about chickens cutting off their own head.
Doug Crockford’s talk about promises
Carl Hewitt discussing the actor model
What is computation?
What is the actor model?
What is an axiom?

Synchronous is ridiculous

I have seen both asynchronous and synchronous programming models used in programming languages that I have used. Asynchronous I/O makes your programs run far quicker. It is possible to implement one model using the other in most cases, but honestly asynchronous programming is far better.

In the asynchronous model your programs do not wait to receive data instead you add a callback and do other stuff while you wait. It’s like waiting for an important phone call, you probably don’t stand next to the phone all day waiting for it to ring, you’re far more likely to do other things and then answer the phone when you see it ring because that’s a better use of your time, it’s more productive.

Now imagine for whatever reason your phone doesn’t show you missed calls and your important call comes but somehow you don’t hear it. The problem in the synchronous model is that you’re still sat there waiting for the call. Whereas in the Asynchronous model you just carry on doing other things. Also you might never get the call which would result in the same situation. You could of course, stop waiting for the call and decide that it’s not going to happen, but this still means that you have been sat there however long you were willing to wait doing nothing in the synchronous model, whilst Mr/Mrs Asynchronous has had a shower, hoovered the house and cleaned the windows. Get the point?

The most common mistake a language can make

Whilst learning more programming languages, it has become fairly obvious to me that the majority of languages have got syntax wrong. To improve the performance of the language, some languages increase the size of the syntax. Just no!

As an example, in English I can tell you that a person usually has two legs, two arms, a body and a head, from now on you may refer to something fitting this description as being a person. Therefore allowing you to reuse a single word instead of the entire definition. Hopefully you can see that this definition improves functionality and performance of the language without increasing syntax, in fact I have defined person using the syntax. Much better! This is a simple idea and we use it all the time in language.

Now you might be saying, “but surely we have to add syntax in programming languages to reduce processing speed”. No! Of course I’m not suggesting that we start implementing division and multiplication using a for loop when the processor can obviously optimise the calculation. Therefore we should use native functions to improve performance as seen in JavaScript. This is the best way to improve performance for two reasons. One, the syntax is not changed therefore programmers do not have to learn anything new to use the feature. Two, backwards compatibility, old implementations of the language will still work and the same functionality can be provided by libraries if the feature is not available.

Native functions should ONLY be used if it improves performance. However it may be a good idea to add some built-in functions to the language. For example, if everyone is using a library it might be good idea to provide that library as part of the language. Otherwise, libraries should be made to improve production time, obviously in a popular language the majority of libraries will be made by users of the language.

I hope this makes sense. Native functions and libraries are good, more syntax is bad. A language with simple syntax is far easier to learn, a simple language becomes a powerful language when you add great libraries and native functions.