What is the best method to use and share variables between functions in non object-oriented program languages?
Let’s say that I use 10 parameters from DB, ID and 9 other values linked to it. I need to work with all 10 parameters in many functions. I can do it next ways:
1. call functions only with using ID and in every function get the other parameters from DB.
Advantage: local variables are clear visible, there is only one input parameter to function
Disadvantage: it’s slow and there are the same rows for getting parameters in every function, which makes function longer and not so clear
2. call functions with all 10 parameters
Advantage: working with local variables, clear function code
Disadvantage: many input parameters, what is not nice
3. getting parameters as global variables once and using them everywhere
Advantage – clearer code, shorter functions, faster processing
Disadvantage – global variables – loosing control of them, possibility of unwanted overwriting (Especially when some functions should change their values)
Maybe there is some another way how to implement this and make program cleaner and more effective.
Can you say which way is the best for solving this issue?
7
In short: Most languages have standards or ways to address this exact problem and will depend on the language, in C – use a struct; I don’t know Ash but it might have some sort of data structure or way that you can group data together and use that instead.
In terms of the methods you have outlined:
Call functions only with using ID and in every function get the other parameters from DB.
Your disadvantage is pretty much spot on here, you shouldn’t be calling the database multiple times throughout your code execution as it is slow and wasteful, get the data once and reuse it where you can.
Call functions with all 10 parameters
In the event that the particular language has no possible way of grouping data together to let you pass that around, then this is probably the best option. It saves you from using globals (especially mutable ones) that can potentially be ruined by other processes and it makes it clear where the data is coming from. Although there are many input parameters, if your language simply doesn’t support data grouping then this is probably the way to go.
It is also possible to write your code in such a way that each function might not need all 10 variables, then you can just pass the ones that you need to the function and progress through your code that way. Then, the main function only knows about all 10 and deals with the end result – and you delegate particular pieces of business logic to smaller, more manageable methods.
Getting parameters as global variables once and using them everywhere
As you’ve stated in your disadvantages – this can quickly become out of control and if they’re mutable then you don’t know if someone else has changed the value or state of your variable and you can have unexpected results in your application. Using global variables is almost always a bad idea (there are some exceptions).
Although globals might sound like an easy and quick way to achieve something now, as you add more features and more functions add to your code base and your code gets bigger. It becomes harder to add features because you have to start to remember what each global is doing and what is modifying it, what state it is currently in and if your new feature/function can modify/use it at a particular point in time.
2