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…
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…
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…
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…
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…
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 -…
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…
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…
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…
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…
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.…
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…
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…
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…
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…