DEV Community

Cover image for βœ”||🀒 Commit or Vomit | checks.some()
 🐀πŸ₯‡ Jasper de Jager
🐀πŸ₯‡ Jasper de Jager

Posted on • Updated on

βœ”||🀒 Commit or Vomit | checks.some()

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);
Enter fullscreen mode Exit fullscreen mode

[].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

Oldest comments (17)

Collapse
 
annervisser profile image
Anner Visser • Edited

I think your last variable should be called isInvalid :)
(or isValid = !checks.some...)

Collapse
 
jmdejager profile image
🐀πŸ₯‡ Jasper de Jager • Edited

😘 thanks, fixed it!

Collapse
 
annervisser profile image
Anner Visser

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 it break;s after the first hit.

I think this is a good opportunity for some higher level functions:

const checks = [
    (data) => data.age > 18,
    (data) => data.username.length > 6,
    (data) => {
        if (data.isNewUser) {
            return data.email !== undefined;
        } else {
            return  data.deleted === false;
        }
    }
];

const isInvalid = checks.some(check => !check(data));
Enter fullscreen mode Exit fullscreen mode

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

Collapse
 
jmdejager profile image
🐀πŸ₯‡ Jasper de Jager

good example! 😍

Collapse
 
jackmellis profile image
Jack

this πŸ‘

Collapse
 
darkwiiplayer profile image
π’ŽWii πŸ³οΈβ€βš§οΈ • Edited

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:

const validate = validations => subject => validations.every(validation => validation(subject))

const confirmed = validate([ user => user.email_confirmed ])
const allowed = validate([ confirmed, user => user.age > 18  ])
Enter fullscreen mode Exit fullscreen mode
Collapse
 
malikkillian profile image
MalikKillian

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.

Collapse
 
bugb profile image
bugb • Edited

The problem is how do you know what the piece of data that is invalid age or username 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)

a=[].map(v=>v.v.v.v.v.v) // it is valid
Enter fullscreen mode Exit fullscreen mode

It can cause debuging becomes nightmare!

So I prefer if-else.

Collapse
 
nikhil27b profile image
Nikhil Bobade

That's good

Collapse
 
ingosteinke profile image
Ingo Steinke • Edited

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.

Collapse
 
jmdejager profile image
🐀πŸ₯‡ Jasper de Jager

The code does work, it isn't a real world example though πŸ˜‰

Collapse
 
devlorenzo profile image
DevLorenzo

Hey, where is the funny joke "we all know unicorns don't vomit"?

In all cases, keep it up!

Collapse
 
jmdejager profile image
🐀πŸ₯‡ Jasper de Jager

hehe, that's in the first one, I'll use it from now on 😁

Collapse
 
jmdejager profile image
🐀πŸ₯‡ Jasper de Jager

Even edited this one especially for you πŸ˜‹

Collapse
 
devlorenzo profile image
DevLorenzo

πŸ™ƒ

Collapse
 
jankapunkt profile image
Jan KΓΌster

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

Collapse
 
natalia_asteria profile image
Natalia Asteria

🀒

Would rather make them into multiple if statements that throws a custom hand-crafted error message. Much more debuggable.