关于不变性影响的经验数据? [英] Empirical data on the effects of immutability?

查看:86
本文介绍了关于不变性影响的经验数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天在课堂上,我的教授正在讨论如何构建课程.该课程主要使用Java,并且我比老师有更多Java经验(他来自C ++背景),所以我提到在Java中应该支持不变性.我的教授请我为我的回答辩护,并给出了我从Java社区听到的原因:

In class today, my professor was discussing how to structure a class. The course primarily uses Java and I have more Java experience than the teacher (he comes from a C++ background), so I mentioned that in Java one should favor immutability. My professor asked me to justify my answer, and I gave the reasons that I've heard from the Java community:

  1. 安全性(尤其是带有线程)
  2. 减少的对象数量
  3. 允许某些优化(尤其是对于垃圾收集器)

教授对我的发言提出了质疑,他说他想对这些好处进行一些统计测量.我引用了大量的轶事证据,但即使这样做,我仍然意识到他是对的:据我所知,还没有关于不变性是否真正提供了它在现实世界代码中所带来的好处的实证研究.我知道这是从经验中得出的,但是其他人的经验可能会有所不同.

The professor challenged my statement by saying that he'd like to see some statistical measurement of these benefits. I cited a wealth of anecdotal evidence, but even as I did so, I realized he was right: as far as I know, there hasn't been an empirical study of whether immutability actually provides the benefits it promises in real-world code. I know it does from experience, but others' experiences may differ.

所以,我的问题是,在现实世界代码中是否有关于不可变性影响的统计研究?

So, my question is, have there been any statistical studies done on the effects of immutability in real-world code?

推荐答案

我将指向

I would point to Item 15 in Effective Java. The value of immutability is in the design (and it isn't always appropriate - it is just a good first approximation) and design preferences are rarely argued from a statistical point of view, but we have seen mutable objects (Calendar, Date) that have gone really bad, and serious replacements (JodaTime, JSR-310) have opted for immutability.

这篇关于关于不变性影响的经验数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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