DEV Community

Discussion on: Should I use ternary or the logical and operator?

Collapse
 
moopet profile image
Ben Sinclair

I'm a fan of making things as easy to read as possible.

In anything shy of a line in the shell I'm going to choose ternary over logical operators because it's easier for me to read, and my naive assumption is that it'll be the same for other people.

In the examples you've given, where you make the lines longer and more verbose, and you add further conditions to show how they get more difficult to read... I'd use an if block, and perhaps assign a variable with a clear name. You did this with hasError where it makes perfect sense to build a flag from a bunch of other flags. Not only does that make it easier to scan with your eyes, but it partially documents the code. You can immediately see that the component is shown if there's any error, because it's called hasError and that's defined above.