It's Tuesday!
Time for a new ✔||🤢 Commit or Vomit! This time it is about using [].some(check ⇒ !check). What do you think of the following code?
const checks = [
data.age > 18,
data.username.length > 6
];
if(data.isNewUser) {
checks.push(data.email !== undefined)
} else {
checks.push(data.deleted === false)
}
const isInvalid = checks.some(check => !check);
[].some(true) ✔||🤢?
❤: Commit (I'd commit something like this)
🏷: Vomit (we all know unicorns don't vomit)
🦄: Like your post please continue this series!
Let's vote! 😊
Photo by Nik Shuliahin on Unsplash
Top comments (17)
I quite like the idea (and
.some
and.every
are lovely), but this implementations is somewhat misleading to read:All the checks are always performed at the time they're added, even though the power in
some
is that itbreak;
s after the first hit.I think this is a good opportunity for some higher level functions:
Overall I like the idea, but I think it will seldom be sufficient to deal with just a boolean as output. And if it is that simple, I'd prefer good ol' if/else.
So it's a 🤢 from me
Yea, this was my reaction as well; when a simple
if
isn't enough anymore, higher-order functions is the way to go.However, I'd add the data as its own argument to make things more convenient:
The problem is how do you know what the piece of data that is invalid
age
orusername
or something (you need to have additional code). And what if you want to throw custom error message for each case?Array in JS is also weird (if you dont know the spec of
map
or using Typescript)It can cause debuging becomes nightmare!
So I prefer
if-else
.I'd prefer a ternary over the if-else, but changing the tests to a list of functions is excellent. Adding, updating, removing tests becomes dead simple at this point and it's easier to test.
good example! 😍
this 👍
Thanks for the inspiring example. Although
.some
is a useful method, I would never commit or even write code like this, I even doubt it works (in the way I thought it was intended to).In general, I prefer to write more explicit and verbose code rather than oversimplified stuff that might be misleading when reading to maintain and modify.
Thanks to Anner Visser for pointing out what the code actually does: less magic than I thought was intended. To make the checks run in the last line, you would have had to use validator functions in the lines above (or computed properties, unless you stick to vanilla JS), which makes the code already less elegant, less compact, and more like what I have seen in React projects.
The code does work, it isn't a real world example though 😉
Hey, where is the funny joke "we all know unicorns don't vomit"?
In all cases, keep it up!
Even edited this one especially for you 😋
🙃
hehe, that's in the first one, I'll use it from now on 😁
I think your last variable should be called
isInvalid
:)(or
isValid = !checks.some...
)😘 thanks, fixed it!
Some is a great tool especially because it skips the for loop. I would only use it in a project with experienced devs, newcomers could be confused. Still commit
That's good
🤢
Would rather make them into multiple if statements that throws a custom hand-crafted error message. Much more debuggable.