Skip to main content
added 2 characters in body
Source Link
Paul
  • 201
  • 2
  • 8

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?

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?

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?

Source Link
Paul
  • 201
  • 2
  • 8

Formatting a string by converting to a number then back to a string

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?