I've been looking on stack overflow and searching blog posts for info on this and can't seem to find a definitive answer.
It regards using if
condition or a ternary to execute functions. Is there a "best practice" regarding which we should use?
Let's say we have 2 functions and that one of them will execute based on some boolean value.
We can write functionality like this using traditional if
/else
statements:
if (boolean) {
doThis()
} else {
doThat()
}
Alternatively, we can write something functionally equivalent like this:
boolean ? doThis() : doThat()
Generally, most ternaries I use and see from others are used for assignment, and not to execute functions.
Everything on stack overflow and blogposts that I've seen just state that, "Yes, that's a thing. We can execute functions in ternaries".
Nothing ever speaks to whether we should favor one over the other. Is there a best practice when it comes to which one you should choose?
It seems like it'd be completely fine to use one if the functionality you'd like to execute conditionally is just one function. If you have more then that it seems like if
conditions should be favored for readability.
So, with all that context, here's the question:
Does it just depend on the situation? Or should we always favor if
conditions for function calls and relegate ternaries strictly to assignment?