- Feature Articles
- CodeSOD
- Error'd
-
Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
It's not someone who's afraid of logarithms, but more afraid of mhtiragols, better known as powers (we want Math.Pow(...) rather than Math.Log(...)).
But yeah, on a scale of badness of code, this scores highly.
Admin
' *** the generates a frist post!
Admin
12345.ToString("D99") generates 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000012345
which I assume (didn't count it) is 99 digits long
Admin
I came to make the same point - glad to see I wasn't getting confused!
Admin
I wonder why they cap the "number of decimals" to 99 whereas
x
will be hard-coded to1
as soon asnumDecimals > 9
(due to the conversion toint
throwing anOverflowException
).Also, the actual function... Why do they even tie the case to parity?
Admin
And... the actual function's comment gives 'aa7bb' as an example, but can't actually return that? (only 'aa7BB')
Admin
It can't return either, but can return aA7Bb
Admin
Nvm, I was mistaken
Admin
Ah, so the real WTF is they excluded z, Z and 9?
Admin
Why tie the case to parity? Generating a random character code that could be in either the capital letter range or the lowercase letter range, but not in between, is hard. It's easier to use a random number to flip the case 50% of the time. But why generate a new random number, when a perfectly good random number has already been generated? Of course they reuse
i
. To do anything else would be wasteful!Admin
No, it's easy. In C/C++ style pseudocode
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"[random(0 to 51 inclusive)]
Admin
Am I missing something, or is the first if block totally unnecessary? It sets
n
andx
, but these variables are never used. It fills instrNum
, but then it's immediately cleared.Could it be left over from some previous iteration of the code that used these variables?
Admin
It also sets the 1st, 2nd, 4th, 5th, 8th and 9th characters to letters (and the rest as numbers) - even though the string can't have an 8th or 9th character. Which rather suggests that it used to generate 10-character strings at one point.
Admin
Moment of horror as I saw ***. Someone found my code. But the naming on the next line calmed me down, as I have never used that standard.
I have used *** as TODO/Fix/GetBackTo for as long as I remember, but do not know where that came from. Was it a standard in some language?
Admin
A management edict for "minimize lines of code touched to close a ticket" probably has a lot to do with the evolution of this function. And most of the rest of their doubtless benighted codebase.
When refactoring is outlawed, only outlaws will refactor.
Admin
n is used to set x, and x is appended to the return value if numDecimals is greater than 0.
Admin
I came to the comments to point out that System.Random's next(min, max) method is exclusive on max. That is, if you want 2 digits, it will only return up to and including 98.
But then I saw the code for the ID generator and quickly realised that was the least of their problems. That's the sort of code that is only fixed by selecting all and deleting.
Admin
I once worked on a system that generated "ticket IDs" that looked like a third of a UUID. The initial code generated all the random bits and then queried the database to see if it existed and if it did, generated a new one until it had a unique one. This was getting slow.
At that time, my approach to unique IDs was to let the database generate the next sequence number, but that wouldn't look like the "ticket ID". And I didn't really know about UUIDs back then. So I modified the generation algorithm to generate the first part and get all IDs with that prefix, then it could do the generate-the-next-one-check-it-doesn"t-exist in memory. Way way faster, and it kept the same format. which were the only two things I cared about at that time.
Admin
I mean, I can see why you'd return your 5-digit random number as a string if you were going to support leading 0 digits, but....