In your opinion what is more readable: ?? (operator) or use of if's

99封情书 提交于 2019-12-01 20:29:21

Personally I'd go for the corrected version of your bosses - possibly with even more checks on it - if the string's empty, yours will, as you say throw an exception, as "" isn't a well formatted number and ?? only checks for null.

Something like:

public int doSomeWork(string value) {
  int someValue = 0;

  if (!string.IsNullOrEmpty(value)) {
    Int.TryParse(value, out someValue);
  }
}

Which resolves the issue where value equals "Forty Two".

Why not just use TryParse()?

public int doSomeWork(string stringValue)
{
    int value;
    int.TryParse(stringValue, out value);

    return value;
}

The above code will return 0 if the value is anything but an actual number.

So in my opinion, my example is the most readable. I try to parse the int and return it. No coalesce operator, and no string methods are used. This method also handles the exceptions that might be thrown when parsing (unless you WANT the exceptions...).

I think your best option is to do what your boss says, this one isn't worth it! That said, add a few spaces around yours and I like it better.

int someValue = int.Parse(value ?? "0");

I definitely prefer the null coalesce operator (??) over a series of if statements. Especially when you need to coalesce more than one value, the operator approach is FAR more readable. This plays into other newer features of C#, such as lambda expressions, LINQ sugar syntax, etc. The less code there is to muddy up the actual intentful code, the clearer the intent should/will be.

Why parse the string "0" just to get integer value 0? I definitely prefer this:

public int doSomeWork(string value) {
   int someValue;
   if (String.IsNullOrEmpty(value)) {
      someValue = 0;
   } else {
      someValue = Int32.Parse(value);
   }
}

My refactoring would look like this

public int doSomeWork(string value)
{
   int result = 0; //default?

   if(string.IsNullOrEmpty(value))
   {
      result = 0;
   }
   else
   {
      result = int.Parse(value); //you could also consider using TryParse(...) if your string could possibly also be different from a number.
   }

   //do some calculations upon "result"


   return result;
}

I'm currently reading Martin Fowlers book on Refactoring (wanted to read it already for a longer time now) and this is what I normally prefer and I found it is also a commonly suggested "pattern" in the book.

the two options are not equivalent. A part from a bug in the second snippet (it should read if(string.IsNullOrEmpty(value)), it will handle two cases, null and empty strings, whereas the ?? operator only handles nulls.

A part from that it's way more readable. I side with your boss.

Another solution is

int someValue = string.IsNullOrEmpty(value) ? 0 : int.Parse(value);

Your first snippet will only check if value == null, but the seconds snippet checks if value == string.Empty || value == null. I don't know what the requirements of your method is but those two snippets will do different things.

Actually you could refactor to

var value = 0;
int.TryParse(yourString, out value);

either way you always have a valid integer (if that is the goal)

You can do it your way? Cool!

If is definitely more readable, unless everyone is more of a C# wonk than me.

In this case the earlier is more readable as its a trivial example. **However in your case they are not equivalent, as the ?? isn't the same as string.IsNullOrEmpty **

The latter would be better in cases where the if was complex. I'd say horses for courses. Just depends on the audience. Try and keep it simple.

public int doSomeWork(string value)
{
  return int.Parse(value ?? "0");
}



public int doSomeWork(string value)
{
   if(value == null)
      value = "0";
    int SomeValue = int.Parse(value);
    return SomeValue;
}

[Assuming you only need to check for null, and not empty string also, as others have pointed out]

The semantic difference between the two is that ?? is an expression, while if is a statement. An expression says "perform a calculation and return a result", exactly the semantics you seek. More work has to be done to allow an if statement to express the same semantics; beyond that, the if leaves room for more logic than the calculation, room you don't need.

You should use the ?? operator because it expresses exactly the desired intent.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!