# 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

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

• Hmm, I would write it this way

for(\$index=5; \$index < 25; \$index++)

if(\$index == \$s.length) then

Less typing and still confusing

• @s0be said:

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

• @s0be said:

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.

• @s0be said:

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

• OK, forgive the ignorance, but what language is this?

@Chuck Mango said:

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)

• @OSvsOS said:

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.

• Yeah, that should be 250 instead of 500 ...

• @morbiuswilters said:

@s0be said:

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.

• @Zecc said:

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.

• @Welbog said:

@Zecc said:

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.

• @nat42 said:

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 said:

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!

•  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;

}

}

• 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);
```

• @campkev said:

@s0be said:

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.

• @Welbog said:

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.

• @Digitalbath said:

@Welbog said:

@Zecc said:

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

• 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

• @Digitalbath said:

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 was already ill, but now i'm sick

• @Welbog said:

@Digitalbath said:

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.

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

• 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";

}

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.