0

I'm learning functional programming and trying to avoid using statements.

I have a long condition statement seen below:

if (!event.body || 
  !event.body.Id || 
  !event.body.source.accountId || 
  !event.body.sourceId || !event.body.targetId || 
  !event.body.targetName) 
{ return 0; }

I'm thinking of doing something like:

let invalid = [event.body, event.body.Id, event.body.source.accountId, event.body.sourceId, event.body.targetId, event.body.targetName]

if(invalid.includes("")){return 0;}

but I'm not sure about using "" and !, are they the same? I know ! means null or undefined, but not sure "" empty string will do here?

Another way I can think of is:

let invalid = [event.body, event.body.Id, event.body.source.accountId, event.body.sourceId, event.body.targetId, event.body.targetName]

if(invalid.every(x=>{if(x){return x}})){return 0;}

I'm not sure which method is better?

2
  • what would you like to check? is zero a valid value (for an id)? Commented Jul 12, 2018 at 13:48
  • some() is what you want Commented Jul 12, 2018 at 13:54

4 Answers 4

2

includes("") won't detect null, undefined, false, 0 and NaN, just"". Therefore using every is better here:

if(!invalid.every(x => x))

or the same with some:

if(invalid.some(x => !x))
Sign up to request clarification or add additional context in comments.

2 Comments

The way the array is named (invalid) it makes me believe it should be x => !x instead
@adelin close, but yes I had to improve a bit :)
2

Note that !a || !b is equivalent to !(a && b).

So, consider this:

const isValid = event => event.body && event.body.Id && event.body.source.accountId ....

if(!isValid(event)){
  return 0;
}

3 Comments

Off-topic here, but: Why use a function instead of a boolean for isValid?
you said you want a more functional approach... what if you'll need to verify the event validity in other places?
I'm not the OP but ok ;)
0

I'm thinking can I do like

let invalid = [event.body, event.body.Id, event.body.source.accountId, event.body.sourceId, event.body.targetId, event.body.targetName]

if(invalid.includes("")){return 0;}

This works, but only if the values are always strings. For instance, if event.body.Id is null or undefined (or another falsy value that isn't exactly ""), it will still go through.

Another way I can think is

let invalid = [event.body, event.body.Id, event.body.source.accountId, event.body.sourceId, event.body.targetId, event.body.targetName]

if(invalid.every(x=>{if(x){return x}})){return 0;}

This also works, but it uses an if statement, which isn't considered very functional.

My Suggestion:

Do

let invalid = [event.body, event.body.Id, event.body.source.accountId, event.body.sourceId, event.body.targetId, event.body.targetName]

return (invalid.every(x => x) ? 1 : 0)

Using invalid.every(x => x) avoids the if statement and will properly handle all falsy values. And the ternary makes it return either 1 or zero, without the use of an if.

Comments

0

You can use array.every like this:

let invalid = [event.body, event.body.Id, event.body.source.accountId, event.body.sourceId, event.body.targetId, event.body.targetName]

invalid.every(e => e)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.