|
The lengthy string length test
Last post 11-27-2008 7:21 PM by halcyon1234. 23 replies.
-
11-24-2008 3:45 PM
|
|
-
Chuck Mango


- Joined on 07-11-2008
- Posts 28
|
The lengthy string length test
I apologise if something like this has been posted before, but I have to share this gem I found in a cow-orker's code today.
How do you check if a string is between 5 and 25 characters long? Why of course.. if
s.length==5 || s.length==6 || s.length==7 || s.length==8 || s.length==9
|| s.length==10 || [...etc...] || s.length==24 || s.length==25 then
|
|
-
-
s0be


- Joined on 11-22-2005
- Posts 5
|
Re: The lengthy string length test
Some credit is due.... without knowing the language(VB), this does guarantee that not only is the length between 5 and 25, but also guarantees it's an integer... I'm not saying that: if(((int)s.Length == s.Length) && (5 <= s.Length) && (25 >= s.Length)) is worse, though....
|
|
-
-
OSvsOS


- Joined on 07-25-2008
- Posts 35
|
Re: The lengthy string length test
Hmm, I would write it this way for($index=5; $index < 25; $index++) if($index == $s.length) then Less typing and still confusing :)
|
|
-
-
campkev


- Joined on 02-14-2006
- Posts 205
|
Re: The lengthy string length test
s0be:
Some credit is due....
without knowing the language(VB), this does guarantee that not only is the length between 5 and 25, but also guarantees it's an integer...
I'm not saying that:
if(((int)s.Length == s.Length) && (5 <= s.Length) && (25 >= s.Length))
is worse, though....
if s.length returns something other than an integer, you have way bigger issues to worry about
"You can call me anything you want, just don't call me late for dinner" "Or penis-butt."
|
|
-
-
Chuck Mango


- Joined on 07-11-2008
- Posts 28
|
Re: The lengthy string length test
s0be: Some credit is due.... without knowing the language(VB), this does guarantee that not only is the length between 5 and 25, but also guarantees it's an integer... I'm not saying that: if(((int)s.Length == s.Length) && (5 <= s.Length) && (25 >= s.Length)) is worse, though....
My bad for anonymising the pseudo code into something that looks like Javascript, but the original code has no dynamic type checking issues. To maximise your WTF enjoyment, imagine it was C and it's using strlen() :) I bet he's grateful that the maximum length to check wasn't say 50,000 characters.
|
|
-
-
morbiuswilters


- Joined on 01-15-2008
- East Coast Represent!
- Posts 4,990
|
Re: The lengthy string length test
s0be:Some credit is due.... without knowing the language(VB), this does guarantee that not only is the length between 5 and 25, but also guarantees it's an integer... I'm not saying that: if(((int)s.Length == s.Length) && (5 <= s.Length) && (25 >= s.Length)) is worse, though....
You have been registered for 3 years. You have 4 posts. This is the stupidest thing I have read on TDWTF*.
|
|
-
-
nat42


- Joined on 05-29-2008
- Posts 21
|
Re: The lengthy string length test
OK, forgive the ignorance, but what language is this? Chuck Mango:if
s.length==5 || s.length==6 || s.length==7 || s.length==8 || s.length==9
|| s.length==10 || [...etc...] || s.length==24 || s.length==25 then
I'm not up with developments in VB / VB .net, but is that what this is - and if so, when did it get a || operator - which seems to function c style no less?!! This reminds me however: I wish I could post the awesome hilarity that is one of my company's few surviving Excel/VBA, but its devs would kill me (I'm just the poor support tech) - I thought that I didn't know VB until I saw its code and it became clear I could know far less, it's filled with a kind of paranoid ignorance of the basics of the language and Excel that's timeless. I hope I can divolge this much: the original programmer was obsessed with ensuring that things would die - from removing a toolbar 3-4 times in different functions which only call each other (comments like "just to be sure..."), to religiously setting local variables to vbNull just before they went out of scope (and not before this)
|
|
-
-
Zecc


- Joined on 06-12-2007
- Location, Location, Location
- Posts 656
|
Re: The lengthy string length test
OSvsOS:Hmm, I would write it this way for($index=5; $index < 25; $index++) if($index == $s.length) then Less typing and still confusing :)
Got a bug there. It should read $index <= 25. Anyway: $x = $s.length; $y = $x * $x - 500 * $x + 125; // ($x - 5) * ($x - 25); if( $y <= 0 ){ ....
There's got to be some guy's law that says I now have a bug in my implementation.
If mixed metaphors were illegal, I'd be having an indigestion. typeof NaN == 'number'
|
|
-
-
Zecc


- Joined on 06-12-2007
- Location, Location, Location
- Posts 656
|
Re: The lengthy string length test
Yeah, that should be 250 instead of 500 ...
If mixed metaphors were illegal, I'd be having an indigestion. typeof NaN == 'number'
|
|
-
-
tster


- Joined on 04-11-2006
- Natick, MA
- Posts 1,765
|
Re: The lengthy string length test
morbiuswilters: s0be:Some credit is due.... without knowing the language(VB), this does guarantee that not only is the length between 5 and 25, but also guarantees it's an integer... I'm not saying that: if(((int)s.Length == s.Length) && (5 <= s.Length) && (25 >= s.Length)) is worse, though....
You have been registered for 3 years. You have 4 posts. This is the stupidest thing I have read on TDWTF*. Perhaps in that language white space is only counted as half a character.
The pig go. Go is to the fountain. The pig put foot. Grunt. Foot in what? ketchup. The dove fly. Fly is in sky. The dove drop something. The something on the pig. The pig disgusting... see bio for the earth shattering ending.
|
|
-
-
Welbog


- Joined on 02-08-2007
- Posts 586
|
Re: The lengthy string length test
Zecc:Yeah, that should be 250 instead of 500 ...
If by 250 you mean 30...
(x-5)(x-25) = x^2 - 5x - 25x + 125 = x^2 - 30x + 125
It's not rocket science, it's parabolas.
|
|
-
-
Digitalbath


- Joined on 02-23-2006
- Posts 131
|
Re: The lengthy string length test
Welbog: Zecc:Yeah, that should be 250 instead of 500 ...
If by 250 you mean 30...
(x-5)(x-25) = x^2 - 5x - 25x + 125 = x^2 - 30x + 125
It's not rocket science, it's parabolas. FOIL FTW.
Pray for a tighter bomb pattern.
|
|
-
-
Chuck Mango


- Joined on 07-11-2008
- Posts 28
|
Re: The lengthy string length test
nat42:OK, forgive the ignorance, but what language is this? None. In my original message I neglected to mention that what I posted was a pseudo code representation of the original. We use a niche language at work, so I am too paranoid to post the original code. Hey, I read TDWTF at work :) nat42:This reminds me however: I wish I could post the awesome hilarity that is one of my company's few surviving Excel/VBA, but its devs would kill me (I'm just the poor support tech) - I thought that I didn't know VB until I saw its code and it became clear I could know far less, it's filled with a kind of paranoid ignorance of the basics of the language and Excel that's timeless. I hope I can divolge this much: the original programmer was obsessed with ensuring that things would die - from removing a toolbar 3-4 times in different functions which only call each other (comments like "just to be sure..."), to religiously setting local variables to vbNull just before they went out of scope (and not before this)
A fine example of cargo cult programming. I hope that's nothing you have to support!
|
|
-
-
HypocriteWorld


- Joined on 04-04-2008
- Posts 70
|
Re: The lengthy string length test
Reminds me of this nice code I ran into recently: // make a string of tabs based on indent level string indentTabs(int level) { switch(level) { case 1: return "\t"; case 2: return "\t\t"; case 3: return "\t\t\t"; case 4: return "\t\t\t\t"; case 5: return "\t\t\t\t\t"; case 6: return "\t\t\t\t\t\t"; default: string result = ""; for(int i = 0; i < level; i++) { result = result + "\t"; } return result; } }
((lambda (f) (f f)) (lambda (f) (f f)))
|
|
-
-
Ilya Ehrenburg


- Joined on 10-22-2008
- Ashkenas
- Posts 178
|
Re: The lengthy string length test
Heh, nice. Optimised for the most common cases, so it'll be blazingly fast for those. But indentations above level 6 aren't punished nearly enough, should be:
default: return "\t" + indentTabs(level-1);
|
|
-
-
danixdefcon5


- Joined on 01-09-2007
- Mexico City, DF, Mexico
- Posts 608
|
Re: The lengthy string length test
campkev: s0be:
Some credit is due....
without knowing the language(VB), this does guarantee that not only is the length between 5 and 25, but also guarantees it's an integer...
I'm not saying that:
if(((int)s.Length == s.Length) && (5 <= s.Length) && (25 >= s.Length))
is worse, though....
if s.length returns something other than an integer, you have way bigger issues to worry about
Reminds me of a WTF code snippet posted in the Sidebar that throws something other than an Exception; an int if I remember well.
|
|
-
-
morbiuswilters


- Joined on 01-15-2008
- East Coast Represent!
- Posts 4,990
|
Re: The lengthy string length test
Welbog:If by 250 you mean 30...
(x-5)(x-25) = x^2 - 5x - 25x + 125 = x^2 - 30x + 125
It's not rocket science, it's parabolas.
It might be ICBM science, though, since they have a parabolic trajectory.
|
|
-
-
Zecc


- Joined on 06-12-2007
- Location, Location, Location
- Posts 656
|
Re: The lengthy string length test
Digitalbath: Welbog: Zecc:Yeah, that should be 250 instead of 500 ...
If by 250 you mean 30...
(x-5)(x-25) = x^2 - 5x - 25x + 125 = x^2 - 30x + 125
It's not rocket science, it's parabolas. FOIL FTW.
Dispite my FOIL FAIL, the theory was right I guess.. I'm mean, I know! I'm quite sure of it. Maybe I should work on a version that uses bit manipulation instead. I'm thinking Karnaugh tables ...
If mixed metaphors were illegal, I'd be having an indigestion. typeof NaN == 'number'
|
|
-
-
vr602


- Joined on 11-07-2006
- London
- Posts 70
|
Re: The lengthy string length test
I want it in VBA and I want it recursive - faster that way.
Function IsIt5thru25(sTest As String) As Boolean Dim lPos As Long lPos = InStr(sTest, "|") If lPos = 0 Then sTest = "|" & sTest lPos = 1 End If If Right(sTest, 1) = "|" Then If lPos < 6 Or lPos > 26 Then IsIt5thru25 = False Else IsIt5thru25 = True End If Else sTest = Left(sTest, lPos - 1) & Mid(sTest, lPos + 1, 1) & "|" & Mid(sTest, lPos + 2) IsIt5thru25 = IsIt5thru25(sTest) End If End Function
|
|
-
-
Welbog


- Joined on 02-08-2007
- Posts 586
|
Re: The lengthy string length test
Digitalbath:FOIL FTW.
FOIL is a rule invented by people who can do math to help people who can't do math pretend they can do math. I take it as a personal insult that you would remind me of it.
|
|
-
-
t-bone


- Joined on 09-07-2005
- .be
- Posts 71
|
Re: The lengthy string length test
I was already ill, but now i'm sick
|
|
-
-
morbiuswilters


- Joined on 01-15-2008
- East Coast Represent!
- Posts 4,990
|
Re: The lengthy string length test
Welbog: Digitalbath:FOIL FTW.
FOIL is a rule invented by people who can do math to help people who can't do math pretend they can do math. I take it as a personal insult that you would remind me of it.
I don't even remember what they stand for, but I do remember being taught FOIL and FACE. I think FACE actually draws a face, or something. It always struck me as a tool to help profoundly retarded children eke out a passing grade in pre-algebra. It's funny I can remember the names and how much I hated them but not what they actually did. It was due to stuff like FOIL, FACE and so on that I stopped trying to learn math in a school setting. If we were introduced to a new topic they would insist on drilling it into us through hundreds of problem sets. By the time I got to college things moved a tiny bit faster but I was also expected to memorize 70-odd integration formulas. Considering that rote memorization is not my strong point and that consulting a list of the formulas would seem to be acceptable in any real-world scenario, I ended up giving up on formal math education, which sucks because it was always one of my favourite subjects.

|
|
-
-
Ilya Ehrenburg


- Joined on 10-22-2008
- Ashkenas
- Posts 178
|
Re: The lengthy string length test
Whoa! Somebody really thought up an acronym to teach children
how to use the distributive law twice? I am amazed and express my
sincerest commiserations to all who have been put through that ordeal.
|
|
-
-
halcyon1234


- Joined on 09-10-2008
- Posts 93
|
Re: The lengthy string length test
if ( ((int)((s.length).ToString("00")[0]) >= 47 && (int)((s.length).ToString("00")[0]) <= 53) || ((int)((s.length).ToString("00")[1]) == 49 && (int)((s.length).ToString("00")[0]) >= 54 && (int)((s.length).ToString("00")[0]) <= 47) { return (bool)"true"; }
|
|
Page 1 of 1 (24 items)
|
|
|