Trouble in Paradise


Bruce: This function always works on the same string. Notice how the first block of code saves the sTarget argument in the static sSave variable for later iterations. The rest of the code uses sSave and ignores sTarget. The next block of code uses StrSpan to get the start of the token, and then the next block uses StrBreak to get the end. Once you have the start and the end, you cut out the token and return it.


Joe: What the heck are you doing with that Mid$ in your StrSpan call? And there it is again in StrBreak. Why are you passing the string length?


Bruce: Well, the first argument is the string, the second argument is the starting position—it’s a static that gets updated each time through—and the last argument is the string length….


Joe: And the string length never changes. You’re calculating it again and again. Furthermore, it’s wrong. If you’re halfway through the string, you’re giving the whole length of the string. I’m surprised this works at all.


Jane: It’s legal. If you give a length that goes past the end of the string, it just takes the characters to the end. But it is kind of ugly. You can drop that whole argument:

iNew = StrSpan1(Mid$(sSave, iStart), sSeps)

Bruce: Will that make it any faster?


Joe: I don’t remember the Mid$ code exactly, but I doubt that it will make much difference. The main point is that it will be right instead of working by accident.


Bruce: Embarrassing. I knew better than that.


Joe: You should be embarrassed.


Jane: Oh, come on. It’s the kind of mistake anybody might make.


Joe: I wouldn’t make it.


Mary: Joe, we don’t need this kind of negative attitude.


Joe: All right. So maybe anybody could make a slip. But the real problem with this code is deeper. It comes from coding C in Basic.