Array.prototype.remove(item)

Man Hoang jolleekin at outlook.com
Thu Oct 11 09:54:02 UTC 2018


### 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.**
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20181011/13974f15/attachment.html>


More information about the es-discuss mailing list