问题
I have a method that will receive a string
, but before I can work with it, I have to convert it to int
. Sometimes it can be null
and I have to change its value to "0"
. Today I have:
public void doSomeWork(string value)
{
int SomeValue = int.Parse(value ?? "0"); //it can throw an exception(i know)
}
I did it, but my boss asked me to refactor it to:
public void doSomeWork(string value)
{
if(string.IsNullOrEmpty(value))
value = "0";
int SomeValue = int.Parse(value);
}
in your opinion what is the best option?
回答1:
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".
回答2:
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...).
回答3:
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");
回答4:
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.
回答5:
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);
}
}
回答6:
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.
回答7:
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.
回答8:
Another solution is
int someValue = string.IsNullOrEmpty(value) ? 0 : int.Parse(value);
回答9:
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.
回答10:
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)
回答11:
You can do it your way? Cool!
If is definitely more readable, unless everyone is more of a C# wonk than me.
回答12:
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;
}
回答13:
[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.
来源:https://stackoverflow.com/questions/1324331/in-your-opinion-what-is-more-readable-operator-or-use-of-ifs