Page 1 of 2
HASH vs single variables
Posted:
Tue Jan 17, 2023 7:48 am
by Otto
Hello friends,
I am now trying an approach to use a HASH instead of individual variables.
So far, I have named the HASH hApp.
But I think the source code is easier to read if you just use h[] as the name.
You can then see the HashKey better.
First, I define a hash.
Then, when I need a variable somewhere, I insert a new key to be able to access it throughout the program.
An xBrowse(h) shows all keys - variables - used in the program with their current value.
This function shows all the "variables" used, the keys and values.
Best regards,
Otto
Re: HASH vs single variables
Posted:
Tue Jan 17, 2023 7:58 am
by Marc Venken
I also think like you do. Hash before many variables.
Re: HASH vs single variables
Posted:
Tue Jan 17, 2023 12:52 pm
by AntoninoP
good.. next step use classes
Re: HASH vs single variables
Posted:
Tue Jan 17, 2023 1:10 pm
by paquitohm
Hola,
Usar hashes en vez de variables TIENE UN PROBLEMA.
El compilador es incapaz de detectar si te has equivocado al teclear y entonces crea un item nuevo.
Con hash:
- Code: Select all Expand view
Local h:= {=>}
h["CustomarCode"]:= "0001" // No error
Con variable:
- Code: Select all Expand view
Local cCustomerCode
cCustomarCode:= "0001" // Error in compilation time
Con hash, sistema mejorado:
- Code: Select all Expand view
Local h:= {"cCustomerCode"=>NIL}
hb_HAutoAdd(h, .f.) // Swith off auto declare variables
h["CustomarCode"]:= "0001" // Run-time error
Regards
Re: HASH vs single variables
Posted:
Tue Jan 17, 2023 1:20 pm
by Otto
Hello Antonino,
Can you maybe list some pros and cons here.
What advantage would a class have here?
I use classes for e.g. program setup, etc.. with my FIVEWIN programming.
But it seems to me that with mod harbour the OOP is an overhead that brings nothing. Therefore a HASH seems favorable to me here.
Probably this is because at the moment the programs are still very simple.
Best regards,
Otto
Re: HASH vs single variables
Posted:
Tue Jan 17, 2023 1:31 pm
by Otto
Paquitohm, you are right. You have to be more precise here.
Having used this method primarily with mod harbour, I would like to highlight one benefit that I see.
I pass the HASH to a Javascript object and then have practically the same variable names in the Harbor code and in the HTML/JS code.
Best regards,
Otto
Re: HASH vs single variables
Posted:
Tue Jan 17, 2023 4:52 pm
by VictorCasajuana
Otto wrote:Hello Antonino,
Can you maybe list some pros and cons here.
What advantage would a class have here?
I use classes for e.g. program setup, etc.. with my FIVEWIN programming.
But it seems to me that with mod harbour the OOP is an overhead that brings nothing. Therefore a HASH seems favorable to me here.
Probably this is because at the moment the programs are still very simple.
Best regards,
Otto
OOP always contributes.
You may have a small program now, but if you do it with OOP (classes and objects) when it grows you will have a good foundation.
When necessary, you can give a logic to the assignment of a value ( setters and getters ). Another advantage is with the editor, if you use classes, it helps you when writing the code since it autocompletes the properties
Re: HASH vs single variables
Posted:
Tue Jan 17, 2023 9:50 pm
by Otto
Victor, assume that I know both types of programming.
Using a hash table instead of classes can have its own advantages as well.
Hash tables are simpler and more lightweight, and can be faster for certain types of operations, such as looking up an item by key. They also don't require the overhead of creating and managing objects, which can be beneficial for smaller programs or programs that need to handle a large number of simple data structures.
I think I will do many more tests and then develop a personal style.
But at this stage, I'm very excited about the possibilities that HASHs offer.
Best regards
Otto
Re: HASH vs single variables
Posted:
Wed Feb 22, 2023 12:46 am
by Otto
Hello friends,
I am in the process of revising WINHOTEL a bit.
On this occasion I now change the use of variables to hashes.
Here is an example of how I make a switch row with very minimal definition.
Only the red framed part is to be defined for each button.
Best regards,
Otto
Re: HASH vs single variables
Posted:
Wed Feb 22, 2023 8:43 pm
by Otto
Dear friends,
Now we have done some more styling.
I think it looks more up-to-date.
Best regards,
Otto
Re: HASH vs single variables
Posted:
Wed Jun 07, 2023 6:39 am
by Otto
Hello friends,
I have been using a static hash variable for some time now to partially replace local variables. It somehow feels safer to me when making changes to old source code.
I don't need to declare the variables. I simply assign a value to the key, and then I have access to it throughout the prg.
Xbrowse(h)
Lists, for example, all the "variables" used with their values.
Best regards,
Otto
Re: HASH vs single variables
Posted:
Wed Jun 07, 2023 8:14 am
by Marc Venken
I also use this technique. Very easy and better overview. I use a hot-key that pops a xbrowse where i can add a variable on the fly for using in my program.
It also pops all var's with there current values and that is happy for info/debug.
and I can have labels (that gives the type and description of content) that afther time I forgot with older var's
Passing, declaring and remember the names are gone )))
Re: HASH vs single variables
Posted:
Wed Jun 07, 2023 10:43 am
by Otto
Marc, very interesting. I will assign a hotkey too. Could you provide an example of having labels that give the type and description of content?
Best regards,
Otto
Re: HASH vs single variables
Posted:
Wed Jun 07, 2023 11:22 am
by Marc Venken
Otto,
Label is more ment as info (maybe you think a popup label of a king)
In the popup I can change/add.
Type = Return Type data
Veld = VarName
Data = content of Var (can me array,...)
DataType = type that the data is made in (ex. Datatype = array, but Type = C as return value for program
info = what's about...to remember afther time )))
trim = Should result be trimmend.
All tips, enhancements are welcome...
the small code (but you have this already) just for forum
- Code: Select all Expand view
setkey( VK_F6,{|| xbrowser("system") FASTEDIT }) // HotKey for HASH
hash_setup() // Setup alle datagegevens in een hash
function hash_setup()
netopen("system")
do while !system->(eof())
cVeld:=alltrim(system->veld)
cData:=alltrim(system->data)
do case
case system->data_type = "A"
if !empty(cData)
h_System["&cVeld"] := strtoArr(cData)
else
h_System["&cVeld"] := {}
endif
case system->data_type = "N"
h_System["&cVeld"] := val(cData)
case system->data_type = "L"
if cData = ".T."
h_System["&cVeld"] := .T.
else
h_System["&cVeld"] := .F.
endif
otherwise
h_System["&cVeld"] := if(system->trim,alltrim(system->data),system->data)
endcase
system->(dbskip())
enddo
close system
return NIL
Re: HASH vs single variables
Posted:
Wed Jun 07, 2023 2:42 pm
by Otto
Marc,
Do I understand the code correctly?
The `xbrowse` you're showing is from the DBF file, not the hash, right?
Best regards,
Otto