Science Forums
User Name
Password
Science Social Network
home    members    help/rules    who is online    contact   

Go Back   Science Forums > Physical Sciences Forums > Computer Science
Become a science forums sponsor today
Reply
 
LinkBack Thread Tools
Old 10-09-2008   #1 (permalink)
Buffy's Avatar
Resident Slayer

Hypography Staff Member
Administrator

 



What does this code do?

Have you ever been wading through someone else's code and run across something that you had no idea what it did and you were too lazy to work it out?

That's what this thread is for.

So to get things going, what does this code do?
Code:
varURL = StrReverse(varURL)
intCut = InStr(varURL, "", 0)
varURL = Mid(varURL, intCut + 1)
varURL = StrReverse(varURL)
Obviously its in VisualBasic and the input string is a URL.

The obscure we see eventually. The completely obvious, it seems, takes longer,
Buffy


----------------
"If you do not agree with anything I say, I'll not only retract it, but deny under oath that I ever said it!"
__________________________________________________ ______________-- Tom Lehrer

"The shrinks diagnosed me a sociopath with paranoid delusions. But they’re just out to get me cause I threatened to kill them."


Forum Administrator
Hypography Science Forums - Science for Boys and Girls! Its not for nothing that we hang out here.
Reply With Quote
Old 10-09-2008   #2 (permalink)
Qfwfq's Avatar
Exhausted Gondolier

Hypography Staff Member
Administrator

 



Re: What does this code do?

Gosh Buffy years ago I worked at a place where hoards of improvised VB6 devs had done the most imaginative things you could think of, but I've no idea what InStr returns if you tell it to search for the empty string in the midst of the first argument. By reason it ought to occur just befor the start and just after the end as well as between consecutive characters, should it return sem-integer values?

Anyway it strikes me Mid is missing an argument but I suppose the result will just run to the end and I suppose, more seriously, that the call to Instr will return 0 or maybe -1 so you should just get the initial value of the string. Could it be that originally there was a string expression, later replaced by "" instead of removing or commenting out the lines? Obviously this would have sought the reversal of the expression and removed it if found, but they could have just reversed the expression instead.


----------------
Who's afraid of the Big Black Hole?????

Go Black Hole! W the Black Hole!

Hasta que el agujero negro nos traga, siempre!

Hypography Forum PITA...... er, Administrator.
Reply With Quote
Old 10-09-2008   #3 (permalink)
Buffy's Avatar
Resident Slayer

Hypography Staff Member
Administrator

 



Re: What does this code do?

Quote:
Originally Posted by Qfwfq View Post
Gosh Buffy years ago I worked at a place where hoards of improvised VB6 devs had done the most imaginative things you could think of...
I'd use "imaginative" rather loosely here: I've seen code created that was the moral equivalent of "let's try anything and see if something happens" followed by, "it's working now, don't touch it."

Results in lots of "junk DNA" being created!
Quote:
Originally Posted by Qfwfq View Post
...but I've no idea what InStr returns if you tell it to search for the empty string in the midst of the first argument. By reason it ought to occur just befor the start and just after the end as well as between consecutive characters, should it return sem-integer values?
I'm positive it has some sort of bizarre side-effects, which would be the only reason for this code's existence.
Quote:
Originally Posted by Qfwfq View Post
Anyway it strikes me Mid is missing an argument but I suppose the result will just run to the end...
That one was easy: it shows up in VB documentation as something like "Mid(str, start) is equivalent to Right(str, start)"
Quote:
Originally Posted by Qfwfq View Post
...and I suppose, more seriously...
No seriousity necessary!
Quote:
Originally Posted by Qfwfq View Post
that the call to Instr will return 0 or maybe -1
Instr returns -1 if not found, but is the empty string found?

Quote:
Originally Posted by Qfwfq View Post
Could it be that originally there was a string expression, later replaced by "" instead of removing or commenting out the lines? Obviously this would have sought the reversal of the expression and removed it if found, but they could have just reversed the expression instead.
Sure, but that's where this kind of stuff gets *interesting*: what possible reason could one have for doing that in the context of *reversing* the string? We've got a hint because the variable is known to be a URL, but that's all....

BTW: yes, this code snippet has been sitting in a piece of commercial code--on an active code path--for over 5 years...

In San Francisco, Haloween is redundant,
Buffy


----------------
"If you do not agree with anything I say, I'll not only retract it, but deny under oath that I ever said it!"
__________________________________________________ ______________-- Tom Lehrer

"The shrinks diagnosed me a sociopath with paranoid delusions. But they’re just out to get me cause I threatened to kill them."


Forum Administrator
Hypography Science Forums - Science for Boys and Girls! Its not for nothing that we hang out here.
Reply With Quote
Old 10-09-2008   #4 (permalink)
TheBigDog's Avatar
Doing the Impossible

Hypography Staff Member
Moderator
Gallery Curator

 



Re: What does this code do?

When performing an InStr function for an empty string the function will return the start position of the search. Since the start position is omitted the search begins at the start of the string.

InStr Function

It appears to me that the code simply strips the last character off of the string, but it does it by running twice around the house.

It reverses it
It finds he first position
It finds the substring from the second position to the end
It reverses the substring that it found

I would guess that doing a ValURL = Mid(VarURL,1,Len(VarURL)-1) would do the same thing.


----------------
aka TheBigDog - Hypography Full Freaking Moderator
Become a Hypography sponsor!
The truth is incontravertible; malice may attack it, ignorance may deride it, but in the end there it is. - Winston Churchill

TheBigDog's recommended reading: The Science of Success - Charles G. Koch

A neutron goes into a bar and asks the bartender, "How much for a beer?"
The bartender replies, "For you, no charge."

Last edited by TheBigDog; 10-09-2008 at 08:12 PM.
Reply With Quote
Old 10-10-2008   #5 (permalink)
Qfwfq's Avatar
Exhausted Gondolier

Hypography Staff Member
Administrator

 



Re: What does this code do?

Quote:
Originally Posted by Buffy View Post
I'd use "imaginative" rather loosely here: I've seen code created that was the moral equivalent of "let's try anything and see if something happens" followed by, "it's working now, don't touch it."
Yeah, the kind of thing I meant as "imaginative".

Quote:
Originally Posted by Buffy View Post
I'm positive it has some sort of bizarre side-effects, which would be the only reason for this code's existence.
The question is: Could it? (I mean, apart from setting a value to intCut!).... !!!!! Garsh, you're right!!!!
Now I get it!!!!

Quote:
Originally Posted by Buffy View Post
That one was easy: it shows up in VB documentation as something like "Mid(str, start) is equivalent to Right(str, start)"
You really thought I'd search MSDN instead of trusting my foggy memories and logic?

Quote:
Originally Posted by Buffy View Post
Instr returns -1 if not found, but is the empty string found?
I thought I had already semi-answered that yesterday, although more or less seriously....

A doggone mathematician as I strive to be would say there are as many of them as you like, before and after each character of any string! Unfortunately I'd never expect the VB6 team to be so subtle and even less most of their audience. So: -1? But what do Right or Mid do if you feed them start = -1, surely they don't interpret the length bytes of the VBString as a unicode character?

Quote:
Originally Posted by Buffy View Post
We've got a hint because the variable is known to be a URL, but that's all....
Garsh, I haed fergit aboot yon!

If you hadn't said
Quote:
Originally Posted by Buffy View Post
BTW: yes, this code snippet has been sitting in a piece of commercial code--on an active code path--for over 5 years...
I would have now said that perhaps the "" used to be "/" or "&" or "=" but are you saying it's actually useful for it to be ""?

Gee, this is fun, let's see what TBD says now...


----------------
Who's afraid of the Big Black Hole?????

Go Black Hole! W the Black Hole!

Hasta que el agujero negro nos traga, siempre!

Hypography Forum PITA...... er, Administrator.
Reply With Quote
Old 10-10-2008   #6 (permalink)
alexander's Avatar
Resident USSRian

Hypography Staff Member
Administrator
Gallery Curator
Dev Team Member

Latest blog entry:
Open-Source HIDS
 
alexander has a brilliant futurealexander has a brilliant futurealexander has a brilliant futurealexander has a brilliant futurealexander has a brilliant futurealexander has a brilliant futurealexander has a brilliant futurealexander has a brilliant futurealexander has a brilliant futurealexander has a brilliant future
Send a message via AIM to alexander
 



Re: What does this code do?

Agree with TBD, actually read his comment after coming to the same conclusion

the code first reverses the string
returns the start of the string (dumb, if you ask me)
then rereads the string from the middle, in this case ommiting the first character in the reversed string
then reverses the string again to get the result without the last character. this will also strip spaces, if i'm not mistaken, that's the reason to find the start of the string

i changed the TBD's code a little, to compensate for spaces:

ValURL = Left(RTrim(VarURL),Len(RTrim(VarURL))-1)


----------------
And remember that great question that Pierre-Simon Laplace and Sir Isaac Newton, Andrei Markov and David Hilbert, Richard Feynman and Enrico Fermi, Albert Einstein and Edmund Halley did not come to ask throughout all of their dedication and work: "Who the hell is IMing me?"


This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 License.

Last edited by alexander; 10-10-2008 at 06:09 AM.
Reply With Quote
Old 10-10-2008   #7 (permalink)
alexander's Avatar
Resident USSRian

Hypography Staff Member
Administrator
Gallery Curator
Dev Team Member

Latest blog entry:
Open-Source HIDS
 
alexander has a brilliant futurealexander has a brilliant futurealexander has a brilliant futurealexander has a brilliant futurealexander has a brilliant futurealexander has a brilliant futurealexander has a brilliant futurealexander has a brilliant futurealexander has a brilliant futurealexander has a brilliant future
Send a message via AIM to alexander
 



Re: What does this code do?

oh just had a stroke of genius to save you more ticks


ValURL = Left(VarURL,Len(RTrim(VarURL))-1)


----------------
And remember that great question that Pierre-Simon Laplace and Sir Isaac Newton, Andrei Markov and David Hilbert, Richard Feynman and Enrico Fermi, Albert Einstein and Edmund Halley did not come to ask throughout all of their dedication and work: "Who the hell is IMing me?"


This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 License.
Reply With Quote
Old 10-10-2008   #8 (permalink)
TheBigDog's Avatar
Doing the Impossible

Hypography Staff Member
Moderator
Gallery Curator

 



Re: What does this code do?

Quote:
Originally Posted by alexander View Post
oh just had a stroke of genius to save you more ticks


ValURL = Left(VarURL,Len(RTrim(VarURL))-1)
Trim chops the blank spaces from both sides at once, so if you want to remove them simply use...

ValURL = Left(Trim(VarURL),Len(Trim(VarURL))-1)

But the code shown doesn't concern itself with blank spaces. I tried to think of a reason for going so round-aboutly but there was no logic to it, so I can only conclude that the person did this because they were smoking crack.

Bill


----------------
aka TheBigDog - Hypography Full Freaking Moderator
Become a Hypography sponsor!
The truth is incontravertible; malice may attack it, ignorance may deride it, but in the end there it is. - Winston Churchill

TheBigDog's recommended reading: The Science of Success - Charles G. Koch

A neutron goes into a bar and asks the bartender, "How much for a beer?"
The bartender replies, "For you, no charge."
Reply With Quote
Old 10-10-2008   #9 (permalink)
alexander's Avatar
Resident USSRian

Hypography Staff Member
Administrator
Gallery Curator
Dev Team Member

Latest blog entry:
Open-Source HIDS
 
alexander has a brilliant futurealexander has a brilliant futurealexander has a brilliant futurealexander has a brilliant futurealexander has a brilliant futurealexander has a brilliant futurealexander has a brilliant futurealexander has a brilliant futurealexander has a brilliant futurealexander has a brilliant future
Send a message via AIM to alexander
 



Re: What does this code do?

no, no, i have been thinking about that problem all day, too, and i think its because of a combination of factors, they had more then 6 hours of sleep and were really sober....

Maybe the person never heard of the string functions?

btw i thought of making it so it trims both right and left hand spaces, the faster way of doing that would be to split that into two lines that would save some ticks....

ValURL=Trim(ValURL)
and then
ValURL=Left(ValURL,Len(ValURL)-1)

but no, i was thinking that the only function that could potentially strip spaces in their code would be the inStr function, so i thought, well, maybe, as a side effect it stripped spaces... ?


----------------
And remember that great question that Pierre-Simon Laplace and Sir Isaac Newton, Andrei Markov and David Hilbert, Richard Feynman and Enrico Fermi, Albert Einstein and Edmund Halley did not come to ask throughout all of their dedication and work: "Who the hell is IMing me?"


This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 License.
Reply With Quote
Old 10-11-2008   #10 (permalink)
TheBigDog's Avatar
Doing the Impossible

Hypography Staff Member
Moderator
Gallery Curator

 



Re: What does this code do?

I am not sure they were stripping spaces at all, but just arbitrarily removing whatever the last character is. I suppose that the text this was handling has something like a carriage return (chr(13)) as the last space and this was the way they chose to remove it. My other guess is that this code actually does nothing, and it was simply forgotten and never removed. It is like the human appendix; it does something, but removing it has no noticeable effect.

Bill


----------------
aka TheBigDog - Hypography Full Freaking Moderator
Become a Hypography sponsor!
The truth is incontravertible; malice may attack it, ignorance may deride it, but in the end there it is. - Winston Churchill

TheBigDog's recommended reading: The Science of Success - Charles G. Koch

A neutron goes into a bar and asks the bartender, "How much for a beer?"
The bartender replies, "For you, no charge."
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
How would you code this? sanctus Computer Science 4 03-05-2008 07:06 AM
the da vinci code plain_heropt1 Books, movies, games 8 01-09-2008 09:57 PM
New code discovered in DNA C1ay News in Brief 1 07-25-2006 03:44 PM
Break the code! ronthepon Watercooler 25 07-21-2006 02:12 AM
URL code tag test Turtle Test forum 7 07-15-2006 02:21 PM


All times are GMT -8. The time now is 02:50 PM.

Hypography?

Hypography [n.]: A combination of "hyperlink" and "bibliography" - ie, a list of links to electronic documents. Comparable to discography and bibliography, but not cartography.

We have been online since May 2000, and aim to be the best place to find and share science-related content of all kinds.

Share the love!

Please add more science to your life. Use our RSS feeds on your blog, your portal, or your favorite feedreader!

Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0 ©2008, Crawlability, Inc. Copyright © 2000-2008 Hypography
Part of the Hypography - Science for Everyone Network