Tuesday, 7 October 2014

Removing the Repetition in Code (The Array)


After spending some time tidying up a predecessor's javascript code I feel the need to document my findings. Any good developer will know all of this stuff, but while I'll go through explaining how to reduce the number of lines in your code, it's worth bearing in mind that we need to balance this with the idea of avoiding obfuscation.  

Obfuscation is the act of creating code that is difficult for humans to understand. Wikipedia suggests this is done deliberately, but I think that sometimes the best intentions go awry.

The Code as I found it

A function had been written to hide a large collection of bitmaps used to indicate validation failures on a web form.

function showimg(imgId,imgAct) {
    a = document.getElementById(imgId);
    if (imgAct == 'show' ) {
    } else {

This was called in the main processing block in the following way:-


//....loads more lines like this!!


Now if we ignore the fact that the showimg() function isn't very robust (it will generate an error if an object name isn't found in the DOM), it seems like an OK way of doing things.

The problem I had was that there were 124 of these calls, probably the result of a copy-&-paste frenzy. And while there's nothing actually wrong with doing this, it makes the code difficult to maintain. (and annoys people like me)

Let's say at some stage that we wanted to modify the parameters passed to the showimg() function. All of a sudden we have over a hundred lines to change. It might have started out simple, but now it's a complete pain!

How do we make it better?

The answer is to use an array to hold the image IDs, and then use a loop to process them.

I'd only use it if we get more than a handful of calls because the added complexity would hinder more than it helped. But, with 124 lines it's well worth bother, so first of all we'll define an array and fill it using the data from the original calls:

var imgArr = ['imgoriginator','imgeffectdate','imggroupnumber','imggroupname','imgaccountnumber','imgaccountnumber1','imgaccountnumber2',  'imgaccountnumber3','imgaccountnumber4','imgaccountnumber5','imgbranchname',

Then we have a simple 'For' loop:

for (var i = 0; i < imgArr.length; i++){

... and that's it!

If you need to add further field IDs it's a simple task of adding them to the array definition. There's no need to worry about getting the calls right, they just do the same as all the others.

An Alternative

Some of you will probably have your own ideas about how to improve this code.

One method to remove the array definition altogether requires you modify the web page to set each of the image class names to 'validate',... yes all 124 of them! And then have code that builds the array directly from the DOM..

var imgArr = document.getElementsByClassName('validate');

The loop needs a slight tweak because the array is now an array of tag objects, or we could do away with the showimg() function call and perform the hide within the loop..

for (var i = 0; i < imgArr.length; i++){

Can't get much simpler than that, and maintenance is easier still, but it's now not so clear how it works! 

I'll leave you with a joke that Microsoft recently tweeted...

Q: Why did the programmer quit his job?

A: Because he didn't get arrays!