5 ms·
Instead of booleans or enums why not have different methods for each value of the bool/enum, e.g.: openAsync(...), open(...)
by rdfi 11y ago
Instead of booleans or enums why not have different methods for each value of the bool/enum, e.g.:
openAsync(...),
open(...)
- skwirl 11y agoInstead of having different methods like open/openAsync why not have enums?
- ygra 11y agoDifferent names don't scale well beyond a single boolean argument.
- barrkel 11y agoI'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 11y 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 11y 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 11y 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 11y 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 11y agoYes (and toggleClass too), but they were only added in 1.3.