Page 1 of 3
What is best... define.s variable or define variable.s
Posted: Fri Jul 29, 2022 10:53 pm
by Oso
I'm new to PureBasic but I've noticed in some examples that define.s can be used to define a string. But also, it seems helpful to suffix a string with ".s"
Which is regarded as the best way in PureBasic? It seems to me that variable.s is more descriptive throughout the rest of the code. What is the best convention to use, that expert coders prefer?
Re: What is best... define.s variable or define variable.s
Posted: Fri Jul 29, 2022 11:07 pm
by jacdelad
Technically it doesn't matter, but Define.s is more convenient when declaring a lot of variable with one type:
Code: Select all
Define a.s,b.s,c.s,d.s,e.s;...
;vs
Define.s a,b,c,d,e;...
I'm not an expert, but I declare my variable in a wild horde, so I use the normal Define and append the type to each variable.
Re: What is best... define.s variable or define variable.s
Posted: Fri Jul 29, 2022 11:09 pm
by mestnyi
Code: Select all
Define var.s
Define.s var1, var2, var3...
Re: What is best... define.s variable or define variable.s
Posted: Fri Jul 29, 2022 11:30 pm
by Oso
mestnyi wrote: Fri Jul 29, 2022 11:09 pm
Code: Select all
Define var.s
Define.s var1, var2, var3...
Yeah, okay I think my preference would be to define blocks of variables according to their association with each other, rather than all the strings together and then all the numerics together.
This is because when we look at code logic, it can be helpful to group variables according to their relationship. For instance...
Code: Select all
define custcounter.i
define custname.s
Code: Select all
define productcode.s
define productprice.l
define invoicetotal.l
Personally (and this is very much a personal thing) I wouldn't like to see all the strings together and then all the numerics together. Also, software modifications and additions are often best defined in their own right, so they are in a block of code of their own.
Re: What is best... define.s variable or define variable.s
Posted: Fri Jul 29, 2022 11:51 pm
by jacdelad
Well, that's up to you.

Re: What is best... define.s variable or define variable.s
Posted: Sat Jul 30, 2022 12:11 am
by Oso
jacdelad wrote: Fri Jul 29, 2022 11:51 pm
Well, that's up to you.
This is getting interesting. I just found that it doesn't matter how the variable is referenced, after it's defined. These all appear to be equal...
Code: Select all
Define.s name
name.s=Input()
Define.s name.s
name=Input()
Define.s name.s
name.s=Input()
Define name.s
name=Input()
Re: What is best... define.s variable or define variable.s
Posted: Sat Jul 30, 2022 1:06 am
by jacdelad
Yes, the type is only needed for the declaration. This also means each name can only be used once, for one variable
...will result in an error.
Also, string variables can have an appended "$", like
Code: Select all
Define MyString$="Hello"
Debug MyString$
Re: What is best... define.s variable or define variable.s
Posted: Sat Jul 30, 2022 9:00 am
by Shardik
You should also keep in mind that MyString.S and MyString$ are different variables:
Code: Select all
Define MyString.S = "Test 1"
Define MyString$ = "Test 2"
Debug MyString ; "Test 1"
Debug MyString$ ; "Test 2"
Re: What is best... define.s variable or define variable.s
Posted: Sat Jul 30, 2022 9:36 am
by BarryG
And following on from Shardik's post, you can also do this:
Code: Select all
Define MyString.s = "Test 1"
Define MyString$ = "Test 2"
Define *MyString = @"Test 3"
Debug MyString ; "Test 1"
Debug MyString$ ; "Test 2"
Debug PeekS(*MyString) ; "Test 3"
Re: What is best... define.s variable or define variable.s
Posted: Sat Jul 30, 2022 10:22 am
by #NULL
In addition to barry's post above:
When using addresses of string literals, keep in mind that identical literals will be reused, so different pointer variables could point to the same string, affecting each other:
Code: Select all
EnableExplicit
Define s1.s = "test"
Define s2.s = "test"
s2 = "test2"
Debug s1 ; test
Debug s2 ; test2
Define *s3 = @"test"
Define *s4 = @"test"
PokeS(*s4, "x")
Debug PeekS(*s3) ; x
Debug PeekS(*s3) ; x
But I don't know why you would want to use pointers to string literals, especially poking at them will corrupt memory if the poked string is to long. There is no automatic reallocation like with normal string operations:
Code: Select all
*s = @"0123"
ShowMemoryViewer(*s, 100)
Delay(1000)
PokeS(*s, Space(38))
ShowMemoryViewer(*s, 100)
Re: What is best... define.s variable or define variable.s
Posted: Sat Jul 30, 2022 11:34 am
by infratec
To be clear:
once you defined a variable, the trailing .x is no longer needed.
Personally I prefer the $ at the end for strings, this is always at the end of the variable and so I see that it is a string.
And in general: your first command in any code should be:
Re: What is best... define.s variable or define variable.s
Posted: Sat Jul 30, 2022 1:30 pm
by Mijikai
And the last command should always be:
to properly signal the OS that the program has ended.
The OS may want to perform additional cleanup tasks!
Re: What is best... define.s variable or define variable.s
Posted: Sat Jul 30, 2022 2:52 pm
by Paul
Mijikai wrote: Sat Jul 30, 2022 1:30 pm
And the last command should always be:
to properly signal the OS that the program has ended.
The OS may want to perform additional cleanup tasks!
Just a note... PureBasic automatically inserts "End" internally at the end of your program so unless you are using "End" to terminate your program early, you may be duplicating things.
Using "C" backend...
Code: Select all
SYS_Quit();
void SYS_Quit() {
PB_EndFunctions();
f_heapdestroy_(PB_MemoryBase);
f_exitprocess_(PB_ExitCode);
}
Using "C" backend and including "End" at the end of your program...
Code: Select all
// End
SYS_Quit();
//
SYS_Quit();
void SYS_Quit() {
PB_EndFunctions();
f_heapdestroy_(PB_MemoryBase);
f_exitprocess_(PB_ExitCode);
}
Using "ASM" backend...
Code: Select all
_PB_EOP:
CALL PB_EndFunctions
MOV rcx,[PB_MemoryBase]
CALL HeapDestroy
MOV rcx,[PB_ExitCode]
CALL ExitProcess
Using "ASM" backend and including "End" at the end of your program...
Code: Select all
; End
JMP _PB_EOP
;
_PB_EOP:
CALL PB_EndFunctions
MOV rcx,[PB_MemoryBase]
CALL HeapDestroy
MOV rcx,[PB_ExitCode]
CALL ExitProcess
Re: What is best... define.s variable or define variable.s
Posted: Sat Jul 30, 2022 3:25 pm
by Mijikai
Ok so PB holds your hand, the manual should reflect this behaviour.
Re: What is best... define.s variable or define variable.s
Posted: Sat Jul 30, 2022 5:45 pm
by skywalk
If you eventually decide to automate your code for documentation or jumping around source code for definitions of variables and other data structures, you should think what is simple?
For me, always enableexplicit, and 1 data type per define.
Define.i x,y,z
Strings are always mystring$
Only 1 array or map or list per define.
Then your parser logic is easy peasy.
Standardize your coding semantics and you minimize your tech burden with debugging and automation.