DEV Community

Discussion on: Some cool JavaScript Shorthand that will make your code cleaner than your peer's

Collapse
 
haaxor1689 profile image
Maroš Beťko

I've never seen any problem at all with using nested ternary operators especially in React component code. Just set up your eslint right and it will consistently format it for you. With nested ternaries you have 2 cases:

  1. Tree-like nesting:
cond1
  ? cond2 // if
    ? opt1 // if
    : opt2
  : opt3
Enter fullscreen mode Exit fullscreen mode
  1. chained nesting:
cond1 // if
  ? opt1
  : cond2 // else if
  ? opt2
  : opt3 // else
Enter fullscreen mode Exit fullscreen mode

It's not that complicated.

Collapse
 
vitiok78 profile image
Victor Cozhuhari

It's even worse because it's tricky and uncommon.
"If" "else" are more straightforward.
But I consider using "else" as a bad practice because it complicates the logic in readability context.
Break this logic into the smaller functions or methods and use early return inside those functions instead of "else". And the code will be clean, readable and easily refactored. The person who will work with it later will thank you a lot.

Thread Thread
 
haaxor1689 profile image
Maroš Beťko

Saying tricky and uncommon is pretty subjective. From my subjective point of view in codebases of our projects at work it's nothing unusual to find ternary operators and no one is complaining.
As I've said it's especially useful in React code since you can't write if statements there. Let's say you want to conditionally show some data that is fetched asynchronously.

<div>
    {response.isLoading ? (
        <div>Loading...</div>
    ) : response.isError ? (
        <div>{response.error.message}</div>
    ) : (
        <div>{repsonse.data}</div>
    )}
</div>
Enter fullscreen mode Exit fullscreen mode

And this is pretty common example I would say. Other option would be to use && operators.

<div>
    {response.isLoading &&
        <div>Loading...</div>}
    {response.isError &&
        <div>{response.error.message}</div>}
    {!response.isLoading && !response.isError &&
        <div>{repsonse.data}</div>}
</div>
Enter fullscreen mode Exit fullscreen mode

And another option would be to make some helper methods or nested component so you can use the if/else statements there.

Personally, I prefer the first option. It's the most scale-able since if you want to add a condition you don't have to update the last one and it's the least overhead.

Thread Thread
 
vitiok78 profile image
Victor Cozhuhari • Edited

It is exactly one of the many reasons why I prefer Vue over React. That ugly JSX mess that mixes JavaScript logic and kinda HTML markup is awful. Separate template engine is always better. Other languages figured it out long time ago. Even in PHP they prefer to use Twig rather than mixed mess.

Vue:

<div>
    <div v-if="response.isLoading">Loading...</div>
    <div v-else-if="response.isError">{{ response.error.message }}</div>
    <div v-else>{{ repsonse.data }}</div>
</div>
Enter fullscreen mode Exit fullscreen mode

P.S. BTW else-if and else look natural and clean in template but they are bad inside JavaScript code at the same time

Thread Thread
 
jonrandy profile image
Jon Randy 🎖️

JSX is an abomination - makes me want to vomit every time I see it 🤮

Collapse
 
shubhracodes profile image
Shubhra Agarwal

Wow, That is a beautiful explanation.

Thanks for explaining it so easily