TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

A Curious Case of the Ternaries

3 pointsby Amorymeltzerover 1 year ago

1 comment

mooglyover 1 year ago
I don&#x27;t know what to post.<p>1) arguing about syntax is awful and this thread will be worse for it<p>2) opinionated tools like Prettier are meant to prevent such discussions<p>3) this post is such discussion bait<p>4) I super hate that formatting<p>I would never nest ternaries that many levels deep, but this always worked for me and I consider it a solved problem:<p><pre><code> const animalName = pet.canSqueak() ? &#x27;mouse&#x27; : pet.canBark() ? pet.isScary() ? &#x27;wolf&#x27; : &#x27;dog&#x27; : pet.canMeow() ? &#x27;cat&#x27; : pet.canSqueak() ? &#x27;mouse&#x27; : &#x27;probably a bunny&#x27;; </code></pre> I reproduce it verbatim, but want to point out the last `pet.canSqueak()` branch is not needed. Maybe that was done on purpose?<p>Almost no tools can do it and it drives me nuts.<p>Everything breaks down when the predicates end up multi-line though, and I don&#x27;t see this particular style improving it.