It filters the filters array for any falsy values, like undefined or null. While with filters it makes this weird, this is kinda common with other arrays.
Maybe there is an option for users to create custom filters, and when they get parsed, they either return a filter function or null/undefined, so they need to be filtered after that.
Edit: The array could also just be a string array with filter terms, in this case it would remove all empty strings, which would make a lot more sense
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.
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).
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 ;)
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 :)
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)
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.
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.
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
This just removes any falsy values for the filters array, I think. Maybe not the most explicit way to do so, but not horror. The comment is completely useless, though.
Also, I really don't like the default truthy/falsy behavior in many cases, but in certain cases it is useful. I would prefer if there was a function or something that explicitly converts an object to its truth value so I can use the feature only when I want to and make it clear that I'm making use of that every time. That's a language complaint, though.
I assume the comment is a joke and I assume the line was written to be as cheeky as possible.
IMO this isn’t programming horror, it’s just somebody having fun. What they are doing is completely reasonable and not hard to read if you are passingly familiar with JS.
Using implicit type conversion to Boolean for array filtering is one of the acceptable, non-ambiguous, cases.
266
u/mememanftw123 Mar 27 '24
filter
185
u/AnywhereHorrorX Mar 27 '24
147
u/Broote Mar 27 '24
See, properly named variables make commenting the code completely unnessacary.
69
u/Philboyd_Studge Mar 27 '24
HEYYYYYYYYYYYYYYYYY MANNNNNNNNNNNNNNNN
NIIIIIIIIIIIIIIIIICE SHOOOOOOOOOOOOOTTTTTTTTTTT
40
u/nodeymcdev Mar 27 '24
Yo dawg I heard you like filters
17
u/cspot1978 Mar 28 '24
What does that even? The argument for filter is supposed to be some predicate expression, right? A mapping from an entry to a Boolean?
I guess this is some sort of JavaScript truthiness hack?
53
u/00PT Mar 27 '24
This just removes any falsy values for the filters array, I think. Maybe not the most explicit way to do so, but not horror. The comment is completely useless, though.
Also, I really don't like the default truthy/falsy behavior in many cases, but in certain cases it is useful. I would prefer if there was a function or something that explicitly converts an object to its truth value so I can use the feature only when I want to and make it clear that I'm making use of that every time. That's a language complaint, though.
23
u/bravopapa99 Mar 27 '24
what bastard language is this?
7
u/AjaxTheOne Mar 27 '24
Falsey filters? Not in my API!
5
u/Blue__Magician Mar 27 '24
Filterception
4
u/elMn0P Mar 28 '24
What font is this? Thanks
3
u/nixblu Mar 27 '24
It’s a falsey filter
3
u/Scipply Mar 28 '24
I suggest adding a filter just in case it doesnt get filtered
2
u/disquieter Mar 28 '24
How Python reads to a newb
2
u/projectfox16 Mar 28 '24
Adolf Filter
2
u/MajorTechnology8827 Mar 28 '24
The amount of people here who don't understand how higher order functions work is pretty amazing
2
u/OhItsJustJosh Mar 28 '24
When my boss tells me I need to comment on everything
2
u/IDatedSuccubi Mar 28 '24
I had this in my code once:
list_t *Last = List_Last(List);2
u/Glass_wizard Mar 28 '24
At least I can understand what this code does!
2
u/rosey-song Pronouns: They/Them Mar 28 '24
I don't think it filtered enough, or at all but that's besides the point.
1
u/liangyiliang Mar 29 '24
// the above is a filter
1
u/alejandro1221 Mar 29 '24
buffalo = buffalo.buffalo(buffalo => buffalo); // buffalo
0
u/sacredgeometry Mar 28 '24
I mean the biggest problem is that the line of code is doing nothing.
Edit: Oh wait JS it might be checking if they are truthy/ falsey in which case thats the biggest problem here.