Page 1 of 1

flawed string assignment and one way to avoid it

Posted: Thu Apr 06, 2023 3:02 pm
by Demivec
Here is some code that demonstrates a string assignment issue that you should be wary of. I ran into the issue when I put together some quick debugging code and then couldn't understand why it wasn't showing me what I wanted.

Below is a simplified version of the code just showing the effect. When the code is ran it executes a test() procedure and adds debug information to a global output string both inside and outside the test() procedure. It displays the string after all is done.

Code: Select all

EnableExplicit
Global mystery$
Define testCounter, maxCounter = 5, a$

Procedure.i test()
  Static t
  t + 200
  mystery$ + " Time: " + t
  Debug mystery$
  ProcedureReturn t
EndProcedure

While testCounter <= maxCounter
  ;Do not make an assignment like the following; the 'old' value of mystery$ is used before test() is executed.
  mystery$ + ";  " + testCounter + ": " + test()
  ;Instead separate the before and after into two separate variables before
  ;recombining them so that the changes in test() can be accounted for properly.
  ;a$ = ";  " + testCounter + ": " + test(): mystery$ + a$
  testCounter + 1
Wend

MessageRequester("Results", mystery$)
The problem is with the addition to the global mystery$ both inside and outside the procedure being combined into one assignment. I figured out the problem and the solution but for fun see if you can see the issue.

If you run the debugger the contents of mystery$ are shown as it is inside the test() procedure and again the contents are shown in a MessageRequester() after all is done. They do not agree and that highlights the bad assignment issue. It isn't a bug but it is bad programming. I may be the only one who would ever hack something together like this but I thought I would post it here as an example of what not to do. :wink:

@Edit: Modified comment line containing function code to contain actual functioning code. :)

Re: flawed string assignment and one way to avoid it

Posted: Fri Apr 07, 2023 1:21 am
by Lunasole
Ha, just looks like one of many ways to shot off own leg in languages like C or PB here.
You should also use Goto more often to make your code much interesting :mrgreen: That example would look much better too if add some goto.
I too, didn't used goto for too long ago, should use in some code... thanks for remembering me about that with your example^^

Re: flawed string assignment and one way to avoid it

Posted: Fri Apr 07, 2023 1:41 am
by Lunasole
I also wondered a bit that it mixes string with int without problems.

Unfortunately have no much own examples like this, I had one related to Debug command, but was too busy to post it and then forgotten.

Re: flawed string assignment and one way to avoid it

Posted: Fri Apr 07, 2023 3:09 am
by jacdelad
Lunasole wrote: Fri Apr 07, 2023 1:41 am I also wondered a bit that it mixes string with int without problems.
Afaik PureBasic auto-converts variables and other stuff into strings as long as the first part is a string.

Re: flawed string assignment and one way to avoid it

Posted: Fri Apr 07, 2023 3:53 am
by Demivec
jacdelad wrote: Fri Apr 07, 2023 3:09 am
Lunasole wrote: Fri Apr 07, 2023 1:41 am I also wondered a bit that it mixes string with int without problems.
Afaik PureBasic auto-converts variables and other stuff into strings as long as the first part is a string.
Yes, that is covered in the manual under Variable, Types and Operators and is part of the description for the '+' operator.