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

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

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

Post by Oso »

#NULL wrote: Sun Jul 31, 2022 2:00 pm Menu > Tools > Structure Viewer > Tab 'Structures' > scroll down list to 'String' and doubleclick to see Definition
Thanks, is that the below, or something other?
https://imgur.com/a/GOhrA5D
#NULL
Addict
Addict
Posts: 1499
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 »

Yes, that's the one.
Little John
Addict
Addict
Posts: 4789
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

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

Post by Little John »

Oso wrote: Sun Jul 31, 2022 11:37 am Yes, $ is convenient and it makes it clear throughout the code. But I was thinking more in terms of the range of variable types, not only strings. For example, the PB documentation lists a dozen Basic Types, some of which include the below.

Code: Select all

.b byte
.c character
.u unicode
.l long
.i integer
.d double
Let's assume we've defined our variables at the top of our code. When someone else happens to be maintaining the main body of our code, it isn't always going to be clear what the types are, just from looking at variable names alone. It's necessary to keep referring back to the definitions, in other words.

Therefore, would it be reasonable to append all variables with their type, throughout the code? Or would that be regarded as unnecessary 'bloat' by many PB developers? It's often better to follow the developer community norms.
I think most of the variable types are only needed in structures, for instance for reading a specific file format, or when an API function requires particular data types. For normal variables, I personally almost exclusively use .i, .q., .d and $.
When a variable e.g. holds values in the range from 10 to 100 during the course of a program, some people use .b as type, because that is big enough for this variable. However, the native data type for the processor is .i (.i denotes a 32-bit variable in an x86 program, and a 64-bit variable in an x64 program), so that it's normally better to use this type – except when you have a huge array, list or map with that variable.

Anyway, I think on principle it would be reasonable to append all variables with their type, throughout the code. But I can't recall ever having seen code like that here on the forum. :-) For me personally, it's important anyway to split the code into logical blocks (aka procedures), which are as independent from each other as possible, and which are not too big. And in small procedures, the definition of a variable is never far away from any point where it is used.

Some people here on the forum are using something similar to Hungarian notation. Personally, I don't like that. If I would like to read the type of a variable every time it occurrs in the code, I actually would use what PureBasic offers: .i, .d etc. When I'm prepending a variable with a certain character, then it is not for showing its type, but its scope. So I use this:

Code: Select all

Global g_GlobalVar$
Define s_SharedVar.i
(and no prefix for variables that are neither Global nor Shared).
Last edited by Little John on Sun Jul 31, 2022 8:18 pm, edited 2 times in total.
User avatar
chikega
User
User
Posts: 40
Joined: Fri Dec 04, 2020 3:19 am

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

Post by chikega »

Not that this will make a big difference in ones code, but it's something I found by accident while playing with PB string types. It is not documented, but:

Code: Select all

Define$
appears to function the same as

Code: Select all

Define.s 
Gary E Chike DMD MS
'Experience is what you get when you don't get what you want' Image
Oso
Enthusiast
Enthusiast
Posts: 595
Joined: Wed Jul 20, 2022 10:09 am

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

Post by Oso »

chikega wrote: Sun Jul 31, 2022 5:43 pm Not that this will make a big difference in ones code, but it's something I found by accident while playing with PB string types. It is not documented, but:

Code: Select all

Define$
appears to function the same as

Code: Select all

Define.s 
Thanks for posting this, I'm beginning to get the impression with PB that there are one or two hidden concepts that aren't always documented. I think it emphasises the importance of sharing ideas on this great forum here!
Olli
Addict
Addict
Posts: 1240
Joined: Wed May 27, 2020 12:26 pm

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

Post by Olli »

I can suggest you this :

Code: Select all

Structure main

 a.i
 b.i
 c.i
 
 name.s
 
EndStructure


Procedure displayC(*this.main)
 With *this
 
  Debug Str(\c)
 
 EndWith
EndProcedure




Procedure main()
 With *this
 
  Define *this.main = AllocateMemory(SizeOf(main) )
  
  \name = "Friday "
   \a = 5
   \b = 3
   \c = \a + \b
   displayC(*this)
 
 EndWith
EndProcedure



main()
There are lots of convenient in writing through this syntax. Define is, here, not used a lot, and is replaced by a personnal structure. This is compatible with several suggests in this subject. Knowing :
- try to use one Define directive per variable
- not to blend several types on the same line.
- try to keep the type on the end of the source line : this allows you to migrate easily variables definings to structure defining.
Post Reply