Init-Time Branching: Minimizing computing as far as possible. For example browser features doesn't change over a session. Instead of checking browser features every time it's better to compute them once and according to that, update other utility function
Memoization: If return value of a function never change for given inputs, it's better to cache them in function as hashtable of serialized input params and outputs. Since functions are objects they can have there own properties and methods.
Configuration Object: It's better to use single object parameter against, multiple input parameters. As project grows, the requirement grows and number of input parameters grows. It's not a good idea to send too many parameters to a function, it's hard to maintain as well. Hence we can send an object which has key value pairs of input names and parameters, so that it's easy to add or remove parameters. Also we can skip optional parameters easily.
Currying: Partial Apply. We can transform an existing function to return another function when it has less number of parameters, and hardcode sent parameter so that we don't have to send that again. Here is an example:
var multiply=function(a,b){
var olda=a, oldb=b;
if(oldb===undefined){
return function(newb){
return olda*newb;
}
}
}
var multiplyby3 = multiply(3)
multiplyby3(4) //12
var multiplyby9 = multiply(9)
multiplyby9(4) //36
No comments:
Post a Comment