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?