flawed string assignment and one way to avoid it

Everything else that doesn't fall into one of the other PB categories.
User avatar
Demivec
Addict
Addict
Posts: 4269
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

flawed string assignment and one way to avoid it

Post 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. :)
Last edited by Demivec on Tue Apr 11, 2023 12:08 pm, edited 1 time in total.
User avatar
Lunasole
Addict
Addict
Posts: 1091
Joined: Mon Oct 26, 2015 2:55 am
Location: UA
Contact:

Re: flawed string assignment and one way to avoid it

Post 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^^
"W̷i̷s̷h̷i̷n̷g o̷n a s̷t̷a̷r"
User avatar
Lunasole
Addict
Addict
Posts: 1091
Joined: Mon Oct 26, 2015 2:55 am
Location: UA
Contact:

Re: flawed string assignment and one way to avoid it

Post 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.
"W̷i̷s̷h̷i̷n̷g o̷n a s̷t̷a̷r"
User avatar
jacdelad
Addict
Addict
Posts: 2010
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: flawed string assignment and one way to avoid it

Post 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.
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
User avatar
Demivec
Addict
Addict
Posts: 4269
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: flawed string assignment and one way to avoid it

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