Showing posts with label javascript. Show all posts
Showing posts with label javascript. Show all posts

Saturday, September 3, 2011

A function, JavaScript engine and the single var pattern to declare variables walk into a pub

Addy Osmani had published a very nice post that summarizes various problems found when doing JavaScript code review. Most of the points there worth paying attention to. However, there is one point there that he got completely wrong. When he discussed the problem of variables declared all over the place within a function scope, he suggested to use the "single var pattern to declare variables", basically write the variables declarations like this:

var someData = "testing",
     otherData = "data1",
     moreData = "data2";

This kind of coding will get you into trouble. Try to find the difference between the code above and the code below:


var someData = "testing"
     otherData = "data1",
     moreData = "data2";

Found?

let's add to the first snippet the scope of each variable:

var someData = "testing", // local within the function
     otherData = "data1", // local within the function
     moreData = "data2"; // local within the function

Now let's do the same for the second snippet:

var someData = "testing" // local within the function
     otherData = "data1", // global
     moreData = "data2"; // global

Big difference, but why?.

Answer: Take a look at the "testing" string. In the first snippet  it is followed by a comma, whereas it is not there in the second snippet. That's all, one comma.

Both snippets are valid JavaScript code, it just happens that even though JavaScript has a C like syntax, it does not require to have a semi-colon at the end of each line, the JavaScript engine will add it if missing.

So the second snippet is actually:


var someData = "testing"; // local within the function
     otherData = "data1", // global
     moreData = "data2"; // global

(note the semi-colon after the "testing").

One more thing to know about JavaScript: when declaring a variable inside a function without the var keyword, it is defined as a global variable.

Now go and find that little comma somewhere in your code, at 1:00 am , a few hours before delivery.

I prefer the simpler approach to define variables, and do the following:


var someData = "testing"; // local within the function
var otherData = "data1"; // local within the function
var moreData = "data2"; // local within the function

You can say that it is less elegant, but it is much less error prone, much more descriptive of what you want, can save hours of looking for a comma and simply put, it is just a simpler code. Just remember that beauty is in the eyes of the beholder, and to me, in code, simplicity is beauty .

Sunday, February 27, 2011

Equality, transitivity and JavaScript

One of the simplest ways to explain transitivity is using the equality operation, by saying that if A equals B and B equals C then A equals C. This is commonsense stuff, how can it be different.

Well, it can.

In JavaScript, the '==' (double equal) does type coercion, and therefore ends up in non-common-sense situation where equality in not transitive. See in the following example where  A equals B, B equals C, but still, A does NOT equals C :

'0' == 0 // true - the string 0 (zero) equals the number 0
0 == '' // true - the number zero equals empty string
'0' == '' // false - the string 0 (zero) does not equal empty string


seems that common sense is over appreciated...

Monday, November 15, 2010

Re-using Java

The first programming language I worked with as a professional was Java. It seems to me as an excellent language to start learning real software development (as oppose to learn how to program, for which c is my language of choice).
It teaches you to think also on the architecture of the software (as it is reflected in the package structure). It forces you to understand various design patterns (some of which comes along with the language). It introduces you to the need of working with frameworks (the large set of Java packages, as well as external libraries), and on top that, the rigid typing with the tough compiler are just like two good parents that show you the where are the limits and does not allow you to cross them. I've worked with Java for about 6 years, and learned a lot from it.

Time had passed and I started another project, in which I had to develop in JavaScript. At first, I just wrote Java code in JavaScript (the fact that I worked with the Dojo toolkit made it really easy). Then, I started to learn the difference between the languages, I started to see the power that JavaScript gives you, was overwhelmed by the breaking of almost any limit that Java had taught me. I got to a point where I write JavaScript code in JavaScript,  use its flexibility and its dynamic nature, as well as the various brilliant programming concepts within it.
I crossed the limits that Java taught me, and allowed myself to do so based on the belief that I'll know when to stop and what lines not to cross.
It is so much easier to write software in JavaScript if you know what you are doing (especially how does your code is really read by the machine).
It is much easier to make a big mess too, this must alway be in mind - no parents, you can run freely, nobody will prevent you from failing and falling.

A short while ago I had some free time on my hands, and wanted to learn something new, so I developed a small android application.
In Java.
Initially I thought that it would be just like riding a bicycle, but it turned out to be more like riding a rusty old bicycle, where you cannot almost turn the wheel, all that with one hand tied behind the back.
The package structure is still there, as well as the rich set of libraries and Java packages. BUT, the rigid type system and the compiler are not a directing parents any more, they are more like a bureaucratic figure that forces you to do what you don't want (in order for him to approve your request).
The no dynamic nature make no sense now, sometimes I wanted to polity ask the compiler to move aside and just trust me - I know what I am doing.
It is so burdensome to write software in Java.

Java is great to start with, but it takes you just up to a certain point. And from there, if you do want to expand your abilities, by not just learn new syntax, but rather learn another way of thinking, go with JavaScript, you'll never want to go back.

Thursday, June 24, 2010

A lecture about JavaScript

I was invited to give a lecture about JavaScript in the course "Programming Languages" in the CS faculty of the Technion IIT.

Here are the slides: