How To

Passing on so many variables from function to function that it looks like a mess

3

SP4CEBAR 2023-09-13 19:50

If a function takes too many variables then it looks like a big mess:
'CALL PRINT_MIDI_PLAY(J,V, MIDI_COMMAND_TYPE$, MIDI_COMMAND_NAME$,MIDI_COMMAND_PROP$, O$,KEY, VOI,PITCH,VELOCITY,TRACKN,TRACKM,IS_RUNNING_STATUS,IS_TRACK_END )

But sometimes that's what you need to do, This function passes most of those variables on to its inner functions, which pass those on to their inner functions, all these functions combined need quite a lot of variables.

To prevent this I could use more global variables, which could make it unclear which variables are global and which aren't, or I could make these functions pass on arrays that need proper labeling to not confuse the variables that go in and out of them and either a blob of code to unpack it into variables or the loss of variable names by using indexes, or the introduction of global constants to add names to the indexes

Which approach do you prefer?

While writing this, I think I got a decent solution: global variables with a clear prefix:
Like G_VARIABLE to signify that this variable is global


Timo 2023-09-13 22:29

In other languages you would pass structs or something similar to the function. Classic BASIC was simply not designed to do complex stuff.


was8bit 2023-09-14 02:38

I use globals when most of the code uses them, including SUBS...

But I also like to pass variables to SUBS, and even use SUBS to return values or edit variables outside the sub... this feature is very useful ;)


was8bit 2023-09-14 02:41

For your big call, I would shorten the variables...

CALL PRINT_MIDI_PLAY(J,V, MT$, MN$,MC$,O$,KEY,VOI,PIT,VEL,TRN,TRM,ISRUN,ISEND )


was8bit 2023-09-14 02:43

I have attempted ONCE to condense all variables into one array... TOO hard to remember which index# was what variable, made coding too complex to sort out if you have bugs to root out...


SP4CEBAR 2023-09-14 06:48

@was8bit I stopped over-using abbreviations in variable names because I will forget most of them


Timo 2023-09-14 08:17

I agree, variable names should be understandable, otherwise it's really hard to come back to old code. I noticed with my tools :/
As an exception I use abbreviations for temporary variables, like "I" in small FOR loops etc.


McPepic 2023-09-14 13:48

Whenever I use abbreviations or arrays to store a lot of variables, I usually leave comments on the previous line to specify what each variable is. This also allows you to explain what the variable does so you don’t just have to remember everything.


SP4CEBAR 2023-09-14 14:22 (Edited)

@mcpepic If you come across a random variable that is used far away from the code line where it was initialized and explained then you'd have to go look that comment up, and there's a chance that that comment is outdated


SP4CEBAR 2023-09-16 07:20

I just found out that renaming over-abbreviated one-character variables within a big program is a huge pain


Log in to reply.