Still think Ternary operators are evil?

OK, I’ve seen a lot of the VB guys complaining that using the ternary operator (which VB.NET just got as of 3.5) makes the code unclear.  To which I’ve said said, and continue to say, “Yes of course, if used incorrectly.”

But, this post, over at ASP.NET without ASP.NET shows a pratical use of the ternary operator and shows that it can be a lot more clear than multiple if/else if statements.

Now, we all know that a case statement would be preferred to multiple if/else if statements WHEN THAT IS POSSIBLE.  But, sometimes your if/else if is either not comparing against the same value, or is comparing multiple values.  In both cases, a switch statement will not work, or be as clear as this example of the ternary operator.

Most Commented Post

Leave a Reply

Comment Policy:

  • You must verify your comment by responding to the automated email that is sent to your email address. Unverified comments will never show.Leave a good comment that adds to the conversation and I'll leave your link in.
  • Leave me pure spam and I'll delete it.
  • Leave a general comment and I'll remove the link but keep the comment.

Notify me of followup comments via e-mail

Bear