Forgiveness is a virtue (of Javascript)

Forgiveness and tolerance are Javascript’s greatest virtues: the language does everything it can to prevent errors and makes erroneous code work as much as it can. This is in huge contrast to ‘prima donna’ languages like C++ or Java(*1), which whine and cry at the slightest hint of error and do not hesitate at all to halt execution at the first opportunity.

Let us consider some examples. In the example below, we have a function that takes 1 argument. In the last line, I call the same function without any argument and the code works without any complains from the Javascript interpreter.

function foo(count) //a 1-arg function
{
    for (var i = 0; i < count; i++)
        console.log("count " + i);
}
foo(3); //call the function an argument
foo(); //***call the same function with no argument ***

Output

objects@london(umermansoor) $ node forgivenessVirtue.js
count 0
count 1
count 2

In fact the code above still works if we remove all semicolons (;)

function foo(count) //a 1-arg function
{
    for (var i = 0; i < count; i++)
        console.log("count " + i) //no semicolon
}

foo(3) //no semicolon here either
foo() //double whammy. Everything works

Note: Semi-colons are optional in Javascript and are required only if two statements occur on the same line.

Let’s look at a slightly advanced example. In Javascript, a constructor is a function that returns a reference to an object when called with new. E.g. var object = new SomeObject().

function Console(make) //a constructor
{
    console.log("The current make is: " + this.make + ". Setting make to: " + make);
    this.make = make; //setting make property of the object
}

var gamingConsole = new Console("Wii-U"); //normally that's how a constructor is called

Console("PS3"); //**calling a constructor directly, no complaints.

Note: If you are curious how the second example works when the constructor is called directly and there is no context for the this keyword. The answer is that Javascript uses the most global context in this case, normally the window object for web pages.

Why is Javascript so forgiving?

Javascript is THE language of the web. It is by far the most, popular language on the planet. I can even dare to bet that if you randomly pick two sites, there is a very high probability that both sites will be running Javascript in some form or fashion.

Javascript was designed to just work and this is the reason we don’t see sites running Javascript crash completely in the presence of errors. Your clients don’t come running to you complaining that the site is down entirely if there are Javascript errors. Please don’t get me wrong: Javascript programs can still contain irrecoverable errors, for example, missing closing “)” in a for statement. The point I’m trying to make is that Javascript will try very hard to prevent errors, but if errors are unavoidable, it will at least run the program as much as it can.

Javascript is very easy to learn and is fun to work with. I recall my first experience with Javascript when I was able to write a miniscule “Todo” program (or something like that) within an hour of first starting the language and the program just worked on the first try. This again is in sharp contrast with Java, which is very strict when it comes to discipline. When I started Java, for quite some time, I found it very difficult to write programs in plain text editor say vim without the assistance from an IDE say Netbeans, and run them without any syntactical errors on my first attempt.

Now back to the question: In my opinion, Javascript had to be forgiving in order for it be to widely adopted as the de-facto standard language of the web. This freedom however was mostly not intentional. Javascript is an outstanding Object Oriented (OO) language, however, it has more than its share of bad. A lot of different ways of doing the same thing, many different flavors, I can go on for hours talking about the bad parts of the language. In fact, there is an entire book on this subject: JavaScript: The Good Parts.

Summary:

Whether the freedom that Javascript lends to developers is a good thing or bad depends on the perspective. Beginners using Javascript as their first language love it. Veteran developers coming from background of disciplined language such as Java, may find themselves frustrated due to the fact that Javascript does not forces developers to behave themselves.

It seems to me that the freedom in Javascript is well balanced on the spectrum of most expressive languages like Perl and least expressive like Java. Javascript is an amazing OO language which is fun to work with, easy to use, and with the arrival of server side Javascript based platforms like Node.js, it can be used as the only language (not counting HTML + CSS) to furnish complete sites. (As opposed to PHP + JS, or Python+JS).

*1: Oracle/Sun Java is an extraordinary OO language. Java and Javascript were designed for two very different purposes, had different audiences and goals. Both are very good and popular in what they do. Context is the keyword here.
 

~~~ Ignore below this part ~~~~

About these ads
About

I'm Umer Mansoor , the author of this blog.

Tagged with: , , , ,
Posted in javascript

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: