I'm not sure whether this qualifies as code smell, or if there may be some better way of performing the same task, but, basically, I want to format a 10 digit string, using C#, and have the following options...
Option 1
public string FormatNumber(string value)
{
int i;
if(int.TryParse(value, out i)) return i.ToString("000 000 0000");
else throw new InvalidArgumentException("'" + value + "' is not a valid argument.");
}
Option 2
public string FormatNumber(string value)
{
int i;
if(int.TryParse(value, out i)) return value.Insert(6," ").Insert(3," ");
else throw new InvalidArgumentException("'" + value + "' is not a valid argument.");
}
Both achieve the correct result, but they both feel a little ... smelly.
Am I being too pedantic, or is their a clear winner?
TryParse
if you're just going to throw on the parse error anyway?else
to shorten (in SE) what it does in the real world.