MetaChat is an informal place for MeFites to touch base and post, discuss and
chatter about topics that may not belong on MetaFilter. Questions? Check the FAQ. Please note: This is important.
Well, the winning iPod health warning was really good (referencing the iPod advertising silhouette graphics, right?). The code snippet looks monstrous, but I'm too stupid to get the whole implication.
Reasons why it's a bad piece of code:
1) The function is repeated wherever it's needed instead of only being in one place.
2) It doesn't work properly ("12345678P~?" will still validate as a valid SSN)
3) Code for testing each section is repeated. (It should be in a loop)
Even without the *evil* regexp, you should be able to shrink this down to 10 or so lines of code)
in VDF...(My language)
function isValidSSN string sSSN returns boolean
boolean isValid
string sChar
move (true) to isValid
if (length(sSSN)) ne 8 move (false) to isValid
for iIndex from 1 to 8
move (mid(sSSN,1,iIndex)) to sChar
if ((sChar"9")) move (false) to isValid
loop
function_return isValid
end_function
I don't really know Java and regexes are still fairly new to me but I guess this would be fairly close to what the coder intended: ssn.matches("[0-9]{9}");
Still, as they say in that thread, this still wouldn't be the correct way to validate SSNs but it does go to show just how bloated the code is. Nightmarish.
"matches" hah. You young 'ens have got it easy.
I remember when you only had four variables, and you could only compare strings one byte at a time. We 'ad to program uphill both ways back then.