The Ternary Cometh

Developers are likely to be familiar with ternary conditional operators as they're legal in many languages (Ruby, Python, C++, etc).  They're also often used in coding interviews to test an applicant as they can be a familiar source of code errors.  While some developers couldn't care less about ternary operators, there's been a cult following waiting for them to show up in Powershell.  That day is almost upon us.
Any Powershell developer can easily be forgiven for scratching their heads and wondering what a ternary is.  In the most basic sense a ternary evaluates an expression to a binary result and carries out one of two possible outcomes.  Lets start by looking at some code examples:

puts (if 1 then 2 else 3 end)


PS > 1 ? 2 : 3
PS > 2

The above is the same conditional expressed first in Ruby, then in Powershell, and both examples have a return of 2.  First off, let's get around the obvious confusion in the Powershell example.  The alias for Where-Object is '?', and that is not what the '?' represents in the powershell ternary operation.  The likely reason for implementing '?' instead of another character is for inter-language operability and reducing the level of effort for migrating code from other languages to Powershell.
The best way to explain how to read the ternary example above is to express it in a more familiar context.  That is to say, let's turn it into an If/Then/Else statement:


Right off we can see one of the benefits of a ternary is that it makes code more succinct.  It should also make more sense why we refer to the ternary as a conditional operator.  It's intended as an optional replacement for If/Than/Else in much the same way that Select/Case is for organizing multiple conditional statements.  Wondering if there's a performance enhancement?

Nope.  So the ternary is intended to boost readability but does that happen in reality?  Some engineers, who shall remain nameless, misuse its purpose into statements you'll wish you could unsee or will make your eyes bleed.  For example:

Bool c1, c2,c3;
// Assign some values to c1, c2 and c3.
int x = c1?c2?1:2:c3?3:4;

Who wants to code review that?  Or decipher it while trying to resolve a problem that's affecting your critical operations?
There's a definite place for ternary operators in Powershell.  They have the potential to enhance the programing experience while simplifying readability.  Some will look upon the ternary as a way to show off their elite skills and create code only they can read.  The great engineers will leverage the best syntax for the correct reason at just the right moment in their code.  If you want to experiment with Powershell ternary operators early, grab the build linked above (or any later build) and run 'Enable-ExperimentalFeature PSTernaryOperator'.

Please, enjoy ternary operators responsibly.
Remembering Dorothy Vaughan on this anniversary of JFK's "We choose to go to the Moon" speech.

Comments are closed.