S
Scott Sauyet
dhtml said:Gotcha. No, I don't think overloading's always bad.
Okay, I'd love to hear other people's ideas as well on this.
I try to minimize complexity in methods. Conditionals and loops add
complexity, too but sometimes it makes sense to use them.
The distinction I've been trying to make, though, is between
complexities within the implementation and complexities within the
interface presented. When there's a tradeoff to be made between the
two, I almost always will choose to keep the interface complexity
low. Of course it's better if neither is complex, but still there is
often a choice to be made between these.
Invoice.prototype.addLineItems = function(items) {
var myItems = this.items;
items = isArray(items) ? items : [items];
each(items, function(item) {
myItems.push(item);
});
};This is a form of overloading. The function can take a single
LineItem parameter or an array of LineItems. To me, since the systems
that feeds me does not always distinguish between single items and
arrays of them, it's much cleaner to have the isArray check, and hence
the overloading, in this function.
That's what `Array.prototype.concat` does. So you can get rid of the
entire function and just use `concat`. Like so:
| myItems.concat(items);
I can't win. :-(
I swear, if it weren't for a non-disclosure agreement, I'd just post
some actual code from my current project. pretend instead of
| myItems.push(item);
I wrote
| doSomethingDifficultThatDoesntHaveAnEquivalentNativeOneLiner(item)
I'm taking the Worse is Worse side in the classic Richard Gabriel
debate. [1]
Haha, yes - I like that "less is more" stuff. And knowing how and
when to say "no" when asked to provide spurious features.
That latter is something I struggle with far too much. It's hard when
those asking for the features are the ones signing the checks!
-- Scott