HASH vs single variables

HASH vs single variables

Postby Otto » Tue Jan 17, 2023 7:48 am

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



Image

Image
********************************************************************
mod harbour - Vamos a la conquista de la Web
modharbour.org
https://www.facebook.com/groups/modharbour.club
********************************************************************
User avatar
Otto
 
Posts: 6332
Joined: Fri Oct 07, 2005 7:07 pm

Re: HASH vs single variables

Postby Marc Venken » Tue Jan 17, 2023 7:58 am

I also think like you do. Hash before many variables.
Marc Venken
Using: FWH 23.04 with Harbour
User avatar
Marc Venken
 
Posts: 1434
Joined: Tue Jun 14, 2016 7:51 am
Location: Belgium

Re: HASH vs single variables

Postby AntoninoP » Tue Jan 17, 2023 12:52 pm

good.. next step use classes
AntoninoP
 
Posts: 375
Joined: Tue Feb 10, 2015 9:48 am
Location: Albenga, Italy

Re: HASH vs single variables

Postby paquitohm » Tue Jan 17, 2023 1:10 pm

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
paquitohm
 
Posts: 266
Joined: Fri Jan 14, 2022 8:37 am

Re: HASH vs single variables

Postby Otto » Tue Jan 17, 2023 1:20 pm

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
********************************************************************
mod harbour - Vamos a la conquista de la Web
modharbour.org
https://www.facebook.com/groups/modharbour.club
********************************************************************
User avatar
Otto
 
Posts: 6332
Joined: Fri Oct 07, 2005 7:07 pm

Re: HASH vs single variables

Postby Otto » Tue Jan 17, 2023 1:31 pm

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
********************************************************************
mod harbour - Vamos a la conquista de la Web
modharbour.org
https://www.facebook.com/groups/modharbour.club
********************************************************************
User avatar
Otto
 
Posts: 6332
Joined: Fri Oct 07, 2005 7:07 pm

Re: HASH vs single variables

Postby VictorCasajuana » Tue Jan 17, 2023 4:52 pm

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
--------
¿ Y porque no ?
¿ And why not ?
User avatar
VictorCasajuana
 
Posts: 265
Joined: Wed Mar 28, 2018 4:38 pm
Location: Vinaròs

Re: HASH vs single variables

Postby Otto » Tue Jan 17, 2023 9:50 pm

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
********************************************************************
mod harbour - Vamos a la conquista de la Web
modharbour.org
https://www.facebook.com/groups/modharbour.club
********************************************************************
User avatar
Otto
 
Posts: 6332
Joined: Fri Oct 07, 2005 7:07 pm

Re: HASH vs single variables

Postby Otto » Wed Feb 22, 2023 12:46 am

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

Image

Image

Image

Image
********************************************************************
mod harbour - Vamos a la conquista de la Web
modharbour.org
https://www.facebook.com/groups/modharbour.club
********************************************************************
User avatar
Otto
 
Posts: 6332
Joined: Fri Oct 07, 2005 7:07 pm

Re: HASH vs single variables

Postby Otto » Wed Feb 22, 2023 8:43 pm

Dear friends,
Now we have done some more styling.
I think it looks more up-to-date.
Best regards,
Otto

Image

Image

Image
********************************************************************
mod harbour - Vamos a la conquista de la Web
modharbour.org
https://www.facebook.com/groups/modharbour.club
********************************************************************
User avatar
Otto
 
Posts: 6332
Joined: Fri Oct 07, 2005 7:07 pm

Re: HASH vs single variables

Postby Otto » Wed Jun 07, 2023 6:39 am

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
********************************************************************
mod harbour - Vamos a la conquista de la Web
modharbour.org
https://www.facebook.com/groups/modharbour.club
********************************************************************
User avatar
Otto
 
Posts: 6332
Joined: Fri Oct 07, 2005 7:07 pm

Re: HASH vs single variables

Postby Marc Venken » Wed Jun 07, 2023 8:14 am

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 )))
Marc Venken
Using: FWH 23.04 with Harbour
User avatar
Marc Venken
 
Posts: 1434
Joined: Tue Jun 14, 2016 7:51 am
Location: Belgium

Re: HASH vs single variables

Postby Otto » Wed Jun 07, 2023 10:43 am

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
********************************************************************
mod harbour - Vamos a la conquista de la Web
modharbour.org
https://www.facebook.com/groups/modharbour.club
********************************************************************
User avatar
Otto
 
Posts: 6332
Joined: Fri Oct 07, 2005 7:07 pm

Re: HASH vs single variables

Postby Marc Venken » Wed Jun 07, 2023 11:22 am

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



 



Image
Marc Venken
Using: FWH 23.04 with Harbour
User avatar
Marc Venken
 
Posts: 1434
Joined: Tue Jun 14, 2016 7:51 am
Location: Belgium

Re: HASH vs single variables

Postby Otto » Wed Jun 07, 2023 2:42 pm

Marc,
Do I understand the code correctly?

The `xbrowse` you're showing is from the DBF file, not the hash, right?

Best regards,
Otto
********************************************************************
mod harbour - Vamos a la conquista de la Web
modharbour.org
https://www.facebook.com/groups/modharbour.club
********************************************************************
User avatar
Otto
 
Posts: 6332
Joined: Fri Oct 07, 2005 7:07 pm

Next

Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: Google [Bot] and 84 guests