2 ms·
I've seen this in practice a lot in jQuery and many people with jQuery experience. You end up with methods like showFoo / hideFoo, enableFoo / disableFoo, and t
by barrkel 12y ago
I've seen this in practice a lot in jQuery and many people with jQuery experience. You end up with methods like showFoo / hideFoo, enableFoo / disableFoo, and then you have to continuously write if-statements like this:
if (someCondition) {
showFoo();
} else {
hideFoo();
}
Instead of:
setFooVisible(someCondition);
The point being, creating lots of different methods for each variant reduces one's ability to use other abstraction tools, like variables, to control parameters.
- chriswarbo 12y agoFunctions are first-class values, so what's wrong with: (someCondition? showFoo : hideFoo)(); You can also pass in arguments if they both accept the same parameters, eg. (someCondition? show : hide)(foo, duration);
- masklinn 12y agoExcept they're methods so you have to write (someCondition ? foo.show : foo.hide)(); except the language does not auto-bind so you have to write (someCondition ? foo.show : foo.hide).call(foo); or maybe (someCondition ? foo.show.bind(foo) : foo.hide.bind(foo))();
- chriswarbo 12y agoOr: foo[someCondition? 'show' : 'hide'](); Although looking up functions using raw strings is less than elegant. I'd be tempted to abstract the pattern using a function, but at that point I'd be doing functional programming anyway and wouldn't be using methods ;)
- masklinn 12y ago> I've seen this in practice a lot in jQuery and many people with jQuery experience. A bit odd given jQuery often provides both options e.g. show[0]/hide[1] and toggle(bool)[2]. [0] http://api.jquery.com/show/ http://api.jquery.com/show/ [1] http://api.jquery.com/hide/ http://api.jquery.com/hide/ [2] http://api.jquery.com/toggle/#toggle-display http://api.jquery.com/toggle/#toggle-display
- barrkel 12y agoYes (and toggleClass too), but they were only added in 1.3.