3 ms·
Using an abstraction which increases the total amount of code could be worthwhile if it makes the difficult parts of your code simpler. Pulling an example out
by DougBTX 17y ago
Using an abstraction which increases the total amount of code could be worthwhile if it makes the difficult parts of your code simpler.
Pulling an example out of thin air, shorter total code:
var counter = 0;
function foo() {
log("Incremented");
counter++;
}
function bar() {
log("Incremented");
counter++;
}
Code with an abstraction which makes foo and bar shorter:
var counter = 0;
function increment() {
log("Incremented");
counter++;
}
function foo() {
increment();
}
function bar() {
increment();
}
Even longer code, but which encapsulates the counter variable, possibly making it easier to reason about:
var Counter = (function ()
{
var counter = 0;
return {
increment : function() {
log("Incremented");
counter++;
},
current : function() {
return counter;
}
};
});
function foo() {
Counter.increment();
}
function bar() {
Counter.increment();
}