DEV Community

Cover image for ✔||🤢 Commit or Vomit | checks.some()

✔||🤢 Commit or Vomit | checks.some()

🐤🥇 Jasper de Jager on April 20, 2021

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 followi...
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
 
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
 
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
 
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
 
jmdejager profile image
🐤🥇 Jasper de Jager

good example! 😍

Collapse
 
jackmellis profile image
Jack

this 👍

Collapse
 
ingosteinke profile image
Ingo Steinke, web developer • 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
 
lorenzoblog profile image
Lorenzo

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

Even edited this one especially for you 😋

Collapse
 
lorenzoblog profile image
Lorenzo

🙃

Collapse
 
jmdejager profile image
🐤🥇 Jasper de Jager

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

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
 
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
 
nikhil27b profile image
Nikhil Bobade

That's good

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.