flawed string assignment and one way to avoid it
Posted: Thu Apr 06, 2023 3:02 pm
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.
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.
@Edit: Modified comment line containing function code to contain actual functioning code.
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$)
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.

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