Doing a bit of testing last week, I have found out that the source code for the Game Of Life JavaScript Implementation is not working with Internet Explorer 7 (IE7).
It turns out that IE7 doesn’t support the forEach
construct, and in while rewriting it, I noticed how the code becomes so much uglier and less readable, which led me to think about the programming language evolution.
Take a moment to compare the following two excerpts:
_.neighbours.forEach(function (neighbour) {
sum += +neighbour.alive;
});
versus
for (var i = 0; i < _.neighbours.length; i++) {
sum += +_.neighbours[i].alive;
}
Sometimes, the syntax changes can lead to huge improvements in coding and readability.
One great example is JavaScript’s forEach.
Now, while this might not seem like a big difference, I think it’s HUGE.
Not only we need to introduce a variable to iterate, but we also need to access the array itself. This takes the focus away from the primary concern, which is the current element that we want to work with.
It is so much nicer to work with the current member of the array instead of focusing on _.neighbours
in the loop.
Now consider a more complex example:
_.cells.forEach(function (row, i) { row.forEach(function (cell, j) { neighboursY = neighbourIndexes(j); neighbourIndexes(i).forEach(function (x) { neighboursY.forEach(function (y) { // this line cell.neighbours.push(_.cells[x][y]); }); }); }); });
versus
for (i = 0; i < size; i++) { for (j = 0; j < size; j++) { indexesX = neighbourIndexes(i); indexesY = neighbourIndexes(j); for (k = 0; k < indexesX.length; k++) { for (l = 0; l < indexesY.length; l++) { // against this _.cells[i][j].neighbours.push(_.cells[indexesX[k]][indexesY[l]]); } } } }
See the difference? In the second excerpt, I feel like I can’t see the tree from the forest. :)
Another great thing is that the syntax enables the easy refactorization, just take out the function, whereas you would have to rewrite the code.
All this points to me that the evolution of programming languages is a very good thing in the means of the way we see and work with the code.
Leave a Reply