Questions tagged [immutability]

Immutability is about having objects that cannot be changed.

Immutability is about having objects that cannot be changed.

165 questions
250
votes
23 answers

If immutable objects are good, why do people keep creating mutable objects?

If immutable objects¹ are good, simple and offer benefits in concurrent programming why do programmers keep creating mutable objects²? I have four years of experience in Java programming and as I see it, the first thing people do after creating a…
Vinoth Kumar C M
  • 15,455
  • 23
  • 57
  • 86
140
votes
7 answers

Why doesn't Java 8 include immutable collections?

The Java team has done a ton of great work removing barriers to functional programming in Java 8. In particular, the changes to the java.util Collections do a great job of chaining transformations into very fast streamed operations. Considering…
GlenPeterson
  • 14,890
  • 6
  • 47
  • 75
108
votes
6 answers

Does immutability hurt performance in JavaScript?

There seems to be a recent trend in JavaScript towards treating data structures as immutable. For example, if you need to change a single property of an object, better to just create a whole new object with the new property, and just copy over all…
callum
  • 10,377
  • 9
  • 30
  • 33
85
votes
11 answers

Why is String immutable in Java?

I couldn't understand the reason of it. I always use String class like other developers, but when I modify the value of it, new instance of String created. What might be the reason of immutability for String class in Java? I know there are some…
yfklon
  • 1,752
  • 4
  • 19
  • 30
69
votes
4 answers

Why were Java collections implemented with "optional methods" in the interface?

During my first implementation extending the Java collection framework, I was quite surprised to see that the collection interface contains methods declared as optional. The implementer is expected to throw UnsupportedOperationExceptions if…
glenviewjeff
  • 1,405
  • 15
  • 17
68
votes
5 answers

When is it NOT good to use actors in akka/erlang?

I've been working with akka for 7-8 months now daily. When I started, I would be working on applications and notice that actors would be used basically anywhere once inside the actor system for communicating between most objects. So I did the same -…
JasonG
  • 1,129
  • 1
  • 10
  • 14
54
votes
9 answers

Is immutability very worthwhile when there is no concurrency?

It seems that thread-safety is always/often mentioned as the main benefit of using immutable types and especially collections. I have a situation where I would like to make sure that a method will not modify a dictionary of strings (which are…
Den
  • 4,827
  • 2
  • 32
  • 48
53
votes
2 answers

What did Alan Kay mean by "assignment" in The Early History of Smalltalk?

I have been reading The Early History of Smalltalk and there are a few mentions of "assignment" which make me question my understanding of its meaning: Though OOP came from many motivations, two were central. The large scale one was to find a…
50
votes
7 answers

Complete immutability and Object Oriented Programming

In most OOP languages, objects are generally mutable with a limited set of exceptions (like e.g. tuples and strings in python). In most functional languages, data is immutable. Both mutable and immutable objects bring a whole list of advantages and…
Hyperboreus
  • 609
  • 1
  • 5
  • 8
40
votes
5 answers

Does immutability entirely eliminate the need for locks in multi-processor programming?

Part 1 Clearly Immutability minimizes the need for locks in multi-processor programming, but does it eliminate that need, or are there instances where immutability alone is not enough? It seems to me that you can only defer processing and…
38
votes
8 answers

Difference between immutable and const

I've often seen the terms immutable and const used interchangeably. However, from my (little) experience, the two differ a lot in the 'contract' they make in code: Immutable makes the contract that this object will not change, whatsoever (e.g.…
K.Steff
  • 4,475
  • 2
  • 31
  • 28
36
votes
7 answers

Do you need to think about encapsulation if you can ensure immutability?

Encapsulation In object-oriented programming (OOP), encapsulation refers to the bundling of data with the methods that operate on that data, or the restricting of direct access to some of an object's components.1 Encapsulation is used to hide…
29
votes
7 answers

Is there any practical way for a linked node structure to be immutable?

I decided to write a singly-linked list, and had the plan going in to make the internal linked node structure immutable. I ran into a snag though. Say I have the following linked nodes (from previous add operations): 1 -> 2 -> 3 -> 4 and say I want…
Carcigenicate
  • 2,634
  • 3
  • 24
  • 38
28
votes
5 answers

Do not declare interfaces for immutable objects

Do not declare interfaces for immutable objects [EDIT] Where the objects in question represent Data Transfer Objects (DTOs) or Plain Old Data (PODs) Is that a reasonable guideline? Up to now, I've often created interfaces for sealed classes that are…
Matthew Watson
  • 381
  • 3
  • 5
28
votes
8 answers

Favoring Immutability in Database Design

One of the items in Joshua Bloch's Effective Java is the notion that classes should allow mutation of instances as little as possible, and preferably not at all. Oftentimes, the data of an object is persisted to a database of some form. This has led…
Ed Carrel
  • 585
  • 1
  • 5
  • 8
1
2 3
10 11