Alternate (better) solution to the author's problem: Use enums
Alternate (better) solution to the author's problem: Use enums
document addObserver:observer forNotificationNamed:'load' isWeak:false.
xhr openURL:options url type:options type async:options sync negated[document addWeakObserver: observer forNotificationNamed: 'load']
and
[document addObserver: observer forNotificationNamed: 'load']
Assuming for a second, that the non-weak observer should be the normal case.
Given that more time is spent reading than writing code, autocomplete is only a partial solution to this problem.
foo(author: author, title: title, price: price)
The problem in the article is real, but it is the exception. If it's hard to see what 5% of your parameters mean, forcing 100% of them is probably a cure worse than the disease.
When I worked in Java with IntelliJ, the solution was to simply hover over the call, and the declaration would appear.
The disease is far worse than the cure. I frequently (at least once a day) find myself wishing code was written in the smalltalk style but I've only found the smalltalk style inconvenient on a small number of occasions.
ack '\[.*:' | less
to search my code-base for message sends with parameters. Scanning the first hundred or so results shows the pattern you mention to be rare.Dan Ingalls explains the syntax really nicely in this video:
https://youtu.be/P2mh92d-T3Y?t=600
With this type of syntax, most APIs turn into EDSLs all by themselves, and often read like fairly natural sentences.I ramble on a bit about the relationship between keyword syntax and (lack of) operator overloading:
http://blog.metaobject.com/2015/03/why-overload-operators.html xhr.OpenUrl(url, async: true); - (void)doMysteriousThingsWithParameters:(int)x :(BOOL)y :(NSString*)z;
I've come across this recently when creating an HTML5 <canvas> context object to be called from within JavaScriptCore. This needs to expose JS-friendly methods like: - (void)strokeRect:(CGFloat)x :(CGFloat)y :(CGFloat)w :(CGFloat)h;
- (void)arc:(CGFloat)x :(CGFloat)y :(CGFloat)radius :(CGFloat)startAngle :(CGFloat)endAngle :(BOOL)antiClock;Ah, or do you mean the name is just empty? How does that work on the implementation side? I.e. how do you refer to the passed arguments?
I prefer python approach, where you can set "optionally named" and "kwarg only" parameters" on a per function basis.
For a description, see https://www.python.org/dev/peps/pep-3102/
For internal but not external APIs. Enums create tight coupling and are not extensible. Booleans and Enums should be avoided for external APIs.
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. (someCondition? showFoo : hideFoo)();
You can also pass in arguments if they both accept the same parameters, eg. (someCondition? show : hide)(foo, duration); (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))(); 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 ;)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/