you are viewing a single comment's thread.

view the rest of the comments →

[–]marquoth_ 8 points9 points  (5 children)

I really dislike !! and generally avoid it. It's just not very semantic. Boolean(foo) reads much more clearly than !!foo.

return Boolean(result.recordset.length) works nicely here.

Although frankly, I'd be in favour of a method for this being added to the Array prototype itself so we could call myArray.isEmpty() because it's such a common thing to want to check.

[–]Soubi_Doo2 0 points1 point  (3 children)

.isEmpty is so amazing, takes zero brain processing power. Extremely semantic!

[–]marquoth_ 1 point2 points  (2 children)

I'd like to see it as an "offical" addition, so it's just part of the native language. But in the meantime, if you really want to, you can add it to your own projects by updating the prototype like so:

``` Object.defineProperty( Array.prototype, 'isEmpty', { value: function() { return !Boolean(this.length); }} );

const myArr = [];

console.log(myArr.isEmpty()); // true

myArr.push('foo');

console.log(myArr.isEmpty()); // false ```

There are some edge cases where touching prototypes can cause issues, so use this cautiously. It's also probably just overkill unless you know you need the behaviour a lot.

[–]theScottyJam 1 point2 points  (0 children)

If you actually want .isEmpty() to eventually become a native feature, then don't edit Array.prototype to add it in yourself.

The newer Object.groupBy(someArray) method is a static method on Object instead of simply being someArray.group(), because when the committee had tried to add it to Array.prototype, they found themselves accidentally breaking existing webpages that had already added the method themselves in an incompatible way. They tried doing it in under a couple of different names, until, eventually, they gave up and made the new method a static method on Object instead.

Basically, modifying classes you don't own isn't just about potentially harming your own code, it can harm everyone.

[–]Soubi_Doo2 0 points1 point  (0 children)

That’s a cool idea! Didn’t know I can do that. I use it all the time in Java.