• Number 1 Mwhahaha (unregistered)

    Number 1 Mwhahaha

  • Doom (unregistered)

    My eyeeeeeeeeeeees!

  • BentFranklin (unregistered)

    programmer = programmer.replace("right away")

  • Dr. Albax (unregistered)

    Frist not allowed!

  • Anonymous (unregistered)

    Computers are a great way to simplify tedious manual tasks. Of course, some developers don't fully understand this concept.

  • Karl karlington (unregistered)

    This fine programmer must be a TDD guru!

  • BLah Blah (unregistered)

    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!

  • daqq (unregistered)

    Wow... painful code...

  • Your Name (unregistered)

    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.

  • Rob (unregistered)

    This guy was paid by the line, right?

  • Dan (unregistered)

    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.

  • Icould od much better (unregistered) in reply to Rob
    Rob:
    This guy was paid by the line, right?
    No. He wouldn't do 3 replaces per line if it was so.
  • Rob (unregistered) in reply to Icould od much better
    Icould od much better:
    Rob:
    This guy was paid by the line, right?
    No. He wouldn't do 3 replaces per line if it was so.
    Oh, so he was just an idiot. Got it. :)
  • false (unregistered)

    JavaScript .replace() replaces only first occurrence of the needle, so I guess this is TRWTF here.

  • Studley (unregistered)
    #make sure that the phone numbers have no bad characters in them

    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?

  • Meta-Sam (unregistered) in reply to Dan
    Dan:
    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.

    Insane sanitisation ;-)

  • (cs) in reply to BLah Blah
    BLah Blah:
    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!

    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.

  • Groogy (unregistered) in reply to false

    Don't think it is Javascript, looks like ruby looking at the comment.

  • Toc the elder (unregistered) in reply to BentFranklin

    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

  • eMBee (unregistered)

    in pike i'd do this:

    string phonenumber = "(123)456-789/0";
    string clean_phonenumber = `-(phonenumber, @Array.uniq(sort( `-(phonenumber, @"1234567890"/"")/"" )));

    or

    multiset allowed = mkmultiset("1234567890"/"");
    Array.filter(phonenumber/"", lambda(string digit){ return allowed[digit]; }) * "";
  • (cs)

    homephone? more like bananaphone.

  • XXXXX (unregistered)

    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.

  • Bheuk (unregistered)

    Looks like python, this. (Though python does have a very nice regular expression module.)

  • (cs)

    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.

  • (cs)

    Phew, I'm glad that my phone number, 555-a1a2, is allowed.

  • Dan (unregistered) in reply to Meta-Sam
    Meta-Sam:
    Dan:
    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.

    Insane sanitisation ;-)

    Insanitation then. :-p

  • Kevin (unregistered)

    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.

  • (cs)

    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.

  • spaceman (unregistered)

    crap, we've just added another phone.

  • jdw (unregistered) in reply to Dragnslcr
    Dragnslcr:
    Phew, I'm glad that my phone number, 555-a1a2, is allowed.
    You must have missed that he strung three replaces together for each line, thus creating much more compact and efficient code!
  • Bryan the K (unregistered)

    The real WTF is he should have used String.Empty instead of "", right?

  • Silverwizard (unregistered)

    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!

  • Will Noob (unregistered)

    That can't possibly be actual "processional" code...can it?

  • SeySayux (unregistered)

    The closest thing I can come up with...

    for(String& s : {homephone,cellphone,workphone})
       s = filter(s,[](uchar c){return c in_range(48,57);});
    
  • A Gould (unregistered)

    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?

  • Sergej (unregistered)

    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.

  • (cs)

    Even VB can do it better:

        Private Function GetPhoneNumber(ByVal homephone As String) As String
            Dim temp As String = String.Empty
            Dim tempVal As Integer
            Dim i As Integer
            For i = 0 To homephone.Length - 1
                tempVal = Val(homephone(i))
                If ((Not tempVal = 0) And (Not (homephone(i) = "0"(0)))) Then
                    temp += CUInt(tempVal).ToString
                End If
            Next
            GetPhoneNumber = tempVal
        End Function
    
    
    
  • (cs)
    #make sure that the phone numbers have no bad characters in them 
    How do we make sure that dev teams have no bad characters (like the one who wrote this code) in them?

    Similar method?

  • onitake (unregistered)

    wow, and he cuts off +, the universal international phone number prefix. double fail!

  • (cs)

    Stand back everyone!!!

    He's got a replace function, and he's not afraid to use it!!!

  • Anonymous 'oward (unregistered) in reply to Rob

    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...

  • Rosuav (unregistered) in reply to eMBee
    eMBee:
    in pike i'd do this:
    string phonenumber = "(123)456-789/0";
    string clean_phonenumber = `-(phonenumber, @Array.uniq(sort( `-(phonenumber, @"1234567890"/"")/"" )));
    or
    multiset allowed = mkmultiset("1234567890"/"");
    Array.filter(phonenumber/"", lambda(string digit){ return allowed[digit]; }) * "";

    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.

  • Sobriquet (unregistered) in reply to Bheuk
    Bheuk:
    Looks like python, this. (Though python does have a very nice regular expression module.)

    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.

  • Sobriquet (unregistered) in reply to eMBee
    eMBee:
    in pike i'd do this:
    string phonenumber = "(123)456-789/0";
    string clean_phonenumber = `-(phonenumber, @Array.uniq(sort( `-(phonenumber, @"1234567890"/"")/"" )));
    or
    multiset allowed = mkmultiset("1234567890"/"");
    Array.filter(phonenumber/"", lambda(string digit){ return allowed[digit]; }) * "";

    In Perl it's

    tr/0-9//cd
    and I'm done.

  • Sobriquet (unregistered) in reply to Rosuav
    Rosuav:
    eMBee:
    in pike i'd do this:
    string phonenumber = "(123)456-789/0";
    string clean_phonenumber = `-(phonenumber, @Array.uniq(sort( `-(phonenumber, @"1234567890"/"")/"" )));
    or
    multiset allowed = mkmultiset("1234567890"/"");
    Array.filter(phonenumber/"", lambda(string digit){ return allowed[digit]; }) * "";

    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.

    Even Haskell is briefer...

    cleanPhone = filter (`notElem` ['0'..'9'])
    
  • eMBee (unregistered) in reply to Rosuav
    Rosuav:
    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.

    actually, you can shortcut that:

    filter(phonenumber,mkmultiset((array)"1234567890"));

    or even:

    filter(phonenumber,(multiset)(array)"1234567890");

    i didn't think about using filter on strings, thank you!

  • Skilldrick (unregistered) in reply to Silverwizard
    Silverwizard:
    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!

    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]

  • NUXI (unregistered)

    The real WTF is that this code is wrong because letters are allowed in phone numbers...

    replace("a", "") should actually be replace("a", "2")

  • Sectoid Dev (unregistered) in reply to Sobriquet
    In Perl it's

    tr/0-9//cd

    and I'm done.

    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;

  • kjordan (unregistered)

    Actually if you put AABBCCDD, you'd end up with ABCD in this, so letters are allowed, just x-1 of that letter ;)

Leave a comment on “No Letters Allowed!”

Log In or post as a guest

Replying to comment #:

« Return to Article