在C/ c++中,全局变量像我的教授认为的那样糟糕吗?
当前回答
全局变量的问题是,由于每个函数都可以访问这些变量,因此越来越难以确定哪些函数实际读写这些变量。
为了理解应用程序是如何工作的,您几乎必须考虑修改全局状态的每个函数。这是可以做到的,但随着应用程序的增长,它将变得越来越困难,几乎不可能(或至少完全浪费时间)。
如果不依赖全局变量,则可以根据需要在不同函数之间传递状态。这样,您就有更好的机会理解每个函数的功能,因为您不需要考虑全局状态。
其他回答
全局变量的问题是,由于每个函数都可以访问这些变量,因此越来越难以确定哪些函数实际读写这些变量。
为了理解应用程序是如何工作的,您几乎必须考虑修改全局状态的每个函数。这是可以做到的,但随着应用程序的增长,它将变得越来越困难,几乎不可能(或至少完全浪费时间)。
如果不依赖全局变量,则可以根据需要在不同函数之间传递状态。这样,您就有更好的机会理解每个函数的功能,因为您不需要考虑全局状态。
不,他们一点都不坏。你需要查看编译器生成的(机器)代码来做出判断,有时使用局部变量比使用全局变量要糟糕得多。还要注意,在局部变量上添加“static”基本上是使其成为全局变量(并产生真正的全局变量可以解决的其他丑陋问题)。“局部全局变量”尤其糟糕。
全局变量还可以让你完全控制内存的使用,这在局部变量中要困难得多。如今,这只在内存相当有限的嵌入式环境中才重要。在假定嵌入式环境与其他环境相同并假定编程规则全面相同之前,需要了解一些事情。
It is good that you question the rules being taught, most of them are not for the reasons you are being told. The most important lesson though is not that this is a rule to carry with you forever, but this is a rule required to honor in order to pass this class and move forward. In life you will find that for company XYZ you will have other programming rules that you in the end will have to honor in order to keep getting a paycheck. In both situations you can argue the rule, but I think you will have far better luck at a job than at school. You are just another of many students, your seat will be replaced soon, the professors wont, at a job you are one of a small team of players that have to see this product to the end and in that environment the rules developed are for the benefit of the team members as well as the product and the company, so if everyone is like minded or if for the particular product there is good engineering reason to violate something you learned in college or some book on generic programming, then sell your idea to the team and write it down as a valid if not the preferred method. Everything is fair game in the real world.
如果你遵循学校或书本上教给你的所有编程规则,那么你的编程生涯将非常有限。你很可能会生存下来,并拥有一个富有成效的事业,但你所能获得的环境的广度和宽度将是极其有限的。如果你知道规则是如何存在的,为什么会存在,并且能够捍卫它,那很好,如果你的理由只是“因为我的老师这么说”,那就不太好了。
请注意,这样的话题经常在工作场所被争论,并且将继续存在,随着编译器和处理器(以及语言)的发展,这些类型的规则也在发展,如果不捍卫你的立场,可能会被持不同意见的人教训一课,你就不会前进。
与此同时,那个说话声音最大或拿着最大棍子的人说什么,你就做什么(直到你是那个喊得最大、拿着最大棍子的人)。
重要的是要记住总体目标:清晰
“无全局变量”规则的存在是因为大多数时候,全局变量使代码的含义不太清楚。
然而,像许多规则一样,人们记住的是规则,而不是规则的意图。
我曾经见过一些程序,为了避免全局变量的危害,通过传递大量参数,使代码的大小增加了一倍。最后,使用全局变量将使程序对阅读它的人更清楚。由于盲目地遵守规则的文字,原来的程序员未能实现规则的意图。
所以,是的,全局变量通常不好。但是如果你觉得在最后,程序员的意图通过使用全局变量变得更加清晰,那么就继续吧。但是,请记住,当您强迫某人访问第二段代码(全局代码)以理解第一部分是如何工作的时,清晰度会自动下降。
问题不在于它们有多坏,而在于它们有多危险。他们有自己的优点和缺点,在某些情况下,他们要么是最有效的,要么是完成特定任务的唯一方法。然而,它们很容易被误用,即使你采取措施总是正确使用它们。
一些优点:
可以从任何函数访问。 可以从多个线程访问。 在项目结束前都不会超出范围。
一些缺点:
可以从任何函数访问,而不需要显式地作为参数拖入和/或记录。 不是线程安全的。 污染全局名称空间并可能导致名称冲突,除非采取措施防止这种情况。
请注意,如果你愿意的话,我列出的前两个优点和前两个缺点是完全相同的事情,只是措辞不同。这是因为全局变量的特性确实是有用的,但使它们有用的特性正是它们所有问题的根源。
一些问题的潜在解决方案:
Consider whether they're actually the best or most efficient solution for the problem. If there are any better solutions, use that instead. Put them in a namespace [C++] or singleton struct [C, C++] with a unique name (a good example would be Globals or GlobalVars), or use a standardised naming convention for global variables (such as global_[name] or g_module_varNameStyle (as mentioned by underscore_d in the comments)). This will both document their use (you can find code that uses global variables by searching for the namespace/struct name), and minimise the impact on the global namespace. For any function that accesses global variables, explicitly document which variables it reads and which it writes. This will make troubleshooting easier. Put them in their own source file and declare them extern in the associated header, so their use can be limited to compilation units that need to access them. If your code relies on a lot of global variables, but each compilation unit only needs access to a handful of them, you could consider sorting them into multiple source files, so it's easier to limit each file's access to global variables. Set up a mechanism to lock and unlock them, and/or design your code so that as few functions as possible need to actually modify global variables. Reading them is a lot safer than writing them, although thread races may still cause problems in multithreaded programs. Basically, minimise access to them, and maximise name uniqueness. You want to avoid name collisions and have as few functions as possible that can potentially modify any given variable.
它们是好是坏取决于你如何使用它们。大多数人倾向于不好地使用它们,因此对它们普遍持谨慎态度。如果使用得当,它们可以成为一大福音;然而,如果使用不当,它们可能会在你最意想不到的时候回来咬你一口。
从一个好的角度来看,它们本身并不坏,但它们导致了糟糕的设计,并且会成倍地增加糟糕设计的影响。
即使你不打算使用它们,知道如何安全使用它们并选择不使用,也比不使用它们因为你不知道如何安全使用它们要好。如果您发现自己处于需要维护依赖于全局变量的预先存在的代码的情况下,如果您不知道如何正确使用它们,那么您可能会遇到困难。
全局变量的使用实际上取决于需求。它的优点是减少了重复传递值的开销。
但是你的教授是对的,因为它会引起安全问题,所以应该尽可能避免使用全局变量。全局变量还会产生一些问题,有时难以调试。
例如:-
变量值在运行时被修改的情况。此时,很难确定是哪部分代码在什么条件下修改了它。