« [News] Java on Apple's new iPhone? | Main | [Tip] Smart client connection troubleshooting »

January 22, 2007

[Best Practices] Efficient coding guidlines

by David Workman
Data Mosaic

Picture_5_2

It has been a while since I've posted a sample of abstract code. In this example I have three separate check box fields which I use to set which search criteria to use. Only one check box can be active at a time and all three can be empty.

The most direct way to do this would be to have a separate method for each check box onChange() event that simply clears the other two check box fields. The most direct way unfortunately can be tedious if you have a lot of check boxes and/or if you were to add/delete a check box from the list. Maintaining a bunch of methods that all essentially do the same thing is not ideal.

A big step in efficiency is to call one method from all the check box onChange() events. This allows you to manage all of your objects in one method:

//get clicked name
var clicked = application.getMethodTriggerElementName()

//get value of clicked checkbox
switch (clicked) {
    case "ff_30" :
        var value = globals.CLT_FF_log_day_flag
        break
    case "ff_31" :
        var value = globals.CLT_FF_log_week_flag
        break
    case "ff_32" :
        var value = globals.CLT_FF_log_month_flag
        break
}

//set all fields to unchecked
globals.CLT_FF_log_day_flag = null
globals.CLT_FF_log_week_flag = null
globals.CLT_FF_log_month_flag = null

//set checkbox to original value
if (value == 1) {
    switch (clicked) {
        case "ff_30" :
            globals.CLT_FF_log_day_flag = 1
            break
        case "ff_31" :
            globals.CLT_FF_log_week_flag = 1
            break
        case "ff_32" :
            globals.CLT_FF_log_month_flag = 1
            break
    }
}

application.updateUI()

The downside to referring to all of the objects explicitly is that it still takes a bit of work to accommodate change. This makes it a bit of a chore to reuse this code somewhere else as you have to change all of the references. Using naming conventions the following code is much easier to maintain and adapt:

//get clicked name
var clicked = application.getMethodTriggerElementName()

//get value of clicked checkbox
var provider = elements[clicked].getDataProviderID().split(".")
var value = globals[provider[1]]

//set all fields to unchecked
var count = 3
for ( var i = 0 ; i < count ; i++ ) {
    
    var x = elements["ff_3" + i].getDataProviderID().split(".")
    globals[x[1]] = null

}

//set checkbox to original value
globals[provider[1]] = (value) ? 1 : 0

application.updateUI()

Finally, notice that I combined multiple statements by taking advantage of the object hierarchy (getDataProvider().split(".")) and used specific JavaScript language shortcuts (globals[provider[1]] = (value) ? 1 : 0) to arrive at the final result.

Summary: use the following guidelines to write efficient code:

  • combine identical functioning methods into one method
  • reference and process objects abstractly using naming conventions
  • combine multiple functions into single statements
  • use the full range of the JavaScript language

Happy coding!

| Posted by David Workman on January 22, 2007 at 01:00 PM in Best Practices | Permalink

Comments

Post a comment