我真的需要访问器函数来从另一个文件访问全局变量吗? [英] Do i really needed accessor functions to access global variable from another file?

查看:107
本文介绍了我真的需要访问器函数来从另一个文件访问全局变量吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的代码(游戏引擎代码)中,有多个源(.c)文件可维护游戏的状态,例如

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

为了保持状态,使用了一个全局变量gameStatus,该全局变量使用extern关键字在多个源文件之间共享.现在我已经读到全局变量不好用,它允许外部模块对其进行更改,并且随着使用全局变量的组件数量的增加,交互的复杂性也会增加.

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.

因此,我使用static关键字将变量的作用域限制为一个文件,并在同一文件中添加了诸如get or set APIs之类的访问器方法.因此,其他文件仅使用访问器API访问该变量.

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.

我删除了很好的全局变量,但是现在使用该全局变量的所有其他文件都必须调用访问器API,这似乎增加了函数调用的开销,

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,

所以现在我很困惑哪个更好?关于如何有效地在不同源文件之间共享数据的任何C标准?

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

推荐答案

全局变量是不良实践"的事实完全基于观点,并且100%取决于上下文.在不查看代码的情况下,无法说出是否要应用这种不好的做法".全局变量本身不是错误的习惯,它们以错误的方式使用它们.全局变量通常是C中必需的.以C标准库为例:errno是一个全局变量,基本上在库代码和用户代码中的任何地方都使用它来检查错误.那是不好的做法吗?他们能定义一个函数get_errno()吗(老实说,他们实际上只是将其隐藏了……但这是出于复杂的并发原因)?我让你决定.

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.

在您的特定情况下,将全局可见变量更改为static,然后创建两个仅用于获取和设置其值的函数是完全不必要的.代码的任何部分仍然可以修改变量,但是现在这样做很烦人,而且如果未正确优化,也可能导致代码变慢.总之,通过创建这些函数,您只需剥离了static限定符的变量.

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.

这篇关于我真的需要访问器函数来从另一个文件访问全局变量吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆