Empirical data on the effects of immutability?

后端 未结 9 477
长情又很酷
长情又很酷 2021-01-01 19:03

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++ b

相关标签:
9条回答
  • 2021-01-01 19:43

    The biggest advantage of immutability in Java, in my opinion, is simplicity. It becomes much simpler to reason about the state of an object, if that state cannot change. This is of course even more important in a multi-threaded environment, but even in simple, linear single-threaded programs it can make things far easier to understand.

    See this page for more examples.

    0 讨论(0)
  • 2021-01-01 19:46
    • Don't argue with the prof. You have nothing to gain.
    • These are open questions, like dynamic vs static typing. We sometimes think functional techniques involving immutable data are better for various reasons, but it's mostly a matter of style so far.
    0 讨论(0)
  • 2021-01-01 19:50

    What would you objectively measure? GC and object count could be measured with mutable/immutable versions of the same program (although how typical that would be would be subjective, so this is a pretty weak argument). I can't imagine how you could measure the removal of threading bugs, except maybe anecdotally by comparison with a real world example of a production application plagued by intermittent issues fixed by adding immutability.

    0 讨论(0)
提交回复
热议问题