I think your question isn't really about if statement vs ternaries. I think your perhaps looking for different data structures that allow you to abstract over conditionals in a powerful DRY manner.
There's a few datatypes that can come in handy for abstracting over conditions. You could for example use an Any
, or All
monoid to abstract over related conditions. You could use an Either
, or Maybe
.
You could also look at functions like Ramda's cond
, when
and ifElse
. You've already looked at sum types. These are all powerful and useful strategies in specific contexts.
But in my experience these strategies really shine outside of views. In views we actually want to visualise hierarchies in order to understand how they'll be rendered. So ternaries are a great way to do that.
People can disagree what "functional" means. Some people say functional programming is about purity, or referential transparency; others might say its simply "programming with functions". Different communities have different interpretations.
Because FP means different things to different people I'm going to focus on one particular attribute, declarative code.
Declarative code defines an algorithm or a value in one place and does not alter or mutate in separate pieces imperatively. Declarative code states what something is, instead of imperatively assigning values to a name via different code paths. Your code is currently declarative which is good! Declarative code provides guarantees: e.g. "This function definitely returns because the return
statement is on the first line".
There's this mistaken notion that ternaries are nested, while if statements are flat. It's just a matter of formatting.
return (
condition1
? result1
: condition2
? result2
: condition3
? result3
: otherwise
)
Place the condition on its own line, then nest the response. You can repeat this as many times as you want. The final "else" is indented just like any other result but it has no condition. It scales to as many cases as you'd like. I've seen and written views with many flat ternaries just like this, and I find it easier to follow the code exactly because the paths are not separated.
You could argue if
statements are more readable, but I think again readable means different things to different people. So to unpack that, let's think about what we're emphasising.
When we use ternaries, we are emphasising there is only one possible way for something to be declared or returned. If a function only contains expressions, our code is more likely to read as a formula, as opposed to an implementation of a formula.
When we use if statements we are emphasising individual, separated steps to produce an output. If you'd rather think of your view as separate steps, then if statements make sense. If you'd prefer to see a view as a single entity with divergent representation based on context, then ternaries and declarative code would be better.
So in conclusion, your code is already functional. Readability and legibility is subjective, focus on what you want to emphasise. Do not feel like multiple conditions in an expression is a code smell, its just representative of the complexity of your UI, and the only way to solve that (if it needs to be solved) is to change the design of your UI. UI code is allowed to be complex, and there's no shame in having your code be honest and representative about all it's potential states.