 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
TRWTF is it's a roundabout way of just writing
aka the typical tunnel vision noob coder syndrome
Admin
TRWTF is adding digits of a positive number and somehow getting 0.
Admin
@Anon You're the frist guy to see that additional WTF. Well, there is a solution: they use rather complex numbers, not necessarily positive ints only. What about number 738i5342i?
Admin
When I see people call toString() so many times instead of making a local string and calling it one time, its clear they have no idea how anything actually works.
Admin
"Easy Reader Version: I didn't learn that credit card numbers could be checksummed until college, and spent most of my teen years wondering why "just any random number" wasn't a common exploit. I was not a bright child."
Most people aren't aware of the Luhn formula, or any checksum at all, while clutching their Social Security numbers. All they know is that the computer said the number isn't right and try again.
Admin
That's Remy's WTF. Common checksum algorithms do not "sum to 0", but "sum then modulo to 0". That's a rookie mistake there.
But a more blatant WTF is that they're trying to implement a borked version of Luhn algorithm. You multiply every other digit by 2 in Luhn algorithm before adding the digits of each result together because that prevents singledigit errors and adjacent digit swapping errors. Doing it as in the WTF does not protect against the latter (and would've been better just summing the digits, which'd give whatever monkey coding this a easier time).
Admin
@Anon is trolling us, right? Never heard of "casting out nines", which dates to the second century?
Admin
No, summing the digits to get 0 is still a reasonable checksum  it gives a nice large pool of valid numbers  0, 00, 000000, 000000000000000000000, etc.
Admin
That's not what is going on. They're adding positive numbers and expecting a sum that is divisible by 10.
Admin
That's not true, since it will only work if all digits are between 0 and 4 (2n < 10).
The checksum digits map to (2*n % 9)
So you would get int result = certificateNumber.ToString().Sum(c => ((c  '0') * 2) % 9);
Admin
Just rolling with the wrong implementation of the Luhn rule, let's say you did have a cert number '1235' which when you double every digit adds up to 22, % 10 = 2... boy if only there was some way you could figure out what number would add up to 30 without trying them all, huh? But this guy obviously hadn't advanced to subtraction yet.
Admin
Given that this guy uses string to simulate a collection of integers, (which isn't even needed as far as I can tell) he should use strings in style  get him I'm touch with the makers of the StringManager.
Admin
The real WTF is that the place either has no code reviews, or this code passed a code review.
Admin
Are they hiring English majors to do programming? So many of these WTF's, including this one, are about someone who misuses strings because they do not understand integer arithmetic, and also can't figure out how to write "if(x >= 10)".
Converting an integer to a string to isolate the digits is sort of clever, but slow. (I would use %10 to get the lowest digit, then /10 to advance to the next digit, but that requires math beyond the 4th grade.) Borking the Luhn rule is just another little bit added to a steaming pile.
Admin
I could not find a better way than incrementing and rechecking the sum, but here is the simplified .NET code that produces the same values:
private static int CleanupV2(int value) { while (!IsValidV2(value)) { value ++; }
}
private static bool IsValidV2(int value) { int sum = 0; int remainder = value;
}
When testing locally for the first 1,000,000 numbers, they produce the same values ... v1 took 12.9s where v2 took 1.0s. Surprisingly, not that great of a speedup.
Admin
Admin
Nah, I can vouch for it. Casting out nines is a thing.
Admin
See, for example, https://www.google.com/search?q=%22casting+out+nines%22 I haven't seen or heard much of it lately, but back in ancient history when arithmetic was performed by humans it was a convenient way to checksum calculations.