Saturday, December 16, 2023

JS "ternaries" in Prettier(.js)

good, or confusing?

"case-style" ternaries make sense; nested are not readable, to me at least.

 A curious case of the ternaries · Prettier

a quick, contrived example to show the thinking behind "curious" ternaries:

    const animalName =
      pet.canBark() ?
        pet.isScary() ?
          'wolf'
        : 'dog'
      : pet.canMeow() ?
        'cat'
      : 'probably a bunny';
    


    And here's the code rewritten to demonstrate "case-style" ternaries:

    const animalName =
      pet.isScary() ? 'wolf'
      : pet.canBark() ? 'dog'
      : pet.canMeow() ? 'cat'
      : 'probably a bunny';
    

    No comments: