Shorthand Conditional Statements
Published on October 15, 2010 | Filed Under Multiple LanguagesI’m a huge fan of making code shorter, where possible. One of the tricks that I use the most often is to make shorthand IF/ELSE statements. It seems as though a lot of beginner programmers are coming across these on the internet and can’t understand what’s going on. I can assure you that, once you see the syntax, you’ll easily understand it and even become a fan.
Syntax
The shorthand conditional statement is broken up into three parts: Condition, True Result, False Result and is written in the following manner:
Condition ? True Result : False Result
When you write your expression, you leave out the words IF and ELSE. For an example, let’s pretend like we have just asked a user how old they are. If they are under 18, maybe we want to send them to a new website.
User.Age >= 18 ? ProceedToSite() : SendAway();
The syntax should be fairly straight-foward. Luckily, we aren’t limited to just calling function names from these.
Returning Various Object Types
The beauty of this trick is that you are able to return any data type that you wish. It’s sort of like a mini function within your code. Let’s pretend like we have a JavaScript function that displays a message to our users, based on their age. If the user is under 18, we will let them know that they’re not welcome to our website, but if they’re 18 or older, we’ll welcome them with open arms!
<script type="text/javascript"> function UserAge(age) { var Message = age >= 18 ? "Welcome to our website!" : "I'm afraid you have to leave"; alert(Message); } </script>
So what happens when one of those darned kids tries to enter our website?
How about when an adult arrives?
You can also return integer values, if you so choose. I can’t think of a good use for an integer right now, so we’re going to just use a situation where we find out if our visitor is 16 and then we just add two years, which shows when they’ll be 18. (Work with me here and let’s pretend like we already have a lot of code, which doesn’t even exist!)
YearAllowedIn = User.Age == 16 ? GetCurrentYear() + 2 : GetCurrentYear();
Yeah, that was an odd example, but it shows that you don’t just have to return string values.
Conditional Statements with Extra Text
I’ve noticed that my statements often don’t work properly when I’m doing the work inline with other text. If you find yourself doing work like this, just put your conditional statement in parenthesis and it will fix it for you. For example, we can pretend like we’re using the same UserAge function from above, but building the message that we want to return.
<script type="text/javascript"> function UserAge(age) { var Message = "You are " + (age >= 18 ? "" : "definitely not ") + "granted access to our site!"; alert(Message); } </script>
As an added bonus, I have also shown that you can return blank values, if you so choose!
So what happens when those pesky underage kids return?
How about when our adult visitors return?
Life without Shorthand Conditional Statements
I can’t imagine a life without shorthand conditional statements. We basically did our code in just two lines. We could have made it one line if we hadn’t declared a variable. But what would that same statement from above look like, if we spelled everything out in the normal manner?
<script type="text/javascript"> function UserAge(age) { var Message = ''; if(age >= 18) Message = "Welcome to our website!"; else Message = "I'm afraid you have to leave"; alert(Message); } </script>
That same script turns from two lines into six lines! I hope you’re starting to see what the benefit is, here.
Language Compatibility
The languages that I have personally used this syntax for are C#, C++, JavaScript, Java and PHP. The only language which I’m certain does not use this is Visual Basic. I’m sure there are more programs out there that use this syntax, so I welcome you to leave some comments below, describing compatibility. I’ll be more than happy to update this post to reflect your findings!
~Derek Torrence