Should Global Variables be Used for Constant Data Tables?

In summary, if you are using a global variable and you don't plan on changing it, it is probably fine. However, if you are using a global variable and you plan on changing it, then you should use a resource manager to manage the data.
  • #1
fluffy123
19
0
Hi,

I just want to know, if you're writing a reasonably short program (~500 lines), and you're basically using a global variable as a const (user inputs it once, and you won't ever change it), then is it ok?
 
Technology news on Phys.org
  • #2
As far as I'm concerned, it's OK. As far as your instructor's or employer's coding standards are concerned, it may or may not be OK.
 
  • #3
fluffy123 said:
Hi,

I just want to know, if you're writing a reasonably short program (~500 lines), and you're basically using a global variable as a const (user inputs it once, and you won't ever change it), then is it ok?

If you have some set of global variables, it might be an idea to put them into some kind of global data structure, or at least have them be accessible by a global interface of some sort.

It might seem like overkill, but if you start using a tonne of these variables, and you need some way to differentiate between different versions of the software and have issues with compatibility, then by using different context data structures, you can manage problems that might arise.

I acknowledge that you said your program is small, but in many cases small programs can become larger programs (hence my post).

My suggestion for you is if you ever need a standardized way of managing global objects (i.e. data), then you should use a resource manager that allows you to create and manage data structures global to your process. It is overkill for your problem, but you might find if you ever have to make or deal with complex code repositories, it will help you immensely.
 
  • #4
chiro said:
If you have some set of global variables, it might be an idea to put them into some kind of global data structure, or at least have them be accessible by a global interface of some sort.

It might seem like overkill, but if you start using a tonne of these variables, and you need some way to differentiate between different versions of the software and have issues with compatibility, then by using different context data structures, you can manage problems that might arise.

I acknowledge that you said your program is small, but in many cases small programs can become larger programs (hence my post).

My suggestion for you is if you ever need a standardized way of managing global objects (i.e. data), then you should use a resource manager that allows you to create and manage data structures global to your process. It is overkill for your problem, but you might find if you ever have to make or deal with complex code repositories, it will help you immensely.

Do you suggest something like this?

class Number
{
int x;

public:
Number();
int get_x();
} ;

And call the user to input the value in the constructor? I don't think x could be changed in this case once initialized.

P.S. Keep in mind I don't have much programming experience.
 
  • #5
What exactly is the goal you are trying to achieve? If you want to learn object oriented programming then such a construct may be sensible for the sake of learning to program classes. In this case you will then notice that you merely replaced your global variable of type integer with a clumsy construct and a global variable of type "Number", and can then consider even more abstract constructs like singletons which make the trivial task of having a value present everywhere in your program even more complicated. If you want to write a small program that solves your problem at hand then stick to your global variables, they are perfectly fine unless someone who actually knows your problem/program (and has the power to fire you; see jtbell's comment) tells you otherwise.
 
  • #6
fluffy123 said:
Do you suggest something like this?
I think he meant using a class if you have a significant number of global variables, not just a few global variables. The main advantage of using a function to access a variable is that it allows you to change the variable (within reason) without changing what that function returns. Maybe you change a variable from using english units to metric units, but the function continues to return english units for compatability with the existing code. If it's unlikely that you'll ever change the global variable, there's no need to use a function to access it.

resource manager

A resource manager only makes sense if you're using some common resource like windows user interface stuff, where a resource manager would exist. If you have some custom table(s) of constant data, it's probably best to implement it (them) as a global array(s) of const structures.

global variable
As mentioned above, it depends on the standards of your class or your work environment and/or the particular situation. Personally, I don't like the idea of adding a parameter to access a variable to nearly every function in a program to avoid using a global variable, as it's uneeded overhead, and since the functions may use a different name for what should otherwise be a global variable, it makes it difficult to do a simple text search in a source tree to find out all the places a global variable is accessed.
 
  • #7
Hi rcgldr

The resource manager that I am talking provides an interface to access any global shared data either by its pointer, or by its interface.

I agree with you that if you just have a tonne of constants, then you could just collect them up into a single data structure, but if the number of constants is large, then my view is that you should probably transform your program to take those constants and make them external data in a file which now makes them non-constant and derived from some data file. You could structure the interface so that you could not change the underlying data (which is pretty easy to do), if you really wanted to maintain the 'const
behavior.

The resource manager I am talking about is something that manages all of the global resources in your program and part of that deals with resource address translation, which is simply the act of getting a pointer to the object or its interface given some kind of tag or index (e.g. an integer value, or a string).

To me personally, it doesn't make sense to have any program with a large amount of constant variables. You might as well make the whole thing data driven, and with regards to speed, yes I agree there will be overhead, but unless you need every clock cycle you can take, it becomes less of an issue since hardware is getting a lot better every year.
 
  • #8
chiro said:
The resource manager that I am talking provides an interface to access any global shared data either by its pointer, or by its interface.
I've never used a "generic" resource manager. The only resource manager's I've worked with were used to generate user interface stuff, icons, menus, windows, ... . I wasn't aware that generic resource managers even existed.

chiro said:
... external data file ... To me personally, it doesn't make sense to have any program with a large amount of constant variables.
The situation I'm thinking of is one where you have one or more large tables of data. These could be handled via external files, but then you'd have to include the code to parse the files each time. Also if you needed to modify a table, it's much easier to text edit a source or include file than to hex edit some binary data file.

If the table is often updated, then it would make sense to create a utility or to use some database type software to maintain the table, and to export the table in some program friendly format if the data needs to be handled by non-database software.

chiro said:
With regards to speed, yes I agree there will be overhead.
I was also considering the overhead of coding time; the overhead of what I would consider undeeded extra coding done just to avoid using global variables in situations where it would make more sense to use global variables.
 

Related to Should Global Variables be Used for Constant Data Tables?

1. What are global variables and why are they used?

Global variables are variables that are declared outside of a function and can be accessed by any part of the program. They are used to store values that need to be accessed by multiple functions or throughout the entire program.

2. Are global variables considered bad practice?

It depends on the context. In some cases, global variables can make code more organized and easier to maintain. However, they can also lead to potential bugs and make it difficult to track down where a variable was changed. It is generally recommended to minimize the use of global variables and only use them when necessary.

3. Can global variables be modified by any part of the program?

Yes, global variables can be modified by any part of the program. This is why it is important to carefully consider the use of global variables, as unexpected changes to them can affect the functionality of the program in unintended ways.

4. How can global variables affect the performance of a program?

Global variables can negatively impact the performance of a program, especially if they are used extensively. This is because every function that accesses a global variable needs to check its value, which can slow down the program's execution. Additionally, using global variables can make it difficult for the compiler to optimize the code.

5. Are there any alternatives to using global variables?

Yes, there are alternative ways to store and access data without using global variables. One option is to use local variables within functions and pass them as parameters to other functions that need to access them. Another option is to use data structures such as arrays or objects to store related data. These alternatives can often lead to more organized and efficient code.

Similar threads

  • Programming and Computer Science
Replies
13
Views
1K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
5
Views
534
  • Programming and Computer Science
Replies
4
Views
543
  • Programming and Computer Science
Replies
4
Views
509
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
1
Views
6K
  • Programming and Computer Science
Replies
22
Views
977
Replies
63
Views
3K
  • Programming and Computer Science
Replies
3
Views
388
Back
Top