Home » none » Still think Ternary operators are evil?

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.

Like this Article? Subscribe to get every article sent to your email.

Related Post

  • Two Interfaces, One Method, Two Meanings (VB)Two Interfaces, One Method, Two Meanings (VB) Last week I showed how to implement two interfaces with one method and two meanings in CSharp.  One of the first comments I received for this post was, "How does this work in […]
  • Intellisense Everywhere in VB 9 (and a small bug)Intellisense Everywhere in VB 9 (and a small bug) One of the features the CSharp guys have had for quite a while is Intellisense everywhere.  Want to create a private variable?  Just start typing private and hit the tab key as soon as the […]
  • AS is in CSharp too.AS is in CSharp too. Many CSharp programmers are surprised to find that CSharp has an AS keyword that looks remarkably similar to VB.NET's AS keyword.  And while on the surface it looks similar to what AS does […]
  • Assign Multiple Enum values to one variable.Assign Multiple Enum values to one variable. I saw this question and immediately thought, "You can't!  An Enum is an Integer that has been restricted to the values it can accept."And I was basically right.  But, I forgot that […]
  • The VB.NET Ternary Operator The VB.NET Ternary Operator I think the VB.NET Ternary Operator may be the last operator that I really miss in VB.NET from my curly brace language experience.  Although, I have to admit, I wouldn't have missed it all […]

About Dave Bush

Dave Bush is a Full Stack ASP.NET developer. His commitment to quality through test driven development, vast knowledge of C#, HTML, CSS and JavaScript as well as his ability to mentor younger programmers and his passion for Agile/Scrum as defined by the Agile Manifesto and the Scrum Alliance will certainly be an asset to your organization.