(Map|Set|WeakMap)#set() returns `this` ?
andrea.giammarchi at gmail.com
Wed Dec 5 11:02:15 PST 2012
let's summarize then:
1. ~Weak/Map#set and Set#add returns void
1.1 void as return is considered useless
2. cahinability looks like the most common pattern so those methods
should return `this`
2.1 example: return map.has(key) ? map.get(key) : map.set(key,
2.2 useful to set and send same object
2.3 chainability might lead to bad readability
3. Smalltalk returns the value with #put and few here think that should
be the best thing to return
3.1 example: return map.has(key) ? map.get(key) : map.set(key,
operation()); // as result of operation()
3.2 above example to simulate the equivalent of this common operation:
return obj.hasOwnProperty(key) ? obj[key] : (obj[key]
3.3 useful to set without addressing values generators/creators
returning them anyhow since the map reference is already known
3.4 Map and WeakMap are used as storages so always referenced while set
values do not have necessarily to be addressed (boring, problematic, global
pollution without strict, let vs var, common problems we all know with
obj.set(key, value) might reflect (obj[key] = value) semantic rather than
((obj[key] = value), obj) which is not semantic but apparently the most
used pattern with all libraries out there except Dojo that agreed on
Also HTMLElement#appendChild(newChild) where newChild is returned is more
familiar but sometimes we need to append more children so that `this` might
be more appropriate return.
Last, but not least, no pattern blocks anyone to reach same goals inline:
get the value
void: return map.set(key, value) || value;
this: return map.set(key, value).get(key);
value: return map.set(key, value);
get the instance
void: return map.set(key, value) || map;
this: return map.set(key, value);
value: return map.set(key, value), map;
On Wed, Dec 5, 2012 at 10:17 AM, Rick Waldron <waldron.rick at gmail.com>wrote:
> On Wed, Dec 5, 2012 at 1:04 PM, Mark S. Miller <erights at google.com> wrote:
>> On Wed, Dec 5, 2012 at 1:50 AM, Jussi Kalliokoski
>> <jussi.kalliokoski at gmail.com> wrote:
>> > My 2 cents against the windmills...
>> > I personally think returning `this` in absence of any meaningful value
>> > chaining in general) is a bad pattern. Chaining leads to worse
>> > (there's nothing subjective about this, if you have to scan the code to
>> > another page to figure out which object the code is interacting with,
>> > bad readability)
>> This is an excellent point, and has changed my mind. I return to not
>> supporting the "return this" for these cases. Thanks.
> In the cases that we identified as qualifying for the criteria that you
> established at the meeting, a "chain" will most likely only have one more
> method call or property access after .set()—any of the iterable producing
> methods (keys, values, entries), forEach, or the size property. But this
> also now makes any operation where I want to mutate the map/set and
> immediately do something with that object a two part exercise.
> "some people will misuse it" can be the argument for anything, that
> doesn't make it a strong or meaningful argument.
> cc'ing Doug Crockford, Erik Arvidsson, Alex Russell, Yehuda Katz to weigh
> in on this.
> es-discuss mailing list
> es-discuss at mozilla.org
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the es-discuss