Comment On Formatting a Phone Number - The Long Version

Dear Daily WTF Readers, [expand full text]
« PrevPage 1 | Page 2 | Page 3 | Page 4 | Page 5Next »

Re: Formatting a Phone Number - The Long Version

2009-03-09 09:04 • by gosse (unregistered)
omg.

Re: Formatting a Phone Number - The Long Version

2009-03-09 09:04 • by BK (unregistered)
formattedComment.Append("f");
formattedComment.Append("i");
formattedComment.Append("r");
formattedComment.Append("s");
formattedComment.Append("t");
return formattedComment.ToString;

Re: Formatting a Phone Number - The Long Version

2009-03-09 09:09 • by daniel (unregistered)
Ah, the for-case. An easy mistake to make. It just seems so natural.

Re: Formatting a Phone Number - The Long Version

2009-03-09 09:09 • by ross (unregistered)
Djikstra would be happy. Two or more sequential statements, use a for-case.

Re: Formatting a Phone Number - The Long Version

2009-03-09 09:11 • by 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.

Re: Formatting a Phone Number - The Long Version

2009-03-09 09:11 • by Kermos

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;

Re: Formatting a Phone Number - The Long Version

2009-03-09 09:12 • by 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

Re: Formatting a Phone Number - The Long Version

2009-03-09 09:13 • by dpm
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.

Re: Formatting a Phone Number - The Long Version

2009-03-09 09:14 • by Médinoc (unregistered)
The return of the for-case paradigm!

Re: Formatting a Phone Number - The Long Version

2009-03-09 09:14 • by campkev
248111 in reply to 248106
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.

Re: Formatting a Phone Number - The Long Version

2009-03-09 09:17 • by Rene (unregistered)
Nice clean and readable piece of code, cheers !

Re: Formatting a Phone Number - The Long Version

2009-03-09 09:18 • by 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

Re: Formatting a Phone Number - The Long Version

2009-03-09 09:20 • by EPE (unregistered)
248114 in reply to 248111
Bah! Mine goes up to eleven!

Re: Formatting a Phone Number - The Long Version

2009-03-09 09:21 • by EPE (unregistered)
248115 in reply to 248111
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!

Re: Formatting a Phone Number - The Long Version

2009-03-09 09:21 • by 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.

Re: Formatting a Phone Number - The Long Version

2009-03-09 09:22 • by Pim
248117 in reply to 248114
I want to see what the StripNonNumericCharacters function looks like!

Re: Formatting a Phone Number - The Long Version

2009-03-09 09:25 • by Otterdam
248118 in reply to 248116
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.

Re: Formatting a Phone Number - The Long Version

2009-03-09 09:29 • by some schmoe (unregistered)
248119 in reply to 248106
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.

Re: Formatting a Phone Number - The Long Version

2009-03-09 09:31 • by Arlen Cuss (unregistered)
'i' before 'e', except after 'c' ...

Re: Formatting a Phone Number - The Long Version

2009-03-09 09:31 • by 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.

Re: Formatting a Phone Number - The Long Version

2009-03-09 09:32 • by campkev
248122 in reply to 248119
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.</sarcasm>

Re: Formatting a Phone Number - The Long Version

2009-03-09 09:33 • by Leonardo (unregistered)
248123 in reply to 248118
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.

Re: Formatting a Phone Number - The Long Version

2009-03-09 09:33 • by WayneCollins
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...

Re: Formatting a Phone Number - The Long Version

2009-03-09 09:34 • by 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...

Re: Formatting a Phone Number - The Long Version

2009-03-09 09:34 • by Kerio (unregistered)
s/(\d{3})(\d{3})(\d{4})/(\1) \2-\3/

amidoinitrite?

Re: Formatting a Phone Number - The Long Version

2009-03-09 09:36 • by Otto
248127 in reply to 248118
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.

Re: Formatting a Phone Number - The Long Version

2009-03-09 09:36 • by dpm
248128 in reply to 248122
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.</sarcasm>
I beg your pardon! My claim that "the code sucks" was not sarcastic at all.

Re: Formatting a Phone Number - The Long Version

2009-03-09 09:36 • by 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

Re: Formatting a Phone Number - The Long Version

2009-03-09 09:36 • by Computerquip (unregistered)
248130 in reply to 248104
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....

Re: Formatting a Phone Number - The Long Version

2009-03-09 09:38 • by 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.

Re: Formatting a Phone Number - The Long Version

2009-03-09 09:40 • by Brompot (unregistered)
248132 in reply to 248111
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.

Re: Formatting a Phone Number - The Long Version

2009-03-09 09:41 • by 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.

Re: Formatting a Phone Number - The Long Version

2009-03-09 09:41 • by Addison (unregistered)
At least he put it in a function.

Re: Formatting a Phone Number - The Long Version

2009-03-09 09:42 • by Technomage (unregistered)
248135 in reply to 248116
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.

Re: Formatting a Phone Number - The Long Version

2009-03-09 09:44 • by 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

Re: Formatting a Phone Number - The Long Version

2009-03-09 09:44 • by Vollhorst (unregistered)
248137 in reply to 248117

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?

Re: Formatting a Phone Number - The Long Version

2009-03-09 09:45 • by monkeyPushButton (unregistered)
248138 in reply to 248125
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...

Re: Formatting a Phone Number - The Long Version

2009-03-09 09:45 • by Vollhorst (unregistered)
248139 in reply to 248117
Pim:
I want to see what the StripNonNumericCharacters function looks like!
Previous comment should have quoted this before the code segment. :/

Re: Formatting a Phone Number - The Long Version

2009-03-09 09:47 • by Trondster (unregistered)
248140 in reply to 248136
Typo:
Case 9 To 12
..of course. ;)

Re: Formatting a Phone Number - The Long Version

2009-03-09 09:48 • by 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.

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.

Re: Formatting a Phone Number - The Long Version

2009-03-09 09:48 • by dpm
248142 in reply to 248136
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.

Re: Formatting a Phone Number - The Long Version

2009-03-09 09:48 • by some schmoe (unregistered)
248143 in reply to 248122
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.</sarcasm>
Ah-ha!

Re: Formatting a Phone Number - The Long Version

2009-03-09 09:49 • by dpm
248144 in reply to 248141
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.

Re: Formatting a Phone Number - The Long Version

2009-03-09 09:50 • by 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.

Re: Formatting a Phone Number - The Long Version

2009-03-09 09:51 • by blindman (unregistered)
248146 in reply to 248111
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?

Re: Formatting a Phone Number - The Long Version

2009-03-09 09:51 • by Akoi Meexx (unregistered)
My god, I remember when I thought VB was awesome. How stupid and naive I was in those days...

Re: Formatting a Phone Number - The Long Version

2009-03-09 09:51 • by dpm
248148 in reply to 248141
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.

Re: Formatting a Phone Number - The Long Version

2009-03-09 09:52 • by Tempura (unregistered)
248149 in reply to 248113
Bah, ugly!

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

Re: Formatting a Phone Number - The Long Version

2009-03-09 09:53 • by 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.

Re: Formatting a Phone Number - The Long Version

2009-03-09 10:01 • by campkev
248151 in reply to 248150
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.
« PrevPage 1 | Page 2 | Page 3 | Page 4 | Page 5Next »

Add Comment