Learning a new language

[ disclaimer : programming language ]
I had started programming in C. After, making small running programs of sorting and data structures, i learned C++. There is a vast difference between paradigms of both languages - C is procedural and C++ is Object Oriented. However, i could not feel that jump in my programming mind, since i had not developed any real projects in C. So, one can say that i started with Object Oriented mindset.

I also learned Java, as that was the next "cool" thing. Learned little bit of JSP - for websites. Left it as i was not enjoying myself.1

This time it was JavaScript. wait.. [ but is this not that ugly, hackful language. well, it depends, like any different language it has its own peculiarities. If used in simple and elegant ways, it can do many works. ] So i started teaching myself JavaScript and particularly Node.js.


And it was a good experience. JavaScript is a event based programming kind of thing. It is single threaded. Now i would take these two things one by one. But before that why this is so great. For a developer if we just forget the performance, just simple development purposes, nothing is good then single thread. It makes life so easy. Programming is easy and debugging super-duper easy.

        [ but is not single thread a poor design. is it not the cause of all slowness evils.. ??  ]

well i have not yet finished.

Ok so programming is easy. but we are still hungry for performance. so here comes events. For the developer, everything works in single thread. But events would get fired when some background work like database query, file read IO works get done. One can give callbacks - functions that would get executed when the work is done. lets learn by a simple example.

The aim is to show "hello" to the user and then read from database and also show that message to the user.

var message  = "hello";
var dbCallback = function(msg) {
console.log(msg);     // <-- " world" would get printed here.
}
var makeSlowQuery = function() {
    // we have simulated a 2 second time out as a slow query.
    // magic of callback - it would get executed when db fulfills the query after 2 seconds.
    setTimeout(function(){
        dbCallback(" world")
     }, 2000);
}
makeSlowQuery();        // <-- made a very slow db request which is async.

console.log(message); // <-- "hello" would get printed here

Output:

hello world



Output:

hello world


world would be shown after 2 seconds.
so what is the big deal ?

Look carefully. hello gets printed in last line. In languages like c, cpp after last line of language is executed and if we have not written explicitly thread waiting code, program gets finished.

Not in JavaScript. There is always a thread or somthing waiting in the background waiting for some even to occur. When something happens, it push it to the main thread.

       [ but you said there is only one thread in JavaScript.]
For the developer, there is only one thread. All of the functions, data would be executed in one and only one main thread only.

In c, cpp, the flow of code is normally sequential. In Javascript, It goes through the code one time. Then it gets back to the code at the function which get called after the event fired and again goes down and down. And this happens forever, till there are more events.


This event based programming can make developing applications that require lot of IO much faster,
since the main thread is free. And this happens without any multi-thread headaches by the programmer.

Of course, one has to devote some time to get their mind around this kind of event-based programming. Looking at the sheer numbers of JavaScript developers and my personal experience, i think it has small  learning curve.


Learning a new language with a different paradigm is always refreshing.

Lisp here i come !!!

  1. I learned it few years back. It may be a very good platform now. Just heard, struct 3 has got async   IO.


Comments