Thursday, January 3, 2013

Avoid coding by exceptions

generally it is a good practice to avoid using exception for standard coding control, like instead of using if-statements. This hold for javascript as well. There is a lot of reasons for this, one being performance and another debugger breaking. Normally a debugger will stop/break on an exception and the Alfresco Rhino debugger does this as well. So the following code-snipplet from Alfresco server-side JS file: header.get.js should be rewritten:

header.get.js
...
const PREF_COLLAPSED_TWISTERS = "org.alfresco.share.twisters.collapsed";
...
function getTwisterPrefs()
...
response = eval('(' + result + ')');collapsedTwisters = eval('try{(response.' + PREF_COLLAPSED_TWISTERS + ')}catch(e){}');
if (typeof collapsedTwisters != "string")

{
     collapsedTwisters = "";
}


header.get.js (improved)
...
function getTwisterPrefs()
...
 if (response.org.alfresco.share["twisters"] && typeof(response.org.alfresco.share.twisters["collapsed"]) === string){
    collapsedTwisters = response.org.alfresco.share.twisters.collapsed;
}
else
{
    collapsedTwisters = "";

}

So now the debugger does not break there if the twisters are not defined...