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