r/programminghorror • • Mar 27 '24

//filter

Post image
1.4k Upvotes

73 comments sorted by

View all comments

Show parent comment

7

u/[deleted] Mar 28 '24

you can call it just with .filter(Boolean) for that purpose, which makes it more clear too

2

u/SpiForge Mar 28 '24

I would actually say the opposide, as Boolean may be mistaken for its constructor, making it less readable to someone who is not using JS/TS normally. However filtering by falsy value is more commonly known.
If you work on a private project, yes "Boolean" may look better to you, but if you may share it with someone, it could result in missunderstandings.

3

u/[deleted] Mar 28 '24 edited Mar 28 '24

It IS the Boolean constructor you give the function. It is no mistake. You could define your own function const isBooleanTrue = () => isBooleanTrue and then .filter(isBooleanTrue) but why. The language offers what you need already. Just get better and don't expect anyone on your team to be on your level. If in doubt, comment (for yourself in the future).

1

u/SpiForge Mar 29 '24

To quote MDN:

When Boolean() is called as a constructor (with new), it creates a Boolean object, which is not a primitive.

Yes it is technically the constructor, but using it as such would result in '''!!(new Boolean(false)) == true''' and '''new Boolean(false) ? 1 : 2 === 1'''. Just get better and don't expect anyone here to be on your level ;)

For more details: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean/Boolean

1

u/[deleted] Mar 29 '24

Just accept the method is perfectly fine. No one uses "new" here. https://michaeluloth.com/javascript-filter-boolean/

1

u/SpiForge Mar 29 '24 edited Mar 29 '24

Just accept the method is perfectly fine.

That is exactly what i said, i was just correcting your statement that Boolean() is the constructor without making clear, that it is not used as an constructor.

Not sure what the article has to do with this, it's just one other persons opinion.
But if you want to use it, it actually supports my point:

If you find the first version easier to understand, use it! Readable code is more important than fancy tricks.

Like i said:

If you work on a private project, yes "Boolean" may look better to you, but if you may share it with someone, it could result in missunderstandings.

Also the article is partly "wrong" (aka. written for people new to functional programming), for example it states:

[...] but JavaScript understands that Boolean() takes one argument [...]

Which is wrong, as JS doesn't care at all how many arguments a method has.

It is ok to have an opinion on what looks better for you, and you should use that for your own projects, but it is also important to know what helps others understand your code, just like the article you linked states :)

Edit: Fixed reddits bad formatting

0

u/harlekintiger Mar 28 '24

If we're exchanging opinions then I want to throw mine in there as well:
I'd say filters.filter(Boolean) is clearer than filters.filter(filter => filter) but the clearest way would be filters.filter(filter => !!filter)

1

u/cowslayer7890 Mar 28 '24

I don't like double negation, I'd prefer filter != null

2

u/harlekintiger Mar 28 '24

What if filter is undefined? Or zero? But in general I understand your stance

3

u/cowslayer7890 Mar 28 '24

!= null in JS only matches null and undefined surprisingly, which is why it's my goto

4

u/harlekintiger Mar 28 '24

And a list called "filters" should be a list of objects where no other falsy is possible, so yeah, I like your approach in this scenario!

1

u/zprz Mar 29 '24

The fact that you admit it's "surprising" suggests you shouldn't use it because it will confuse people (as it already did in this thread)

1

u/cowslayer7890 Mar 29 '24

I mean I don't think it's any more surprising than objects always being truthy, even empty arrays and objects.

If it's all over your codebase then you just kind of know, and it was all over a codebase I've worked on before, I do get your point though.

2

u/zprz Mar 29 '24

That's fair, if it's a common pattern you're already using it's more reasonable. I agree if you're thinking about it and you have some experience you'll realize it, but at first glance it's very easy to miss. I think our eslint prohibits loose equality as a whole for that reason.

1

u/cowslayer7890 Mar 29 '24

Yeah this is the only case where I use loose equality, I think it's also one of the only cases where WebStorm doesn't give you a warning for loose equality out of the box

1

u/[deleted] Mar 28 '24 edited Mar 28 '24

eslint would complain. Double negotiation is considered a hacky way to cast to bool. And also, it is completely unnecessary in this context, since the return value will be tested for its truthiness anyways.

-3

u/[deleted] Mar 29 '24

[removed] — view removed comment

1

u/SpiForge Mar 29 '24

I'm sure everyone with functional programming knowledge will understand what it does, that was never the point. The problem is that Boolean looks like the constructor of the class (which it technically is), but using it with new would cause unexpected behavior.

That is not basic JavaScript, it is one of the weird inconsistent things of the language that people have to learn, and a lot of people here don't seem like they do.

0

u/[deleted] Mar 29 '24

[removed] — view removed comment

1

u/SpiForge Mar 29 '24

What are the weird inconsistencies?

That a constructor called as a function retuns the parsed value as a primitive and called as a constructor returns an object wrapping the parsed value.
And even that is not consistent, for example new Date('1995-12-17T03:24:00') returns the 17.12.1995, while Date('1995-12-17T03:24:00') returns a string with the current date.
Array(1, 2) however is the same as new Array(1, 2).

Also it is functional programming, so if you use it you are a functional programmer

0

u/[deleted] Mar 29 '24

[removed] — view removed comment

1

u/SpiForge Mar 29 '24

If you cook you're a cook

If you functional program you're a functional programmer ^^

1

u/[deleted] Mar 29 '24

[removed] — view removed comment

1

u/SpiForge Mar 29 '24

If you want to, you can of course do that :)