Thursday, August 21, 2014

Using C# Ternary (?) operator statement as a method parameter


The '?' operator is one of the oldest conditional operators that has been coming along with object oriented languages such as C++, Java and C#. The most significant advantage of using it is it will reduce a couple of lines of code than using the traditional if-else or switch statements, where there is a need to return values.

I first came across the functionality and power of using this just a couple of years ago, while assigned with a task to re-factor some existing code of a senior. More often I have seen the use of this being used with assigning the results to variables. And subsequently my coding behaviour also followed suit. 


Recently I was wondering if the usage could only be limited to assigning variables, and tried adding the statement directly to a method passing it as a variable. It just works fine, since as long as the returning type matches the variable type of the method parameter, such an assignment succeeds. Though this maybe already a straight forward understanding for C# or Java experts, yet this explanation could serve for those in doubt, and to understand the beauty of the language. Ok, with out any further exaggeration, let's have a look at what I am talking about.

Lets assume you have a method to display a name by passing in a name parameter like the following:


Code:
void DisplayName(string name)
{
//
}

Now simply let's assume Two names, 'Ahmed' and 'Jack', and we are to display the name which has less than 5 characters and pass that name to the above method to be further processed. Using the ternary operator the approach would be:


Code:
string name1 = "Ahmed";
string name2 = "Jack";
var nameLessThanFour = name1.Length < 5 ? name1: name2;
DisplayName(nameLessThanFour);

Eliminating the intermediate variable assignment, this could be achieved also as the following:


Code:
DisplayName(name1.Length < 5 ? name1: name2);


Note : This approach would work only for methods that pass arguments by value and not for those that pass arguments by reference (i.e the arguments with ref and out keywords will not work).

Personally I would not recommend the over usage of this approach on method parameters as it could negate the very purpose of simplicity of reading (i.e You might end up with a longer line in the method parameters mixed up with '?'s, ':'s and ","s thereby making it complex to read).

I find the following scenarios where you can advocate this approach:
  1. Methods that have parameters which have a need to pass arguments which require results from a condition.
  2. Using this approach on no more than two or three arguments of a method to reduce reading complexity.
  3. Using this approach on methods that could be called on parts of other lengthy conditional statements such as switch or if-else. This way we could save a lot of unnecessary variable assingments prior to method calls.
That's it. I would like to hear opinions of others on this. Happy Coding ahead...!

1 comment:

Tauqeer Maqsood said...

Find your desire property in Pakistan within your budget on Bakhxi real estate. We deal all Phases of DHA Lahore plot, State Life housing society plot, Banker Town, bahria town, agriculture Land, Commercial Plot|Plaza, House sale Purchase and rent. Bakhxi Real Estate provides your dream Property where your investment never gone wrong.
http://bakhxirealestate.com