When returning multiple values, how would you approach it?

General Help regarding HMG, Compilation, Linking, Samples

Moderator: Rathinagiri

HGAutomator
Posts: 202
Joined: Thu Jul 16, 2020 5:42 pm
DBs Used: DBF

When returning multiple values, how would you approach it?

Post by HGAutomator »

Hi,

When a function can't conveniently broken up into multiple small functions, so that passing several variables by reference would be appropriate, how would you handle it?

The choice would be passing by reference in one function ; or returning an array of the relevant values in that function.

I'm looking for 'How would you approach it?' to consider & reflection, rather than absolute advice. How would other developers deal with this type of dilemma?

I lean on the side of returning an array.
User avatar
mol
Posts: 3825
Joined: Thu Sep 11, 2008 5:31 am
Location: Myszków, Poland
Contact:

Re: When returning multiple values, how would you approach it?

Post by mol »

Personally I don't like passing variables by reference.
You must make a decision by yourself.
HGAutomator
Posts: 202
Joined: Thu Jul 16, 2020 5:42 pm
DBs Used: DBF

Re: When returning multiple values, how would you approach it?

Post by HGAutomator »

Yep, I know mol. I just wanted to hear what other developers are doing about it.
mol wrote: Fri Nov 04, 2022 7:53 pm Personally I don't like passing variables by reference.
You must make a decision by yourself.
edk
Posts: 999
Joined: Thu Oct 16, 2014 11:35 am
Location: Poland

Re: When returning multiple values, how would you approach it?

Post by edk »

The reference is Ok if the number of arguments and return values ​​is constant and not too large. When the number of returned arguments is large and/or is inconsistent, I personally use json/hash as in web solutions.
HGAutomator
Posts: 202
Joined: Thu Jul 16, 2020 5:42 pm
DBs Used: DBF

Re: When returning multiple values, how would you approach it?

Post by HGAutomator »

A Hash makes sense. I forgot that Harbour has a Json contrib library.

Will think about that one, thanks edk.


The number of parameters are in the midrange, in this instance 5 of them.:

Code: Select all

CASE LastKeyPressed_n == K_RIGHT
	IF CurrentlySelectedMenuItemNumber_n < MenuItems_n
		GetMenuParametersAfterMovingRight1( Menu_a, MenuItems_n, @CurrentlySelectedMenuItemNumber_n, @FirstVisibleItemNumber_n, RowPosition_n, @ColumnPosition_n, Item_Lengths_a, @LastVisibleMenuItemNumber_n )
    	DisplayVisibleMenuItems( Menu_a, RowPosition_n, LastVisibleMenuItemNumber_n, FirstVisibleItemNumber_n )
    	DisplayReverseVideoItem( Menu_a, RowPosition_n, ColumnPosition_n, CurrentlySelectedMenuItemNumber_n )
		DisplayMessage( Messages_a, RowPosition_n+1, StartingColumnPosition_n+1, CurrentlySelectedMenuItemNumber_n )
	ELSE
		// We're at the very last menu item on the last screen, so scroll around to the very first menu on the very first screen.
  		JumpToFirstItemFirstScreen( Menu_a, MenuItems_n, RowPosition_n, @ColumnPosition_n, @CurrentlySelectedMenuItemNumber_n, @FirstVisibleItemNumber_n, @LastVisibleMenuItemNumber_n, FirstScreenLastVisibleMenuItemNumber_n )
	ENDIF
edk wrote: Fri Nov 04, 2022 9:59 pm The reference is Ok if the number of arguments and return values ​​is constant and not too large. When the number of returned arguments is large and/or is inconsistent, I personally use json/hash as in web solutions.
User avatar
serge_girard
Posts: 3420
Joined: Sun Nov 25, 2012 2:44 pm
DBs Used: 1 MySQL - MariaDB
2 DBF
Location: Belgium
Contact:

Re: When returning multiple values, how would you approach it?

Post by serge_girard »

I prefer returning an array !

Serge
There's nothing you can do that can't be done...
User avatar
AUGE_OHR
Posts: 2117
Joined: Sun Aug 25, 2019 3:12 pm
DBs Used: DBF, PostgreSQL, MySQL, SQLite
Location: Hamburg, Germany

Re: When returning multiple values, how would you approach it?

Post by AUGE_OHR »

hi,

you can make all Variable PUBLIC ... no Parameter need to pass / return :roll:
i do NOT recommend while have "better" Solution

---

the Benefit of OOP are Member (DATA) Var which you can "read"/"Write" when need and they are "visible" in hole CLASS

HMG have "semi-OOP" ... hm ... :idea:

---

i use this Style to organize n-DIM (Instance) of CLASS TExplorer() using TAB-Control

Code: Select all

#xtranslate Menu_a         			=> Stack_Left\[nDimLeft, 1]
#xtranslate RowPosition    			=> Stack_Left\[nDimLeft, 2]
#xtranslate LastVisibleMenuItemNumber_n    	=> Stack_Left\[nDimLeft, 3]
#xtranslate FirstVisibleItemNumber_n            => Stack_Left\[nDimLeft, 4]
...
#xtranslate xReturnValue                        => Stack_Left\[nDimLeft,99]

Code: Select all

STATIC Stack_Left  := {}
STATIC nDimLeft    := 1

PROCEDURE MAIN
   AADD( Stack_Left, ARRAY( 99 ) )
all "Variable" are in 2-DIM Array
as Array is STATIC it is "visible" in hole PRG like HMG "semi-OOP"

when use a one(!) PUBLIC as Array put all #xTranslate into *.CH and include it in *.PRG where you need it

! Note : you can not use #xtranslate again as LOCAL

as you can see i have add xReturnValue so you can have "input" and "output" in same Array
have fun
Jimmy
User avatar
mol
Posts: 3825
Joined: Thu Sep 11, 2008 5:31 am
Location: Myszków, Poland
Contact:

Re: When returning multiple values, how would you approach it?

Post by mol »

edk wrote: Fri Nov 04, 2022 9:59 pm The reference is Ok if the number of arguments and return values ​​is constant and not too large. When the number of returned arguments is large and/or is inconsistent, I personally use json/hash as in web solutions.
I often use hash in my new programs/modules. It's very comfortable
HGAutomator
Posts: 202
Joined: Thu Jul 16, 2020 5:42 pm
DBs Used: DBF

Re: When returning multiple values, how would you approach it?

Post by HGAutomator »

Yep, that's an interesting approach. A STATIC array would definitely do the trick.

AUGE_OHR wrote: Sat Nov 05, 2022 8:06 am hi,

you can make all Variable PUBLIC ... no Parameter need to pass / return :roll:
i do NOT recommend while have "better" Solution

---

the Benefit of OOP are Member (DATA) Var which you can "read"/"Write" when need and they are "visible" in hole CLASS

HMG have "semi-OOP" ... hm ... :idea:

---

i use this Style to organize n-DIM (Instance) of CLASS TExplorer() using TAB-Control

Code: Select all

#xtranslate Menu_a         			=> Stack_Left\[nDimLeft, 1]
#xtranslate RowPosition    			=> Stack_Left\[nDimLeft, 2]
#xtranslate LastVisibleMenuItemNumber_n    	=> Stack_Left\[nDimLeft, 3]
#xtranslate FirstVisibleItemNumber_n            => Stack_Left\[nDimLeft, 4]
...
#xtranslate xReturnValue                        => Stack_Left\[nDimLeft,99]

Code: Select all

STATIC Stack_Left  := {}
STATIC nDimLeft    := 1

PROCEDURE MAIN
   AADD( Stack_Left, ARRAY( 99 ) )
all "Variable" are in 2-DIM Array
as Array is STATIC it is "visible" in hole PRG like HMG "semi-OOP"

when use a one(!) PUBLIC as Array put all #xTranslate into *.CH and include it in *.PRG where you need it

! Note : you can not use #xtranslate again as LOCAL

as you can see i have add xReturnValue so you can have "input" and "output" in same Array
franco
Posts: 921
Joined: Sat Nov 02, 2013 5:42 am
DBs Used: DBF
Location: Canada

Re: When returning multiple values, how would you approach it?

Post by franco »

I use private variables in calling program. This way they are available where ever I go from calling program. When calling program
is closed the variables are closed.
***************** ( If these variables are NOT closed with program could someone let me know.)
All The Best,
Franco
Canada
Post Reply