|
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
|
|
|
|
|
I once worked on an application where one of the developers fulfilled the requirement of commenting his code by putting at the top of each method:
Well, thank you Captain Obvious!
:josh:
My WPF Blog[ ^]
All of life is just a big rambling blog post.
|
|
|
|
|
hahaha that sounds great, what comment system do you use, i have used doxygen with some sucess but it becomes a little tiresome after a while
"Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius and a lot of courage to move in the opposite direction." -Albert Einstein
|
|
|
|
|
Josh Smith wrote: // Enforce Method Contract
Possibly a placeholder for later elaboration (never done, of course! )
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
I worked with a chap that did a retro fit of function headers on files where they'd largely been left out. The functions themselves were meaningfully named and internally well commented. Unfortunately he decided that he didn't have time to write descriptive text for all of them, so just put TODO: Add function comment. This was a real pain, all of our TODO: Fix nasty bug and TODO: Finish writing this important piece of code before release got buried beneath approximately a thousand worthless TODOs.
|
|
|
|
|
I have seen code where the standard practice was to leave in the // TODO: Add constructor logic here that the compiler puts in automatically. The folk that did it never saw a problem with doing that because they didn't use the Task list. I found it to be a problem as soon as I put in a useful TODO and it was overwhelmed my hundreds of pointless TODOs.
Bill W
|
|
|
|
|
That's good.
"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
|
|
|
|
|
That sounds reasonable to me. I'm pleased to see that he thinks the contract is so important. Or is this some form of bizarre Arnold Schwarzenegger motivational speaking type of thing. Try saying it in an Austrian accent, and it really works.
|
|
|
|
|
Pete O'Hanlon wrote: Try saying it in an Austrian accent, and it really works
:josh:
My WPF Blog[ ^]
All of life is just a big rambling blog post.
|
|
|
|
|
You've got a point there. One can imagine Arnie busting out the one-liner "contract enforced" after dispatching some poor sap.
Steve
|
|
|
|
|
Stephen Hewitt wrote: One can imagine Arnie busting out the one-liner "contract enforced" after dispatching some poor sap.
That's how he runs California.
Faith is a fine invention
For gentlemen who see;
But microscopes are prudent
In an emergency!
-Emily Dickinson
|
|
|
|
|
Hi,
I once saw in a real piece of code the following fragment! Nice name, don't you think?
{
boolean e0x0301=false;
.
.
.
if (!e0x0301) { ... }
}
modified 7-Mar-17 16:39pm.
|
|
|
|
|
Bill, searching frenetically in the pile of paper on his desk while hitting F5.
Bill: "Oh god, I keep losing this post-it on which I note the address of the array cell that does not get filled properly"
James, sitting next to him, smiling :" Why don't you rename a variable after it ?..."
Bill:" Oh yes, good idea, I suppose I'll remove it later in the production code."
|
|
|
|
|
Nice
modified 7-Mar-17 16:38pm.
|
|
|
|