Parameter Management

Sometimes complex function may have excessive number of parameters. This does cause the usability issue for function callers. There is an easy way to handle this by put all parameters into an associative array. Here is an example in JavaScript:

; function example (config)
{
; var p = {para1:0, para2:'', para3:1}
; for (var i in p) p[i] = ('undefined'==typeof config[i]) ? p[i] : config[i]

// Use p['para1'] instead para1 as variable in codes.
}

The function caller would be:

; example ({para2:'newArgument'})

The beauty of it is not only the unneeded parameters can be omitted, but also sequence of the parameters, since it is an associative array. In addition, it does provide a meaningful array key.

This concept can be used in almost all languages.

No comments:

Post a Comment

Labels