|
I found this little gem in an app I worked on recently.
switch (strValue.GetLength())
{
case 1:
{
strValue = "00000" + strValue;
break;
}
case 2:
{
strValue = "0000" + strValue;
break;
}
case 3:
{
strValue = "000" + strValue;
break;
}
case 4:
{
strValue = "00" + strValue;
break;
}
case 5:
{
strValue = "0" + strValue;
break;
}
case 6:
{
break;
}
}
And yes, the code I replaced with comments is the same for all 6 cases.
|
|
|
|
|
Hey, those zeroes are valuable, you can't just go throwing them around willy-nilly!!
Sure, it could have been done with a single append then take the right 6 chars, but how many 0's would have been needlessly lost? Consigned to the bit-bucket of history, before their time... Show some mercy, man!!
--------------------------------------------------------
Knowledge is knowing that the tomato is a fruit.
Wisdom is not putting it in fruit salad!!
|
|
|
|
|
But... they're insignificant.
|
|
|
|
|
Nice. Did you fire up the blame engine (source control) and find the culprit?
Steve
|
|
|
|
|
It does not work that well with outsource contractors! (I have seen similar code from those).
|
|
|
|
|
That was, in fact, my first reaction. I found the culprit, and he still works here.
|
|
|
|
|
Ick - I've seen way too much of this type of thing in code over the last 3 decades. I've come to calling this style of programming "brute force" programming.
So, whoever wrote this loses lots of points for elegance (or the lack thereof), but they do get some points in my book for style (use of braces, indentation, etc). No default case however...
|
|
|
|
|
This is indeed not good, the switch statement isn't flexible at all.
One should master the power off recursion
CString Pad(CString strValue, int iPadSize)
{
if(strValue.GetLength() >= iPadSize)
{
return strValue;
}
else
{
strValue = Pad(CString("0") + strValue, iPadSize);
};
return strValue;
}
Learn from the mistakes of others, you may not live long enough to make them all yourself.
|
|
|
|
|
He didn't simply append one zero in each case block and then fall through?
Did he ensure the strValue was trimmed?
I'm curious what you replaced it with.
Of course, in .net, providing the proper format string to int.ToString() will do it in one swell foop.
|
|
|
|
|
I believe it would be int.ToString(some_string, "000000"); . Of course, I haven't programmed for a while, so I might be wrong.
So the creationist says: Everything must have a designer. God designed everything.
I say: Why is God the only exception? Why not make the "designs" (like man) exceptions and make God a creation of man?
|
|
|
|
|
Well, kinda, yeah.
Assuming the ID begins as an int...
int ID = 123 ;
...
string strID = ID.ToString ( "000000" ) ;
|
|
|
|
|
Hope they don't work with you anymore.
"The clue train passed his station without stopping." - John Simmons / outlaw programmer
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
|
|
|
|
|
If the 'do something with strValue' is the same in all six cases, it obviously makes sense to pull that out of the case. As for the broader question of whether to append discretely-counted zeros, it's obviously ugly but I believe the code will only generate one new string when it's called. Most other approaches I'm familiar with would generate two new strings.
|
|
|
|
|
The System.String class has a nice little pair of PadLeft()/PadRight() functions. All it needs is the character to use for padding, and the final length the string needs to be, so that whole switch could be replaced with
const int FIXED_LENGTH = 6;
str = str->PadLeft('0', FIXED_LENGTH);
Dybs
|
|
|
|