• gosse (unregistered)

    omg.

  • BK (unregistered)

    formattedComment.Append("f"); formattedComment.Append("i"); formattedComment.Append("r"); formattedComment.Append("s"); formattedComment.Append("t"); return formattedComment.ToString;

  • daniel (unregistered)

    Ah, the for-case. An easy mistake to make. It just seems so natural.

  • ross (unregistered)

    Djikstra would be happy. Two or more sequential statements, use a for-case.

  • dpm (cs)

    Yeah, the code sucks, but at least the length is verified before they try to access all ten digits they expect to be there.

  • Kermos (cs)
    for(int i = 0; i < 3;="" i++)="" {="" switch(i)="" {="" case="" 0:="" comment.append("w");="" break;="" case="" 1:="" comment.append("t");="" break;="" case="" 2:="" comment.append("f");="" break;="" }="" }="" return="" comment;="">
  • slugonamission (unregistered)

    [code] For i = 0 To 3 Select Case i Case 0 response.Append("W") Case 1 response.Append("T") Case 2 response.Append("F") Case 3 response.Append("?") End Select Next

  • dpm (cs)

    Damn, now you've got me wondering if I've ever done that. I've coded a lot of stupid things in twenty-five years, but I can't remember doing a for-case . . . which means I probably did and then savagely repressed the memory.

  • Médinoc (unregistered)

    The return of the for-case paradigm!

  • campkev (cs) in reply to dpm
    dpm:
    Yeah, the code sucks, but at least the length is verified before they try to access all ten digits they expect to be there.
    Using the same verification standard, I have verified that my ****** is 10 inches long.
  • Rene (unregistered)

    Nice clean and readable piece of code, cheers !

  • keeper (unregistered)

    python-style:

    output = ''
    for i in range(4):
      if i == 0:
        output.append("W")
      elif i == 1:
        output.append("T")
      elif i == 2:
        output.append("F")
      elif i == 3:
        output.append("?")
    print output
    
  • EPE (unregistered) in reply to campkev

    Bah! Mine goes up to eleven!

  • EPE (unregistered) in reply to campkev

    Sorry, let me try again

    campkev:
    dpm:
    Yeah, the code sucks, but at least the length is verified before they try to access all ten digits they expect to be there.
    Using the same verification standard, I have verified that my ****** is 10 inches long.

    BAH! Mine goes up to eleven!

  • JimmyVile (unregistered)

    What's wrong with a for-case? It's not needed here (LUT can be used, and would be much better here), but for more advanced decoding it's a perfectly valid way of running a (fixed length) state machine.

    I haven't have my coffee yet, but it doesn't look so far fetched.

  • Pim (cs) in reply to EPE

    I want to see what the StripNonNumericCharacters function looks like!

  • Otterdam (cs) in reply to JimmyVile
    JimmyVile:
    What's wrong with a for-case? It's not needed here (LUT can be used, and would be much better here), but for more advanced decoding it's a perfectly valid way of running a (fixed length) state machine.

    I haven't have my coffee yet, but it doesn't look so far fetched.

    Promise me you won't write any code until you've had your coffee.

  • some schmoe (unregistered) in reply to dpm
    dpm:
    Yeah, the code sucks, but at least the length is verified before they try to access all ten digits they expect to be there.
    Where does it do that? I only see a check for length = 0, otherwise it assumes all the characters are there. So if cleanPhoneNumber comes back with only 9 chars or less, it will blow up.
  • Arlen Cuss (unregistered)

    'i' before 'e', except after 'c' ...

  • MaW (unregistered)

    my Str $tired_comment = "";

    for 0 .. 2 { when 0 { $tired_comment ~= "W" } when 1 { $tired_comment ~= "T" } when 2 { $tired_comment ~= "F" } }

    say $tired_comment;

    =end

    Really, some people just need to be beaten daily with a good reference for their programming language of choice until they learn that it has more features than the ones they misuse every day.

  • campkev (cs) in reply to some schmoe
    some schmoe:
    dpm:
    Yeah, the code sucks, but at least the length is verified before they try to access all ten digits they expect to be there.
    Where does it do that? I only see a check for length = 0, otherwise it assumes all the characters are there. So if cleanPhoneNumber comes back with only 9 chars or less, it will blow up.

    Let me help you out

    dpm:
    <sarcasm>Yeah, the code sucks, but at least the length is verified before they try to access all ten digits they expect to be there.
  • Leonardo (unregistered) in reply to Otterdam
    Otterdam:
    JimmyVile:
    What's wrong with a for-case? It's not needed here (LUT can be used, and would be much better here), but for more advanced decoding it's a perfectly valid way of running a (fixed length) state machine.

    I haven't have my coffee yet, but it doesn't look so far fetched.

    Promise me you won't write any code until you've had your coffee.

    Yeah, i needed a coffe to see the WTF too. The downside is that I almost choke on it.

  • WayneCollins (cs)
    What's wrong with a for-case? It's not needed here (LUT can be used, and would be much better here), but for more advanced decoding it's a perfectly valid way of running a (fixed length) state machine

    No, a for-case is completely retarded. A while-case is a perfectly valid way of running a state machine. Something like:

    int state = 0;

    while(state < stateMax) { case 0: // This is probably not a real function call, but a few // lines directly inline, but you get the idea state = doSomethingAndGetNextState(); break; case 1: state = doSomethingAndGetNextState(); break; ... }

    If you muck with the state variable in your loop construct, you'll complicate the hell out of your state machine...

  • Kermos (cs)

    You know, the real WTF and really sad thing is that people who write stuff like that likely get paid way more than I do...

  • Kerio (unregistered)

    s/(\d{3})(\d{3})(\d{4})/(\1) \2-\3/

    amidoinitrite?

  • Otto (cs) in reply to Otterdam
    Otterdam:
    JimmyVile:
    What's wrong with a for-case? It's not needed here (LUT can be used, and would be much better here), but for more advanced decoding it's a perfectly valid way of running a (fixed length) state machine.

    I haven't have my coffee yet, but it doesn't look so far fetched.

    Promise me you won't write any code until you've had your coffee.

    I too have seen a for-case used for a large state machine. In particular, some of the cases modified the value currently in the for loop, to skip states or to go back states in some cases.

    Sadly, instead of making it sane, I added more states to it. Yes, I am ashamed, but sometimes you just gotta get the damn job done and get paid.

  • dpm (cs) in reply to campkev
    campkev:
    some schmoe:
    dpm:
    Yeah, the code sucks, but at least the length is verified before they try to access all ten digits they expect to be there.
    Where does it do that? I only see a check for length = 0, otherwise it assumes all the characters are there. So if cleanPhoneNumber comes back with only 9 chars or less, it will blow up.

    Let me help you out

    dpm:
    <sarcasm>Yeah, the code sucks, but at least the length is verified before they try to access all ten digits they expect to be there.
    I beg your pardon! My claim that "the code sucks" was not sarcastic at all.
  • daniel (unregistered)

    bashscript'd

    for i in seq 0 3; do case "$i" in "0") echo -n "W"; ;; "1") echo -n "T"; ;; "2") echo -n "F"; ;; "3") echo; ;; esac; done

  • Computerquip (unregistered) in reply to daniel
    daniel:
    Ah, the for-case. An easy mistake to make. It just seems so natural.

    .> Really? If I ever did this, I'd shoot myself....

  • Nallam (unregistered)

    OMG, that's a major WTF. No 'Else' case? that's baaaaad. Always put an 'Else' case. Some people never learn. tsk, tsk.

  • Brompot (unregistered) in reply to campkev
    campkev:
    dpm:
    Yeah, the code sucks, but at least the length is verified before they try to access all ten digits they expect to be there.
    Using the same verification standard, I have verified that my ****** is 10 inches long.
    My ruler is 12 inches. A much more common format.
  • Dave (unregistered)

    Yeah, in my first year as an asp.net coder, I'm pretty sure I did that. Though, I did add length validation so as to not add the parens on a number that was only 7 digits. Looking back, I regret using the for-case.

  • Addison (unregistered)

    At least he put it in a function.

  • Technomage (unregistered) in reply to JimmyVile

    It's wrong here because you could remove the for and case, and just have the statements execute in order. Using for and case statements to implement a state machine is probably fine (though I'd consider "while" instead of "for"). Using it to implement a linear ordering of statements is not fine; that's what the statement terminator (newline, semicolon, whatever) is for.

  • Trondster (unregistered)

    Naw - it's much too readable.

                For i = 0 To 14
                    Select Case i
                        Case 0
                            formattedPhoneNumber.Append("(")
                        Case 1,2,3
                            formattedPhoneNumber.Append(cleanPhoneNumber.Chars(i+1))
                        Case 4
                            formattedPhoneNumber.Append(") ")
                        Case Is < 7="" formattedphonenumber.append(cleanphonenumber.chars(i-2))="" case="" 8="" formattedphonenumber.append("-")="" case="" 9="" to="" 13="" formattedphonenumber.append(cleanphonenumber.chars(i-3))="" case="" else="" return="" formattedphonenumber.tostring="" end="" select="" next="">
  • Vollhorst (unregistered) in reply to Pim
                For i = 0 To 12
    
                    Select Case i
                        Case 0
                        Case 1
                            unformattedPhoneNumber.Append(uncleanPhoneNumber.Chars(1))
                        Case 2
                            unformattedPhoneNumber.Append(uncleanPhoneNumber.Chars(2))
                        Case 3
                            unformattedPhoneNumber.Append(uncleanPhoneNumber.Chars(3))
                        Case 4
                        Case 5
                            unformattedPhoneNumber.Append(uncleanPhoneNumber.Chars(5))
                        Case 6
                            unformattedPhoneNumber.Append(uncleanPhoneNumber.Chars(6))
                        Case 7
                            unformattedPhoneNumber.Append(uncleanPhoneNumber.Chars(7))
                        Case 8
                        Case 9
                            unformattedPhoneNumber.Append(uncleanPhoneNumber.Chars(9))
                        Case 10
                            unformattedPhoneNumber.Append(uncleanPhoneNumber.Chars(10))
                        Case 11
                            unformattedPhoneNumber.Append(uncleanPhoneNumber.Chars(11))
                        Case 12
                            unformattedPhoneNumber.Append(uncleanPhoneNumber.Chars(12))
    
                    End Select
    
                Next
    
    Obvious, isn't it?
  • monkeyPushButton (unregistered) in reply to Kermos
    Kermos:
    You know, the real WTF and really sad thing is that people who write stuff like that likely get paid way more than I do...
    Thanks, now I've gone from laughing to crying...
  • Vollhorst (unregistered) in reply to Pim
    Pim:
    I want to see what the StripNonNumericCharacters function looks like!
    Previous comment should have quoted this before the code segment. :/
  • Trondster (unregistered) in reply to Trondster

    Typo: Case 9 To 12 ..of course. ;)

  • Smash King (cs)

    If you look carefully, you'll notice that the code isn't even checking for the right range. A zero-to-twelve loop does 13 passes, not 12.

    At least it shouldn't take too long to fix this crap without side-effects. But if this guy managed to screw with something so simple, I can only imagine how awful this system's more complicated functions can be.

    And if anyone suppose that the right way to handle a phone validation is a state machine, go back to Programming Logic 101 right away.

  • dpm (cs) in reply to Trondster
    Trondster:
    Naw - it's much too readable.
                        Case Is < 7="">
    I really, really hope that whatever language that is *guarantees* evaluations are performed in order of appearance, or I'm really, really gonna laugh.
  • some schmoe (unregistered) in reply to campkev
    campkev:
    Let me help you out
    dpm:
    <sarcasm>Yeah, the code sucks, but at least the length is verified before they try to access all ten digits they expect to be there.
    Ah-ha!
  • dpm (cs) in reply to Smash King
    Smash King:
    And if anyone suppose that the right way to handle a phone validation is a state machine, go back to Programming Logic 101 right away.
    You'd let them keep "programmer" as a career choice? You are far more lenient than I.
  • Stephen (unregistered)

    ... ...

    .... uh.. wow..

    That was amazing stupidity.

    Almost as bad as an obfuscated 700 line function I saw once in Perl that gets the mean average of an array of numbers. I wish I had code from that project somewhere, that was absolutely amazing, and it took me hours to figure out what it did.

  • blindman (unregistered) in reply to campkev
    campkev:
    dpm:
    Yeah, the code sucks, but at least the length is verified before they try to access all ten digits they expect to be there.
    Using the same verification standard, I have verified that my ****** is 10 inches long.
    Yes, but is it clean?
  • Akoi Meexx (unregistered)

    My god, I remember when I thought VB was awesome. How stupid and naive I was in those days...

  • dpm (cs) in reply to Smash King
    Smash King:
    If you look carefully, you'll notice that the code isn't even checking for the right range. A zero-to-twelve loop does 13 passes, not 12.
    I see the "0 to 12" and I see "case 0" through "case 12" inclusive, all of which are correct (given the original WTF), so I have no idea what you're complaining about.
  • Tempura (unregistered) in reply to keeper

    Bah, ugly!

    print ''.join(['W', 'T', 'F', '!'][i] for i in xrange(4))

  • Buddy (unregistered)

    Horrendous.

    What is neat is that you can strip away, Picasso-style, and get at the essence of ApplyPhoneNumberFormatting.

    Public Shared Function ApplyPhoneNumberFormatting(ByVal phoneNumber As String) As String
            Dim cleanPhoneNumber As String
            Dim formattedPhoneNumber As New System.Text.StringBuilder
            'Dim i As Integer
    
                    'If phoneNumber Is Nothing Then
                            'Return String.Empty
                    'End If
    
            cleanPhoneNumber = StripNonNumericCharacters(phoneNumber)
    
            If cleanPhoneNumber.Length = 0 Then
                Return String.Empty
            Else
    
                'For i = 0 To 12
    
                    'Select Case i
                        'Case 0
                            formattedPhoneNumber.Append("(")
                        'Case 1
                            formattedPhoneNumber.Append(cleanPhoneNumber.Chars(0))
                        'Case 2
                            formattedPhoneNumber.Append(cleanPhoneNumber.Chars(1))
                        'Case 3
                            formattedPhoneNumber.Append(cleanPhoneNumber.Chars(2))
                        'Case 4
                            formattedPhoneNumber.Append(") ")
                        'Case 5
                            formattedPhoneNumber.Append(cleanPhoneNumber.Chars(3))
                        'Case 6
                            formattedPhoneNumber.Append(cleanPhoneNumber.Chars(4))
                        'Case 7
                            formattedPhoneNumber.Append(cleanPhoneNumber.Chars(5))
                        'Case 8
                            formattedPhoneNumber.Append("-")
                        'Case 9
                            formattedPhoneNumber.Append(cleanPhoneNumber.Chars(6))
                        'Case 10
                            formattedPhoneNumber.Append(cleanPhoneNumber.Chars(7))
                        'Case 11
                            formattedPhoneNumber.Append(cleanPhoneNumber.Chars(8))
                        'Case 12
                            formattedPhoneNumber.Append(cleanPhoneNumber.Chars(9))
    
                    'End Select
    
                'Next
    
                Return formattedPhoneNumber.ToString
    
            End If
    
        End Function
    

    Or with lines stripped out:

    Public Shared Function ApplyPhoneNumberFormatting(ByVal phoneNumber As String) As String
    
        Dim cleanPhoneNumber As String
        Dim formattedPhoneNumber As New System.Text.StringBuilder
    
        cleanPhoneNumber = StripNonNumericCharacters(phoneNumber)
    
        If cleanPhoneNumber.Length = 0 Then
            Return String.Empty
        Else
            formattedPhoneNumber.Append("(")
            formattedPhoneNumber.Append(cleanPhoneNumber.Chars(0))
            formattedPhoneNumber.Append(cleanPhoneNumber.Chars(1))
            formattedPhoneNumber.Append(cleanPhoneNumber.Chars(2))
            formattedPhoneNumber.Append(") ")
            formattedPhoneNumber.Append(cleanPhoneNumber.Chars(3))
            formattedPhoneNumber.Append(cleanPhoneNumber.Chars(4))
            formattedPhoneNumber.Append(cleanPhoneNumber.Chars(5))
            formattedPhoneNumber.Append("-")
            formattedPhoneNumber.Append(cleanPhoneNumber.Chars(6))
            formattedPhoneNumber.Append(cleanPhoneNumber.Chars(7))
            formattedPhoneNumber.Append(cleanPhoneNumber.Chars(8))
            formattedPhoneNumber.Append(cleanPhoneNumber.Chars(9))
            Return formattedPhoneNumber.ToString
        End If
    
    End Function
    

    Certainly braindead, and still worthy of WTF - unless input is guaranteed to be 0 or 9+ numeric characters.

  • campkev (cs) in reply to Buddy
    Buddy:
    Horrendous.

    What is neat is that you can strip away, Picasso-style, and get at the essence of ApplyPhoneNumberFormatting.

    Public Shared Function ApplyPhoneNumberFormatting(ByVal phoneNumber As String) As String
            Dim cleanPhoneNumber As String
            Dim formattedPhoneNumber As New System.Text.StringBuilder
            'Dim i As Integer
    
                    'If phoneNumber Is Nothing Then
                            'Return String.Empty
                    'End If
    
            cleanPhoneNumber = StripNonNumericCharacters(phoneNumber)
    
            If cleanPhoneNumber.Length = 0 Then
                Return String.Empty
            Else
    
                'For i = 0 To 12
    
                    'Select Case i
                        'Case 0
                            formattedPhoneNumber.Append("(")
                        'Case 1
                            formattedPhoneNumber.Append(cleanPhoneNumber.Chars(0))
                        'Case 2
                            formattedPhoneNumber.Append(cleanPhoneNumber.Chars(1))
                        'Case 3
                            formattedPhoneNumber.Append(cleanPhoneNumber.Chars(2))
                        'Case 4
                            formattedPhoneNumber.Append(") ")
                        'Case 5
                            formattedPhoneNumber.Append(cleanPhoneNumber.Chars(3))
                        'Case 6
                            formattedPhoneNumber.Append(cleanPhoneNumber.Chars(4))
                        'Case 7
                            formattedPhoneNumber.Append(cleanPhoneNumber.Chars(5))
                        'Case 8
                            formattedPhoneNumber.Append("-")
                        'Case 9
                            formattedPhoneNumber.Append(cleanPhoneNumber.Chars(6))
                        'Case 10
                            formattedPhoneNumber.Append(cleanPhoneNumber.Chars(7))
                        'Case 11
                            formattedPhoneNumber.Append(cleanPhoneNumber.Chars(8))
                        'Case 12
                            formattedPhoneNumber.Append(cleanPhoneNumber.Chars(9))
    
                    'End Select
    
                'Next
    
                Return formattedPhoneNumber.ToString
    
            End If
    
        End Function
    

    Or with lines stripped out:

    Public Shared Function ApplyPhoneNumberFormatting(ByVal phoneNumber As String) As String
    
        Dim cleanPhoneNumber As String
        Dim formattedPhoneNumber As New System.Text.StringBuilder
    
        cleanPhoneNumber = StripNonNumericCharacters(phoneNumber)
    
        If cleanPhoneNumber.Length = 0 Then
            Return String.Empty
        Else
            formattedPhoneNumber.Append("(")
            formattedPhoneNumber.Append(cleanPhoneNumber.Chars(0))
            formattedPhoneNumber.Append(cleanPhoneNumber.Chars(1))
            formattedPhoneNumber.Append(cleanPhoneNumber.Chars(2))
            formattedPhoneNumber.Append(") ")
            formattedPhoneNumber.Append(cleanPhoneNumber.Chars(3))
            formattedPhoneNumber.Append(cleanPhoneNumber.Chars(4))
            formattedPhoneNumber.Append(cleanPhoneNumber.Chars(5))
            formattedPhoneNumber.Append("-")
            formattedPhoneNumber.Append(cleanPhoneNumber.Chars(6))
            formattedPhoneNumber.Append(cleanPhoneNumber.Chars(7))
            formattedPhoneNumber.Append(cleanPhoneNumber.Chars(8))
            formattedPhoneNumber.Append(cleanPhoneNumber.Chars(9))
            Return formattedPhoneNumber.ToString
        End If
    
    End Function
    

    Certainly braindead, and still worthy of WTF - unless input is guaranteed to be 0 or 9+ numeric characters.

    Ironic that in a post about removing unnecessary lines, you have an if-return-else statement.

Leave a comment on “Formatting a Phone Number - The Long Version”

Log In or post as a guest

Replying to comment #:

« Return to Article