Andrew Holzer
1 min readAug 10, 2017

--

I want to raise issue with your response here. Usually, when making an

if (!variable) { return null; }

call, you are verifying that you actually have some data before operating on it. That console.log(variable) is going to be more than one line long in most cases, a series of operations working on that data you just made sure you had.

In the languages that I use ternary operators in, your solution (with the console.log replaced with whatever operations you’re looking to perform on your data) is just invalid code. Even if it was valid, I think it is rather ugly, and just harkens back to those nasty if…else… blocks where the actual functionality of the code is stuffed inside one logical branch and the error escape in the other.

I’d argue that this if statement is concise enough and easy to follow, so it doesn’t need to be rewritten as a ternary. It’s one line and simply states “If there is no data, then return”. You can even ditch those brackets surrounding the return, in the languages that I am aware of. Moving past that statement means you have something and can set about validating it or transforming it, and you can’t simply express that with a ternary due to it requiring the false condition be handled.

--

--