2

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
  • 1
    Use a struct :-)
    – Andrea
    Commented Oct 17, 2012 at 8:34
  • Struct is a good point - for C... But what about languages where there are no structs? What about ash? (I have faced this problem there as well)
    – srnka
    Commented Oct 17, 2012 at 8:42
  • 1
    I do not know the Ash language. In any case, most languages allow you to group stuff together in one way or another, either using objects, records, structs, hashes and whatnot. A programming language that does not allow to talk of a bunch of data as a single logical entity should have very compelling features to persuade me to give it a try.
    – Andrea
    Commented Oct 17, 2012 at 8:46
  • 1
    When you work with a language that doesn't support structs, chances are that using global variables is the best solution. Such simple languages might not support proper scopes anyway, so why bother.
    – user281377
    Commented Oct 17, 2012 at 9:35
  • 1
    The way I did it way back in my early MASM days was to use pointers. I'd pass in the pointer to a memory location. Of course, this isn't a particularly safe way to do things but it may be an option for you. A Google search revealed some ways to create pseudo-pointers in BASH, maybe that would help.
    – jfrankcarr
    Commented Oct 17, 2012 at 13:26

1 Answer 1

4

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
  • About point 1; some premature optimization here. I'd say try passing the Id around, you can always refactor later.
    – dvdvorle
    Commented Oct 17, 2012 at 9:10
  • 2
    It is not premature. I am also against over-optimizing, but keeping querying the DB for nothing is just silly. Moreover, you actually have to query your db in each function, leading to repetition
    – Andrea
    Commented Oct 17, 2012 at 9:13

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.