Why I'm phasing out ternary statements
Tags: javascript, opinion, career, beginners
Published: 2020-02-28 16:28:52

---
title: Why I'm phasing out ternary statements
published: true
description: Why I've stopped using ternary statements
tags: javascript,opinion,career,beginners
---

I'm in a little bit of a bind. Very early on in my software development career, I learned about these things called ternary statements, and I thought they were the coolest thing ever. I started using them everywhere...but now, I'm just having different thoughts. Let's take a step back for a minute though.

# What are ternary statements?

Ternary statements are (put simply) shortcut if statements that look like this:
```javascript
const val = "test";
const showTest = val == "test" ? "The value was a test value" : "The value was not a test value";
``` 

(Go to this MDN article to learn more about ternary operators: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator)

Spoken in plain english that code is translated to:
IF val is equal to the value of "test"
THEN set the variable showTest to the value of "The value was a test value"
ELSE set the variable showTest to the value of "The value was not a test value"

# What's the alternative??

To me, early on in my career, this seemed like an absolute godsend. The alternative code looks like this:
```javascript
const val = "test";
let showTest;
if (val == "test") {
    showTest = "The value was a test value";
} else {
    showTest = "The value was not a test value";
}
```

And if you hated more lines of code like I did early on, this seems like a no brainer. A two line solution verses a seven line solution is really no question.

# So what's the problem?

Remember how in the beginning I referenced that I was in a bind...well here's the bind.

In the application that I have built (and now work on maintaining for the company that I work for) I used ternary statements EVERYWHERE. I used it for absolutely everything that I could. Less lines of code, right? Well I've realized that the only thing that cares about lines of code in your app is this website right here:

https://linesofcode.app

And besides that, going after less lines of code isn't always the best. In fact, I run into two problems now going back to the application and trying to refactor the ternary statements a little bit.


- The mental overhead it takes to make a change in the logic. 

Ternary operators only support one action for each block of the if. So in the code above, maybe all I wanted to do in the beginning was set the showTest variable to a string...but what if now I also want to fire off a function if val does equal test? I have to go back, remember what it was doing, try and remember why I thought there would be no other actions necessary, and then refactor it to look like the second example I've posted up anyways.

- The distinct lack of code readability

Code like this that does so much in so few lines of code really loses it's luster when it comes to readability. It *may* scan better, but it also *does* provide a mental block when it comes to readability that the other example does not.

# So what?

All in all, I will probably still use ternary operators...even after everything I said. Why? Because there is definitely a time and a place for them, just like with everything. What we have learned here where I work though, is that time and place isn't littered through the codebase, just trying to cut a 127 line file down to a 122 line file.

What are your thoughts on ternary operators?