What is best... define.s variable or define variable.s

Just starting out? Need help? Post your questions and find answers here.
User avatar
Oso
User
User
Posts: 66
Joined: Wed Jul 20, 2022 10:09 am

What is best... define.s variable or define variable.s

Post 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?

Code: Select all

define.s variable

Code: Select all

define variable.s
User avatar
jacdelad
Enthusiast
Enthusiast
Posts: 431
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

Re: What is best... define.s variable or define variable.s

Post 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.
PureBasic 6.0/XProfan X4a/Embarcadero RAD Studio 11/Perl 5.2/Python 3.10
Windows 11/Ryzen 5800+/32GB RAM/Radeon 7770 OC/3TB SSD/70TB HDD
Synology DS920+/20GB RAM/38TB
mestnyi
Addict
Addict
Posts: 902
Joined: Mon Nov 25, 2013 6:41 am

Re: What is best... define.s variable or define variable.s

Post by mestnyi »

Code: Select all

Define var.s
Define.s var1, var2, var3...
User avatar
Oso
User
User
Posts: 66
Joined: Wed Jul 20, 2022 10:09 am

Re: What is best... define.s variable or define variable.s

Post 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.
User avatar
jacdelad
Enthusiast
Enthusiast
Posts: 431
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

Re: What is best... define.s variable or define variable.s

Post by jacdelad »

Well, that's up to you. :wink:
PureBasic 6.0/XProfan X4a/Embarcadero RAD Studio 11/Perl 5.2/Python 3.10
Windows 11/Ryzen 5800+/32GB RAM/Radeon 7770 OC/3TB SSD/70TB HDD
Synology DS920+/20GB RAM/38TB
User avatar
Oso
User
User
Posts: 66
Joined: Wed Jul 20, 2022 10:09 am

Re: What is best... define.s variable or define variable.s

Post by Oso »

jacdelad wrote: Fri Jul 29, 2022 11:51 pm Well, that's up to you. :wink:
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()
User avatar
jacdelad
Enthusiast
Enthusiast
Posts: 431
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

Re: What is best... define.s variable or define variable.s

Post by jacdelad »

Yes, the type is only needed for the declaration. This also means each name can only be used once, for one variable

Code: Select all

Define Var1.a,var1.s
...will result in an error.

Also, string variables can have an appended "$", like

Code: Select all

Define MyString$="Hello"
Debug MyString$
PureBasic 6.0/XProfan X4a/Embarcadero RAD Studio 11/Perl 5.2/Python 3.10
Windows 11/Ryzen 5800+/32GB RAM/Radeon 7770 OC/3TB SSD/70TB HDD
Synology DS920+/20GB RAM/38TB
User avatar
Shardik
Addict
Addict
Posts: 1903
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: What is best... define.s variable or define variable.s

Post 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"
BarryG
Addict
Addict
Posts: 2272
Joined: Thu Apr 18, 2019 8:17 am

Re: What is best... define.s variable or define variable.s

Post 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"
#NULL
Addict
Addict
Posts: 1424
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: What is best... define.s variable or define variable.s

Post 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)
infratec
Always Here
Always Here
Posts: 5876
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: What is best... define.s variable or define variable.s

Post 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:

Code: Select all

EnableExplicit
User avatar
Mijikai
Addict
Addict
Posts: 1087
Joined: Sun Sep 11, 2016 2:17 pm

Re: What is best... define.s variable or define variable.s

Post by Mijikai »

And the last command should always be:

Code: Select all

End
to properly signal the OS that the program has ended.
The OS may want to perform additional cleanup tasks!
User avatar
Paul
PureBasic Expert
PureBasic Expert
Posts: 1159
Joined: Fri Apr 25, 2003 4:34 pm
Location: Canada
Contact:

Re: What is best... define.s variable or define variable.s

Post by Paul »

Mijikai wrote: Sat Jul 30, 2022 1:30 pm And the last command should always be:

Code: Select all

End
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
Image Image
User avatar
Mijikai
Addict
Addict
Posts: 1087
Joined: Sun Sep 11, 2016 2:17 pm

Re: What is best... define.s variable or define variable.s

Post by Mijikai »

Ok so PB holds your hand, the manual should reflect this behaviour.
User avatar
skywalk
Addict
Addict
Posts: 3712
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: What is best... define.s variable or define variable.s

Post 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.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
Post Reply