Jump to content
Science Forums

Recommended Posts

Posted

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?

 

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

Posted

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

Posted
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! :bouquet:

...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? :xx: :D
I'm positive it has some sort of bizarre side-effects, which would be the only reason for this code's existence.
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)"
...and I suppose, more seriously...

No seriousity necessary! :embarassed:

that the call to Instr will return 0 or maybe -1
Instr returns -1 if not found, but is the empty string found?

 

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. :doh:
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, :confused:

Buffy

Posted

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.

Posted
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". ;)

 

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!).... :doh:!!!!! Garsh, you're right!!!!

Now I get it!!!! :doh:

 

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?

 

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 :evil: would say there are as many of them as you like, before and after each character of any string! :hihi: 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? :hihi:

 

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

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

Posted

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)

Posted
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

Posted

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

Posted

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

Posted

The code isn’t syntactically correct generic or Visual BASIC, because the instr() has arguments:

instr([starting position as number,] value to search as string, string to search for as string)

while the code has

instr(string, string, number)

 

Like ordinary set theory and most programming languages, the zero length string ("") is considered contained in every string, so instr("anything","") = 1

 

If it’s fixed to be syntactically correct, it behaves as alexander described in post #7, stripping the rightmost character from the string, or doing nothing if the string is the zero length string.

 

Here the code, compared to left(,len()-1), from a VB immediate mode window:

A$=" testing 123 ":?"[";A$;"]"
[ testing 123 ]
?"[";left(A$,len(A$)-1);"]"
[ testing 123]
A$=StrReverse(A$):?A$
321 gnitset 
B=instr(A$,""):?B
1 
A$=mid(A$,B+1):?A$
321 gnitset 
A$=StrReverse(A$):?"[";A$;"]"
[ testing 123]

I think TDB’s right, that this code’s not likely being executed, as unless masked by an error trap, it would cause a runtime error.

 

Even if it was syntactically correct, it’s pointlessly complex. My guess is that it was written by someone inexperienced (or simply not very good) with BASIC, or at programming in general.

Posted
The code isn’t syntactically correct generic or Visual BASIC, because the instr() has arguments:

instr([starting position as number,] value to search as string, string to search for as string)

while the code has

instr(string, string, number)

 

Actually the VB InStr() function has 4 arguments. The syntax for VB InStr() is InStr([start,] string1, string2 [, compare]) where the [start] and [compare] arguments are optional but, if the compare argument is given then the start argument is required thus, it is syntactically incorrect as quoted.

 

If a 1 was given as a start position then InStr() would return a 1 and the code would strip the last character from the string. As written it should give a runtime error.

Posted

I dunno about the arguments, I'm not looking through the docs, but:

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.
Actually I agree it would remove the last character, because of the + 1 (which I kept forgetting to take into account). To do just this, by reversal, it would be far simpler to write:

 

varURL = StrReverse(varURL)
varURL = Mid(varURL, 1)
varURL = StrReverse(varURL)

 

I still think it was meant to remove everything after the last occurrence of some string expression, later replaced by the literal "" as a way to disactivate it but leaving it in for future need. I often do "dumb" things because it can be quicker than commenting something out when it might need to be restored.

 

The 1 added to intCut suggests it was a single character and the stripping included it, like perhaps it was "/" because previously they needed to strip away /filename.ext?querystring. If this was so, I'd say Buffy's code was a clever enough way of doing it.

Posted

Around the turn of 1980, a lot of BASIC variants and other languages had a pretty string operator/function usually called “slicers”, which looked something like

A$ = A$[2,] : B$ = B$[3,7]: C$= C$[i,J]: D$ = D$[5]

as an equivalent of the standard BASIC

A$ = mid$(A$,2): B$= mid$(A$,3,5) : C$ = mid$(A$,I,J-I+1): D$ = mid$(D$,5,1)

 

I still have an old TS2068 that has a built-in BASIC (Sinclair BASIC) that uses slicers that look like

A$ = A$(2 TO ) : B$ = B$(3 TO 7): C$ = C$(I TO J) : D$ = D$(5 TO 5):

 

I recall a BASIC – I think it was Waterloo BASIC – that interpreted negative second arguments as implying subtraction from the string length, so the standard BASIC

A$ = left$(A$,len(A$)-1)

or

A$ = mid$(A$,1,len(A$)-1)

could be written

A$ = A$[,-1]

which is pretty hard to beat for terseness. :)

 

Slicers are nifty syntax. Like most syntaxes, they don’t promote anything revolutionary, but did make for nice, readable code. I regret that they’re not more common. :(

Posted

I have a hard enough time bouncing back and forth between VBA and vb.net, much less remembering syntax from BASIC (I was an Atari 800 user in the early 80's) VBA uses Functions such as Mid to do string manipulations, while vb.net treats each string as an object which has built in Methods for doing string manipulations. So Mid(string,1,5) in VBA is the same as string.substring(1,5) in vb.net. Len(string) is the same as string.length.

 

I am sure there is a reason for this switch, but I could not tell you exactly what it is. :(

 

Bill

Posted
I am sure there is a reason for this switch, but I could not tell you exactly what it is.
I'd say it's what they call object orientation. Nowadays it even gets brought to the extreme, although it does make sense to have a class for String.

 

Make everything an object, man! Anything! Even when it doesn't need to be one!

 

In Coffee and prolly in ButterflyNet the only way to have a libe of functions is to put'em in a package of classes. Those who design them often make a whole host of classes where fewer of them would suffice.

Posted
I am sure there is a reason for this switch, but I could not tell you exactly what it is. :(

 

Follow the money. If the language developer changes the syntax enough how much more will you need to spend on learning materials to jump on the new bandwagon?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...