1

What is the correct/best way to handle the string as optional parameter in ternary operator:

public static void method(string s1, string s2="")
{
    var str = !string.IsNullOrEmpty(s2) ? s2.Equals("abc") ? "abcuser" : "xyzuser" : "xyzuser";
    Console.WriteLine(str);     
}

OR

public static void method(string s1, string s2="")
{
    var str = !string.IsNullOrEmpty(s2) && s2.Equals("abc") ? "abcuser" : "xyzuser";
    Console.WriteLine(str);     
}
2
  • To my mind, the second one is much better especially with braces var str = (!string.IsNullOrEmpty(s2) && s2.Equals("abc")) ? "abcuser" : "xyzuser" for readability. In this case you don't add additional level of condition branching that makes expression harder to understand. Commented Feb 12, 2022 at 4:23
  • There's a lot of noise in those tests, both of OP's as well as Kitta's. s2 == "abc" is sufficient to know if it should be "abcuser".. The == operator for strings will handle either side (or both) being null without having to test separately, and the case of s2 being empty is not worth testing because if it's empty, it can't equal "abc" anyway. Commented Feb 12, 2022 at 4:32

1 Answer 1

2

When creating a ternary operator, it's important that you keep them as clean and simple as possible so it's easier for other developers to read. In this instance, I can't see a use for the "s1" parameter. If this is your entire method, I recommend removing it and keeping it simple. Your "s2" parameter must equal "abc" to be accepted. Hence, a null check is not required. However, if you ever need to use a check like this, I recommend using IsNullOrWhiteSpace() because it indicates whether a specified string is null, empty, or consists only of white-spaced characters. This means it will check if the string contains empty space as well. E.g. if "s2" = " ", IsNullOrEmpty() wouldn't pick that up. I will leave a code example below, with a commented out line using this string method to check for white space. However, in this instance, I suggest you only use the smaller line.


public static void method(string s1, string s2 = "") {
     //var str = !string.IsNullOrWhiteSpace(s2) && s2.Equals("abc") ? "abcuser"
       : "xyzuser";
       var str = s2 == "abc" ? "abcuser" : "xyzuser";
       Console.WriteLine(str);
}

Too make it cleaner and more optimized, if you are not using that var variable again, you could just do this.

public static void method(string s1, string s2 = "") {
            Console.WriteLine(s2 == "abc" ? "abcuser" : "xyzuser");
        }

Or another alternative would be to return this value as a string

public static string method(string s2 = "") {
            return s2 == "abc" ? "abcuser" : "xyzuser";
        }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.