0
votes

In my code (game engine code) there are multiple source (.c) files which maintain the status of the game, status like

START
CONFIGURE
STOP
END
DEFAULT
RUNNING

for maintaining state, one global variable gameStatus used which shared between multiple source files using extern keyword. now I have read that the global variable is bad to use and it allows the outside module to change it and as the number of components using global variable increases, the complexity of the interactions can also increase.

So I have limited scope that variable to one file using static keyword and added accessor methods like get or set APIs in the same file. So other files only access that variable using accessor APIs.

I have removed the global variable that is good but now every other file which used that global variable have to call accessor APIs which seems to add the overhead of function calls,

so now I am confused which is better? any C standard about how efficiently share the data between different source files?

1
A private variable with a pair of getter and setter is just a complicated public variable (private and public in the general sense of accessibility). Either pass it around or keep it global. If you later run into a concrete problem with it being global, you'll know what to do. - Nelfeal
Although, if you really need to check for the game state in multiple places, you should reconsider your design. It should be enough to have this check somewhere in a high-level location, propabably in your main loop. - Hulk
@Nelfeal what if only getter API is there? means another module can get value only, set value not possible from another file. - user2520119
@user2520119 In that case, sure, that's one way to do it. Another would be to register callbacks to call at each state transition, but that's already quite a bit more complex. Go simple if you can. - Nelfeal

1 Answers

1
votes

The fact that global variables are "bad practice" is entirely opinion based and 100% dependent on the context. It is impossible to say whether you are applying such "bad practice" or not without looking at your code. Global variables are not bad practice per se, using them in the wrong way is. Global variables are often necessary in C. Take as an example the C standard library: errno is a global variable that is used basically everywhere in both library code and user code to check for errors. Is that bad practice? Could they have defined a function get_errno() instead (well to be honest they actually did it's just hidden... but that's for complex concurrency reasons)? I'll let you decide.

In your specific case, changing a globally visible variable to static and then creating two functions only to get and set its value is totally unnecessary. Any part of the code can still modify the variable, but now it's just more annoying to do so, and it could also lead to slower code if not optimized correctly. All in all, by creating those functions you just stripped the variable of the static qualifier.