Olivier Muhring

Olivier Muhring

  • 1.5k
  • 262
  • 9.5k

Can I simply my lengthy shorthand string comparison?

Dec 11 2018 7:32 AM
In one of my applications I receive a datastruct built like this:

> parent:
> {
>   x,
>   y,
>   z,
>   child[]:
>    {
>     x,
>     y
>    }
> }

I select the parent object, and then loop through all of the child objects. I use this data to create a 3rd object.

As you can see, some of the properties of the parent object can also be found in the child object. The parent object always has priority, so whenever I loop through the child objects I need to check whether or not I have a value @ the parent level for properties x & y. If I do, then I use the parent's value, if not it's the child's values that are being used.

Originally I created separate methods to these checks for me. I had one for int's, booleans and strings.

In the pull request I was asked to remove those methods and use the ? operator for my comparisons. I personally found my original way of working more readable, but OK.

Now, for the string comparisons I found that the expression to be used was somewhat lengthy:
  1. var B = parent.X != null ? (!string.IsNullOrEmpty(parent.X.XName) ? parent.X.XName  
  2.                                                                   : child.X != null ? (!string.IsNullOrEmpty(child.X.XName) ? child.X.XName  
  3.                                                                                                                             : string.Empty)  
  4.                                                                                     : string.Empty)  
  5.                      : (child.X != null ? !string.IsNullOrEmpty(child.X.XName) ? child.X.XName  
  6.                                                                                : string.Empty; 
I'm wondering if there are ways to simplify it?

Answers (1)