A Coding Insight

Someone asked me the other day why I had done something in a particular way.  Here’s an example and an explanation:


//setCookie() and getCookie() do exactly what you would expect them to do...

function setMode(nMode) {
 setCookie("EntryMode",nMode);
 document.getElementById("btnEntryMode1").style.background="";
 document.getElementById("btnEntryMode2").style.background="";
 document.getElementById("btnEntryMode3").style.background="";
 document.getElementById("btnEntryMode" + nMode).style.background="red";
}

function setMode(nMode) {
 setCookie("EntryMode",nMode);
 document.getElementById("btnEntryMode1").style.background="";
 document.getElementById("btnEntryMode2").style.background="";
 document.getElementById("btnEntryMode3").style.background="";
 document.getElementById("btnEntryMode" + getCookie("EntryMode")).style.background="red";
}

I had done something similar to the second example. 

The two versions do exactly the same thing – it’s not a trick question or anything like that.  And you can argue that #1 is actually simpler and more straight forward – and it is.

The question is, “why would you go for a function call to getCookie() when you just set it and you know the value you used to set it?  You already know the value you are going to get…

Explanation: In example two, if the button does in fact change colour, then you’ve got a visual confirmation that the cookie got set properly.  That’s all.

Advertisements

About combatdba

I'm a production DBA at a terabyte-class SQL Server Shop
This entry was posted in S.W.A.T.. Bookmark the permalink.

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