Hidden Gems: The Ternary Operator

If you didn’t already know, I’m majoring in Computer Science here at the University of Michigan and it’s been a long and unpredictable journey. I grew up with a passion for the humanities, especially art, and a large part of me wanted to be an artist. However, I was also talented in math and physics and enjoyed the problem solving required in STEM fields. Eventually I had to decide what I wanted to pursue in college, and I decided to be financially practical and pursue computer science and enjoy the humanities in my free time. Obviously this decision was completely subjective, but I want to give context as to how I approach art and computer science. Originally they were completely separate concepts, but the more I learn about computer science and programming specifically, the more I see it as a form of art. I’ve realized that I’m especially passionate about how code is written; there are no laws as to how code must be written, and style is completely subjective, which leads to each programmer having a unique style, similar to an artist. I’ve also heard comparisons between programmers and authors because both write hundreds of lines, broken up into chapters and paragraphs, which all aim to convey a certain idea. Viewed in this new light, I’ve discovered a lot of hidden gems that exist in computer science, small forms of art that go unnoticed, but are nonetheless works of creativity and intentional artistic design. I’d love to share some of those hidden gems with you, and I’d like to start with the simple ternary operator, a common programming construct found in most languages.

Programming is based on a few recurring ideas: checking certain conditions, using information, and providing interfaces that are easy to use. Checking a condition is incredibly straightforward, and I guarantee you do it all the time. For example, you might say that you’ll go for a walk if it isn’t raining outside, otherwise you’ll stay inside. In very loose code form (referred to as pseudocode), that conditional might be:

if (it isn't raining) {
go outside;
} else {
stay inside;
}

This is fairly easy to understand and it’s easy to see the pieces that correspond to the original sentence. However, it also takes up 5 lines of code. This might sound negligible, but when you’re working on a project that has thousands of files, each with over 100 lines, you quickly realize how much these small conditionals add up, and eventually how hard it is to read the code. In this way, programmers and writers differ: while a writer can spend paragraphs discussing an insignificant detail (think Charles Dickens), a programmer has to express a thought in as little words as possible, while still conveying the same meaning. The purpose is to make the code so easy to read and understand that a future programmer looking back at the code can read it like a book. I find it especially interesting how code is meant to be read by other people; I’ve heard the saying that the first programmer to write a file will only read it once, but that it will be read a hundred times by the programmers that come after them. Put in this perspective, it’s easy to see how important clean code is, and to understand the fine balance between art and efficiency when styling code. So, how can we make this 5 line conditional even more elegant? Answer: the ternary operator. Here is the same logic presented using the ternary operator:

it isn't raining ? go outside : stay inside

Breaking this single line into pieces, it says “is the first statement true? If it is then do this first thing. If it is not true, then do this other thing”, where the question mark signifies the question and the first thing and second thing are the pieces separated by the colon. Take the time to translate our example into this format and compare to the original piece of code. Now appreciate the simplicity and readability of the second form. I hope I am conveying just how fascinating this simple structure is, and how it is a small work of art in the world of programming. It is an art of elegance, conveying complex ideas in simple ways, but with a practical importance not found in most art. Hidden gems like this remind me why I enjoy programming so much, and how art can be found in anything, even when you aren’t looking for it.

jushutch

Junior studying Computer Science. Author of the Hidden Gems column, which explores art and art styles that are often overlooked or underappreciated.

Leave a Reply

1 Comment on "Hidden Gems: The Ternary Operator"


Member
20 days 3 hours ago

As someone on the same career path as you, this really resonates. I think programming is a whole art form in itself, and it makes me think in really different ways. Love your commentary on it!