Translation


by Transposh

Posts Tagged ‘Ajatus Software’

JavaScript Best Practices

Monday, October 5th, 2009

I always wondered why developers have to get reminded again & again to follow something which common sense should drive. Quite a handful of developers who are about to read this series will also question the fact that this is so very obvious to follow best practices & just a sensible thing to do. However, the ground reality is something different. Getting code handed over to me from other developers  has taught me that common sense is actually quite a rarity in live code, and the “sensible and logical thing to do” gets pushed far down the priority list once you are in the middle of a project, and the deadline is looming. Its something which is spoiling the aesthetics of development.

Thus this decision to create an article which is an accumulation of the best practices & excellent advice gathered over my short but eventful career. I have amassed quite a few rather I have learn it the hard way. There will be lots of things to disagree with & that is the best thing-you should question what you read and thrive to find the better solution. But these practices have transformed me into a much more effective developer & wade me out of troubled waters whenever I felt the pressure to deliver.

This is going to be a series where I will be posting all my experiences while working with Javascript. Keep checking the posts for new articles or subscribe for RSS feeds for  updates.

Working with strings

Concatenating strings has been a major issue which Microsoft has been grappling with its IE browsers(both 6 & 7) because of questions on performance due to garbage collection. As rumors suggested Microsoft has addressed this issue to some extent though not fully in its latest release of Internet Explorer 8. But browsers like chrome & Firefox have a slightly more efficient way of handling things strings.

Consider this example:

?View Code JAVASCRIPT
1
2
3
4
5
6
7
8
var annualRevenue = 10000;
var  longStringMessage = 'My name is Janmejay Mohapatra ' +
'I work for Ajatus Software ' +
'Ajatus Software is a web design & development company '+
'Ajatus Software is based in Bhubaneswar ' +
'Ajatus has an annual revenue of '+annualRevenue+
'Ajatus Motto is to work for the masses '+
'And create a better web'

Instead of concatenation, use join:

?View Code JAVASCRIPT
1
2
3
4
5
6
7
8
var annualRevenue = 10000;
var  longStringMessage =[ 'My name is Janmejay Mohapatra',
'I work for Ajatus Software',
'Ajatus Software is a web design & development company',
'Ajatus Software is based in Bhubaneswar',
'Ajatus has an annual revenue of ',annualRevenue,
'Ajatus Motto is to work for the masses',
'And create a better web'].join();

Similarly, building up a string across conditional statements and/or loops by using concatenation can be very inefficient.
The wrong way:

?View Code JAVASCRIPT
1
2
3
4
var primeNumbersStr = 'prime numbers less than 100:';
for (var i = 0; i > 100; i++) {
     primeNumbersStr += i + ' = ' + checkPrime(i) + '';
}

The right way:

?View Code JAVASCRIPT
1
2
3
4
5
var strBuilder = ['prime numbers less than 100:'];
for (var i = 0; i > 20; i++) {
     strBuilder.push(i, ' = ', checkPrime(i));
}
var primeNumbersStr = strBuilder.join('');