Summer Sale! All accounts are 50% off this week.

eggplantSword's avatar

Best way to write if statements

I'm currently migrating an old project and in the code they do this statement

if (question.response_type === "boolean") {
        //action
} else if (
        question.response_type !== "list" &&
        question.response_type !== "multiple" &&
        question.response_type !== "image" &&
        question.response_type !== "text"
) {
       //action
}

Usually I would do it like this

if (question.response_type === "boolean") {
        //action
} else if (
        question.response_type === "date" ||
        question.response_type === "numeric"
) {
       //action
}

My question is does it matter which way I do it and is there a difference?

0 likes
18 replies
Sinnbeck's avatar

Are there only ever those 6 possibilities? If yes, then the new one should be fine

Sinnbeck's avatar

@msslgomez then it should be fine. Personally I would use probably use includes()

if (in_array(['date', 'numeric'].includes(question.response_type) {
//
} 
1 like
rodrigo.pedra's avatar

@Sinnbeck I did the same in my answer, started typing in_array then noticed the lack of $ in the variables and changed to JS's [].includes()

I guess it is the habit on coding more in PHP =)

Sinnbeck's avatar

@msslgomez I would also have this as a function, so I don't use else if. Just if x then return. Then you can return a default or throw an error at the end

if (question.response_type === "boolean") {
        return x;
}

if (in_array(['date', 'numeric'].includes(question.response_type) {
    return y;
}

return z;
2 likes
eggplantSword's avatar

@rodrigo.pedra Great catch! haha I didn't think adding the language would matter since if I needed I could look into converting the solution to js since it's not really a code question, more a logic question. Def will add just in case to my future questions even if I don't think it's necessary.

tykus's avatar

What are you asking here; the difference between logical AND and OR; or between !== and ===???

eggplantSword's avatar

@tykus I'm more asking if the if statement should be inclusive or exclusive. Should I target directly by checking if the value is equal or target by elimination of other options where the value is not equal. Basically if I should one way or the other or if it doesn't really matter and either works.

tykus's avatar

@msslgomez they're logical comparisons; it doesn't matter 🤷‍♂️

So long as they are readable and don't tie you up in knots - either is fine.

1 like
rodrigo.pedra's avatar
Level 56

In theory there should be a minuscule difference as in one snippet you are doing 4 comparisons and in the other you are doing two.

But that would very tiny, and unnoticeable. So I would stick with the one that communicates the code intention better.

When testing multiple options like this I usually go with in_array(...) in PHP, or .includes() in JS, like this:

if (question.response_type === "boolean") {
        //action
} else if ( ! ['list', 'multiple', 'image', 'text'].includes(question.response_type) ) {
       //action
}

or

if (question.response_type === "boolean") {
        //action
} else if (['date', 'numeric'].includes(question.response_type) ) {
       //action
}

This way if you need to remove or add a new type into check you just need to add/remove it from the array.

I personally prefer the second one as I find affirmative conditions easier to read in cases like this.

1 like
Snapey's avatar

if I read the original code, I would have no idea that those other two options existed. Perhaps a case for the new match statement, being explicit about the options and throwing an exception if no match

1 like
eggplantSword's avatar

@Snapey Great point, the other options there list, multiple, image and text don't need a schema, before the if statement I do set question.schema = [] so those other options get an empty array.

rodrigo.pedra's avatar

@Snapey No need to be sorry at all =)

We are all here eager to help, and it is the habit of coding more in PHP

Please or to participate in this conversation.