- 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
Number 1 Mwhahaha
Admin
My eyeeeeeeeeeeees!
Admin
programmer = programmer.replace("right away")
Admin
Frist not allowed!
Admin
Computers are a great way to simplify tedious manual tasks. Of course, some developers don't fully understand this concept.
Admin
This fine programmer must be a TDD guru!
Admin
I get it! It's more flexible! Just in case they decide to allow or disallow another character, they simple add or remove another .replace()! brillant!
Admin
Wow... painful code...
Admin
You may want to do other validation for the phone number as well. For the US, for instance, you'll want to make sure it's ten digits. Also, some checking of the actual digits:
abb-cdd-xxxx
Neither A nor C can be equal to 1, and neither BB nor DD can be equal to 11.
You know, just for a start. I mean, if you're not doing anything useful with the field you don't need to bother, I guess.
Admin
This guy was paid by the line, right?
Admin
I wouldn't call it validation. There isn't really a check for validity happening. I could enter ABCD as my phone number and it'd go through as an empty string.
Sanitisation perhaps.
Admin
Admin
Admin
JavaScript .replace() replaces only first occurrence of the needle, so I guess this is TRWTF here.
Admin
Down! Bad characters! Naughty characters!
Heaven forbid somebody tries to specify an international dialing code, or appends an extension number. Maybe I'll specify my extension number after... backslashes?
Admin
Insane sanitisation ;-)
Admin
And it gets better! If you want to allow certain characters on the cellphone number, but not on the home or work numbers, you can just edit the part that handles cell phone. That would have been way more complicated if this code was placed in a function.
Admin
Don't think it is Javascript, looks like ruby looking at the comment.
Admin
I'm no javascript expert, but doesn't replace require a value to replace the regex with? So at best you have replaced the programmer with a new programmer (cause strings are immutable) that has no "right now", and thus no drive.
Congratulations, you have created a wally. Well done PHB
Admin
in pike i'd do this:
or
Admin
homephone? more like bananaphone.
Admin
It's a good thing businesses don't have phone numbers like 1-800-ACME-BUG. Then you might have a phone number in your database that looks like 1800.
Admin
Looks like python, this. (Though python does have a very nice regular expression module.)
Admin
This looks like another job-security thing to me. I'm sure a user will use one of the ŵëîřđ letters any day now, and then the developer will have another incident to keep busy.
Admin
Phew, I'm glad that my phone number, 555-a1a2, is allowed.
Admin
Insanitation then. :-p
Admin
What is particularly brilliant about this is that, rather than call a single function for the replaces, it is repeated three times in so homehone, workphone and cellphone can be customized individually.
Admin
The programmer was just future proofing the code. If he only checked if the character was a number trying to fix it if phone numbers started to allow non-numbers would require a team of at least 60 developers and 28 different enterprise integrating companies. The way it validates data means simply removing the validation, and like magic it will work.
Admin
crap, we've just added another phone.
Admin
Admin
The real WTF is he should have used String.Empty instead of "", right?
Admin
This is MASSIVE duplicates code, you think he'd just do: homephone = makeValid(homephone); workphone = makeValid(workphone); cellphone = makeValid(cellphone);
Yeesh! Everyone knows that methods are useful for saving time!
Admin
That can't possibly be actual "processional" code...can it?
Admin
The closest thing I can come up with...
Admin
Let's not forget the even more basic WTF of: if you simply must go through character by character to remove these things, why aren't they in any sort of order?
Admin
Reminds me of something similar I've seen in a java project: some 15 lines of phone number validation logic, including a for(;;) loop filled with weird string manipulation code featuring multiple breaks, string searches and a home built StringBuilder implementation. sigh
Turned out, the phone number wasn't actually used. Instead, an Oracle stored procedure silently threw that parameter away. One of those aha moments I could do without.
Admin
Even VB can do it better:
Admin
Similar method?
Admin
wow, and he cuts off +, the universal international phone number prefix. double fail!
Admin
Stand back everyone!!!
He's got a replace function, and he's not afraid to use it!!!
Admin
Of course he wasn't paid by the line.
homephone = homephone.replace(" ", "") homephone = homephone.replace("-", "") homephone = homephone.replace("(", "")
... is what you do when you're paid by the line.
ludus - luddite? I'm sure it fits...
Admin
Wow, another Pike programmer! A rare breed...
Here's another option: string clean_phonenumber = filter(phonenumber,(<'0','1','2','3','4','5','6','7','8','9'>)); Can't shortcut this one with mkmultiset as it requires the elements to be characters (aka int), but done this way, it's filtering the string instead of flitting it to array and back.
Admin
re works and, aside from the gotcha with .match vs. .search, the syntax is very nice. Main problem is, especially coming from Perl, they are sloooow.
Admin
In Perl it's
and I'm done.Admin
Even Haskell is briefer...
Admin
actually, you can shortcut that:
or even:
i didn't think about using filter on strings, thank you!
Admin
OMG You're right, I hadn't realised that the duplicate code could be extracted into a separate function - that's definitely the biggest WTF here. [/sarcasm]
Admin
The real WTF is that this code is wrong because letters are allowed in phone numbers...
replace("a", "") should actually be replace("a", "2")
Admin
Perl being the wonderful write only language that it is I'd have to rewrite your line noise with my own
$phone =~ s/\D//g;
Admin
Actually if you put AABBCCDD, you'd end up with ABCD in this, so letters are allowed, just x-1 of that letter ;)