全局变量在Java程序中为什么不被推荐使用?

huangapple go评论84阅读模式
英文:

Why are global variables discouraged in a Java program?

问题

我是新手程序员,对于为什么有些人坚持认为全局变量是不好的感到相当困惑。我目前正在编写一个包含多个类和大量检查以及重写存储在变量中的数据的程序。我发现全局变量非常有帮助,因为我不需要为每个局部或可能的类变量创建getter和setter。通过使用全局变量作为不同类之间的桥梁,我发现在这些类之间交换信息非常容易。

但再次说一遍,我对编程还相当陌生,如果有人能解释为什么这样做不可取,我将不胜感激。

编辑:真是抱歉!我不知道全局变量和公共类变量完全不同,显然我错误地将公共字段误认为是全局变量。你永远不知道一个初学者程序员会犯多么傻的错误,哈哈。谢谢大家提醒我。

英文:

I'm new to programming and I'm quite confused why some people are insisting that global variables are bad. I'm currently writing a program with several classes and a lot of checking and rewriting data stored in variables. I found global variables extremely helpful since I don't need to create a getter and setter for each of the local or possibly class variables. By using global variables as bridges between different classes, I found it surprisingly easy to exchange information between those classes.

But again, I'm quite new to programming and I would be much appreciated if someone could explain to me why this is not advisable to do.

Edit: My apologies! I didn't know that global variables and public class variables are completely different and apparently I misidentified public fields as global variables. You never know how silly of a mistake a beginner programmer can make Lol. Thanks everyone for bringing this to my attention.

答案1

得分: 2

问题在于这种做法不可扩展。随着项目变得更加复杂或涉及更多合作者,全局变量会引发混乱和错误,因为难以跟踪可能的副作用变得越来越困难。 "左手不知道右手在做什么。" 或者至少,没有经过大量测试和研究就不知道。这增加了维护的成本。

您可以查阅一本关于最佳实践的书,例如罗伯特·C·马丁的经典著作《Clean Code》。

在安全性方面也存在类似的情况,我们有授予最低权限的概念。

英文:

The problem is that this practice doesn't scale. As projects get more complex or involve more collaborators, globals invite confusion and bugs as it becomes ever harder to keep track of possible side effects. "The left hand doesn't know what the right hand is doing." Or at least, not without considerable testing and research--adding to the cost of maintenance.

You might peruse a book on best practices, such as "Clean Code" by Robert C. Martin, a classic.

A similar situation arises in security, where we have the concept of granting least privileges.

答案2

得分: 1

理解面向对象编程需要一些时间。全局变量的主要问题与获取器和设置器的问题相同,即将任何内容公开或扩展超类。你对每个涉及全局变量、设置器或获取器的代码都做出了承诺,即你不会更改已公开的实现。现在没问题。两年后,当有 100,000 行代码依赖于该变量,并且现在你意识到你的 "int x, y" 实现细节实际上应该是 "int x, y, z" 时,会发生什么?想象一下在周末追踪和修复数千个对这段代码的引用,感觉如何?现在想象一下这个问题乘以你无需向代码用户公开的所有状态的实例...

英文:

It’s going to take a while to really understand object-oriented programming. The big issue with global variables is the same issue with getters and setters, making anything public or extending a superclass. You’re making a commitment to every piece of code that touches a global variable, or a setter or a getter that /you will not change the implementation/ that you’ve exposed. Right now that’s fine. Two years from now, what happens when 100,000 lines depend on that variable and now you realize that your “int x, y” implementation detail really should be “int x, y, z”? Feel like spending the weekend tracking down and fixing thousands of references to this code? Now imagine this problem multiplied by all the instances of state you’ve needlessly exposed to users of your code...

huangapple
  • 本文由 发表于 2020年8月7日 07:35:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/63293166.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定