## Friday, June 11, 2021

### Exploring the Ternary Operator

I’ve never had a lot of use for the ternary operator. Until recently, I think I under appreciated it. I Don’t recall ever using it in professional development though that may be largely related to using languages that didn’t support it. I never taught it to beginners either. Between time available and topics to be covered it never rose to a high enough priority for me. I’m starting to rethink that. But first, what am I talking about?

In C-style languages the syntax is:

`(expression-1) ? expression-2 : expression-3`

There is a Boolean expression inside the parenthesis, followed by a ? and a value for a true case, a : and a value for a false case. For example:

`(Player == WHITE) ? "White" : "Black";`

Other languages have different formats. For example, in Python the true option comes before the Boolean expression.

`[on_true] if [expression] else [on_false]`
```>>> x, y = 5, 6
>>> print("x" if x> y else "y")```

We actually see this sort of thing in spreadsheets though I never really thought of it as a type of ternary operator. This example from Excel is really a ternary operation

`=IF(G10 <  G11  ,"big","small")`

Back in the day we had something not all that different in FORTRAN. There was (probably still is) an IF statement in FORTRAN that branches to one of three lines depending on if an arithmetic value is less than, equal to, or greater than zero.

`IF ( N ) 10, 20, 30 `

But enough history. What really changed my mind about this operator? As is typical for me I revalued it when I found a good use for it. I have been writing a version of the Reversi program in .NET and C#. I borrowed a bunch of Java code from my friend Tom Indelicato (read about that program here) Steal from the best is my motto and Tom is an outstanding programmer. Along with the syntactic changes moving from Java to C# I wrote a lot of code to make the game work in a graphical user interface. I made some different design decisions as well which changed how some things are handled. I created a user control class for the game squares for starters. As part of this I used three different integer values to indicate whether the square held a black disk,a white disk, or was empty.

In the past I have often used a Boolean value to indicate which of two players was the current player. Switching players is pretty simple with a Boolean value.

`currentPlayer = !curentPlayer;`

That wasn’t going to work if my player indicators were integers. I decided to use the ternary operator because I didn’t want to write multiple lines of code for something that simple. So I wrote:

`Player = (Player == WHITE) ? BLACK : WHITE;`

WHITE and BLACK are defined constants. The next thing I knew, I was finding uses for this operator in all sorts of places. The really cool thing (ok for a geek like me) was that I could use this statement inside other statements.

`message = “No legal moves for " + ((Player == BLACK) ? "Black" : "White");`

I guess this old dog can learn new (to him) tricks. Do you use/teach this operator? Are there other language features you have ignored until one day you realized they would be just right for something you were doing?