Array.prototype.remove(item)

Andrea Giammarchi andrea.giammarchi at gmail.com
Thu Oct 11 09:59:37 UTC 2018


TBH, for consistency sake with everything else in JS, I'd call the method
`delete`, since it's the one that returns `true` or `false` in pretty much
every other case.

On Thu, Oct 11, 2018 at 11:54 AM Man Hoang <jolleekin at outlook.com> wrote:

> ### With `Array.prototype.remove(item)`
>
> Code is clean and concise.
>
> ``` js
>
> if (array.remove(item)) {
>
>     // [item] was removed, do something here.
>
> }
>
> ```
>
>
>
> ### Without `Array.prototype.remove(item)`
>
> Code is verbose or duplicated (everyone has their own version of
> `removeArrayItem`) => **WASTE**.
>
> ``` js
>
> // Using `indexOf` and `splice` directly.
>
>
>
> const i = array.indexOf(item);
>
> if (i >= 0) {
>
>     array.splice(i, 1);
>
>     // [item] was removed, do something here.
>
> }
>
>
>
> // Using a helper function.
>
>
>
> if (removeArrayItem(array, item)) {
>
>     // [item] was removed, do something here.
>
> }
>
> ```
>
>
>
> ### With `Set.prototype.add` returning a boolean
>
> Code is clean and concise. Together, `add` and `delete` are a perfect pair.
>
> ``` js
>
> if (set.add(item)) {
>
>     // [item] was added, do something here.
>
> }
>
>
>
> if (set.delete(item)) {
>
>     // [item] was deleted, do something here.
>
> }
>
> ```
>
>
>
> ### With `Set.prototype.add` returning `this` (almost, if not always,
> useless)
>
> Code is verbose or duplicated (everyone has their own version of
> `addItemToSetIfAbsent`) => **WASTE**.
>
> ``` js
>
> // Using `has` and `add`.
>
>
>
> if (!set.has(item)) {
>
>     set.add(item);
>
>     // [item] was added, do something here.
>
> }
>
>
>
> // Using `size` and `add`.
>
>
>
> const oldSize = set.size;
>
> set.add(item);
>
> if (set.size > oldSize) {
>
>     // [item] was added, do something here.
>
> }
>
>
>
> // Using a helper function.
>
>
>
> if (addItemToSetIfAbsent(set, item)) {
>
>     // [item] was added, do something here.
>
> }
>
> ```
>
>
>
> My point of view is JS and the web itself were not designed for app
> development. They have constantly been hacked to do so. If they had been,
> people wouldn’t have wasted too many resources creating workarounds (not
> solutions, not technologies) such as jQuery, Ember, Knockout, Polymer,
> Angular, React, Vue, GWT, CoffeeScript, TypeScript, and so on. No matter
> how we change JS, its inherent problems remain because we cannot introduce
> breaking changes. (Please do not argue about this as it will lead to
> nowhere)
>
>
>
> Having said that, let's get back to the main point of this thread and this
> whole website: **proposing ideas to make JS better (or less terrible)**.
> For me, changes such as adding `Array.prototype.remove(item)` and modifying
> `Set.prototype.add` to return a boolean may seem small but would have a
> huge impact when multiplied by the number of usages.
>
>
>
> **One small improvement repeated many times makes a big improvement.**
>
> **Many small improvements combined make a big improvement.**
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20181011/d3efcfe9/attachment-0001.html>


More information about the es-discuss mailing list