为什么静态变量被认为是邪恶的? [英] Why are static variables considered evil?

查看:32
本文介绍了为什么静态变量被认为是邪恶的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名 Java 程序员,刚进入企业界.最近我使用 Groovy 和 Java 开发了一个应用程序.在我编写的所有代码中,使用了相当多的静态变量.高级技术人员要求我减少使用的静力学数量.我在谷歌上搜索了大致相同的内容,我发现许多程序员相当反对使用静态变量.

I am a Java programmer who is new to the corporate world. Recently I've developed an application using Groovy and Java. All through the code I wrote used quite a good number of statics. I was asked by the senior technical lot to cut down on the number of statics used. I've googled about the same, and I find that many programmers are fairly against using static variables.

我发现静态变量使用起来更方便.而且我认为它们也很有效(如果我错了,请纠正我),因为如果我必须对类中的函数进行 10,000 次调用,我会很高兴使该方法成为静态方法并使用简单的 Class.methodCall() 在它上面而不是用 10,000 个类的实例来混乱内存,对吗?

I find static variables more convenient to use. And I presume that they are efficient too (please correct me if I am wrong), because if I had to make 10,000 calls to a function within a class, I would be glad to make the method static and use a straightforward Class.methodCall() on it instead of cluttering the memory with 10,000 instances of the class, right?

此外,静态减少了对代码其他部分的相互依赖.他们可以充当完美的国家持有者.除此之外,我发现静态在某些语言中被广泛实现,例如 SmalltalkScala.那么,为什么这种对静态的反对在程序员中普遍存在(尤其是在 Java 世界中)?

Moreover statics reduce the inter-dependencies on the other parts of the code. They can act as perfect state holders. Adding to this I find that statics are widely implemented in some languages like Smalltalk and Scala. So why is this opposition to statics prevalent among programmers (especially in the world of Java)?

PS:如果我对静力学的假设有误,请纠正我.

PS: please do correct me if my assumptions about statics are wrong.

推荐答案

它不是很面向对象:有些人可能认为静力学是邪恶的"的一个原因是它们与 面向对象范式.特别是它违反了数据封装在对象中的原则(可以扩展,信息隐藏等).静态,在您描述使用它们的方式中,本质上是将它们用作全局变量,以避免处理范围等问题.然而,全局变量是过程或命令式编程范式的定义特征之一,而不是好的"面向对象代码的特征.这并不是说程序范式不好,但我的印象是您的主管希望您编写良好的面向对象代码",而您确实想编写良好的程序代码".

Its not very object oriented: One reason statics might be considered "evil" by some people is they are contrary the object-oriented paradigm. In particular, it violates the principle that data is encapsulated in objects (that can be extended, information hiding, etc). Statics, in the way you are describing using them, are essentially to use them as a global variable to avoid dealing with issues like scope. However, global variables is one of the defining characteristics of procedural or imperative programming paradigm, not a characteristic of "good" object oriented code. This is not to say the procedural paradigm is bad, but I get the impression your supervisor expects you to be writing "good object oriented code" and you're really wanting to write "good procedural code".

当您开始使用并不总是很明显的静态时,Java 中有许多问题.例如,如果您的程序有两个副本在同一个 VM 中运行,它们是否会破坏静态变量的值并弄乱彼此的状态?或者当你扩展类时会发生什么,你可以覆盖静态成员吗​​?您的虚拟机是否因为有大量静态数据而内存不足,并且无法为其他需要的实例对象回收该内存?

There are many gotchyas in Java when you start using statics that are not always immediately obvious. For example, if you have two copies of your program running in the same VM, will they shre the static variable's value and mess with the state of each other? Or what happens when you extend the class, can you override the static member? Is your VM running out of memory because you have insane numbers of statics and that memory cannot be reclaimed for other needed instance objects?

对象生命周期:此外,静态的生命周期与程序的整个运行时相匹配.这意味着,即使你使用完你的类,所有这些静态变量的内存也不能被垃圾收集.例如,如果您将变量设为非静态,并且在 main() 函数中创建了类的单个实例,然后在完成 10,000 次调用后要求您的类执行特定函数 10,000 次,并且您删除了对单个实例的引用,所有静态变量都可以被垃圾收集和重用.

Object Lifetime: Additionally, statics have a lifetime that matches the entire runtime of the program. This means, even once you're done using your class, the memory from all those static variables cannot be garbage collected. If, for example, instead, you made your variables non-static, and in your main() function you made a single instance of your class, and then asked your class to execute a particular function 10,000 times, once those 10,000 calls were done, and you delete your references to the single instance, all your static variables could be garbage collected and reused.

防止某些重复使用:此外,静态方法不能用于实现接口,因此静态方法可以防止某些面向对象的功能可用.

Prevents certain re-use: Also, static methods cannot be used to implement an interface, so static methods can prevent certain object oriented features from being usable.

其他选项:如果效率是您的主要关注点,那么可能有其他更好的方法来解决速度问题,而不是仅考虑调用通常比创建更快的优势.考虑是否在任何地方都需要瞬态或易失性修饰符.为了保持内联的能力,可以将方法标记为 final 而不是 static.方法参数和其他变量可以被标记为 final 以允许基于关于什么可以改变这些变量的假设进行某些编译器优化.一个实例对象可以多次重用,而不是每次都创建一个新实例.通常应该为应用程序打开编译器优化开关.也许,设计应该设置为 10,000 次运行可以是多线程的并利用多处理器内核.如果不考虑可移植性,也许原生方法会比静态方法获得更快的速度.

Other Options: If efficiency is your primary concern, there might be other better ways to solve the speed problem than considering only the advantage of invocation being usually faster than creation. Consider whether the transient or volatile modifiers are needed anywhere. To preserve the ability to be inlined, a method could be marked as final instead of static. Method parameters and other variables can be marked final to permit certain compiler optimiazations based on assumptions about what can change those variables. An instance object could be reused multiple times rather than creating a new instance each time. There may be compliler optimization switches that should be turned on for the app in general. Perhaps, the design should be set up so that the 10,000 runs can be multi-threaded and take advantage of multi-processor cores. If portablity isn't a concern, maybe a native method would get you better speed than your statics do.

如果由于某种原因您不想要一个对象的多个副本,单例设计模式, 优于静态对象,例如线程安全(假设您的单例编码良好),允许延迟初始化,保证对象在使用时已正确初始化,子类化,在测试和重构代码方面的优势,更不用说,如果在某个时候您改变了只想要一个对象实例的想法,那么删除代码以防止重复实例要比重构所有静态变量代码以使用实例变量容易得多.我以前不得不这样做,这不好玩,而且你最终不得不编辑更多的类,这增加了引入新错误的风险......第一次设置正确"要好得多,即使它看起来有它的缺点.对我来说,如果您决定需要多个副本,则所需的返工可能是尽可能少使用静力学的最令人信服的原因之一.因此我也不同意你关于静态减少相互依赖的说法,我认为如果你有很多可以直接访问的静态,而不是一个知道如何做的对象",你最终会得到耦合度更高的代码东西"本身.

If for some reason you do not want multiple copies of an object, the singleton design pattern, has advantages over static objects, such as thread-safety (presuming your singleton is coded well), permitting lazy-initialization, guaranteeing the object has been properly initialized when it is used, sub-classing, advantages in testing and refactoring your code, not to mention, if at some point you change your mind about only wanting one instance of an object it is MUCH easier to remove the code to prevent duplicate instances than it is to refactor all your static variable code to use instance variables. I've had to do that before, its not fun, and you end up having to edit a lot more classes, which increases your risk of introducing new bugs...so much better to set things up "right" the first time, even if it seems like it has its disadvantages. For me, the re-work required should you decide down the road you need multiple copies of something is probably one of most compelling reasons to use statics as infrequently as possible. And thus I would also disagree with your statement that statics reduce inter-dependencies, I think you will end up with code that is more coupled if you have lots of statics that can be directly accessed, rather than an object that "knows how to do something" on itself.

这篇关于为什么静态变量被认为是邪恶的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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