Questions tagged [immutability]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
102
votes
6answers
26k views

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 ...
1
vote
3answers
216 views

How is inheritance possible with immutability?

The title almost sums it up but let me be example-led and clearer. Assuming we have a class: class T: def goto(self, value): print('go') And a second inheriting class: class A(T): def ...
1
vote
2answers
152 views

Why it is possible to specify a value comparer for ImmutableDictionary<TKey, TValue>?

What is the intended usage of the ImmutableDictionary<TKey,TValue>.ValueComparer property? Why is it useful being able to compare dictionary values by using a specified equality semantic? I ...
0
votes
1answer
93 views

Would an mutable to immutable transpiler be possible?

I'm very new to writing immutable code. Would there be any fundamental issues trying to transpile arbitrary mutable code person.age = 20 into immutable code person = { ...person, age: 21 }? I am aware ...
5
votes
3answers
543 views

How could a computer program do anything if everything is immutable?

I feel this is a bad question because I probably do not understand what I am talking about. In my effort to learn about functional programming, I became stumped on understanding the idea of immutable ...
16
votes
10answers
5k views

Are immutable objects important only in multi-threaded applications and if so, how are shared immutable objects useful?

I think the answer to the first part of my question is, "yes" -- no point in making objects immutable in a single-threaded application (or I guess in a multi-threaded application if that ...
20
votes
8answers
5k views

Immutable String and Integer in Java: What is the point if assignment in effect changes the value?

If immutability is "good" and yet you can in effect change the value in an Integer or String variable (never mind that you get a new reference -- the value has changed) what good is it that ...
138
votes
6answers
50k views

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 ...
12
votes
2answers
2k views

Can we really use immutability in OOP without losing all key OOP features?

I see the benefits of making objects in my program immutable. When I am really deeply thinking about a good design for my application I often naturally arrive at many of my objects being immutable. It ...
9
votes
3answers
8k views

C# - Initialize DTOs in constructor or via properties?

Update: C# 9 should fix this problem using records :D I was wondering if there is a recommended approach to initializing the properties of a plain object that is used for data transfer, for example ...
30
votes
8answers
37k views

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 ...
5
votes
1answer
298 views

What is the state of immutability in Javascript in 2019?

I'm all on board with functional programming in Javascript - particularly within the context of using React and Redux. Something that I've been running into again and again, is how easy it is to ...
27
votes
8answers
13k views

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 ...
1
vote
1answer
66 views

Validation framework and immutability

I have designed a simple objects validation framework in Java in the context of a code refactoring. The framework has a ValidationRule interface with one method Errors validate(MyObject myObject, ...
1
vote
3answers
139 views

Should we consider immutability for local scoped variables

I have the following code: const string endPoint = @"foo{0}?pageNum={1}&itemsPerPage={2}"; const int itemsPerPage = xxx; InvoiceCollection response = await _apiClient .GetAsync<...
36
votes
6answers
8k views

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 ...
48
votes
7answers
10k views

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 ...
-1
votes
2answers
160 views

Immutability and encapsulating state changing behavior

Immutable objects In object-oriented and functional programming, an immutable object (unchangeable[1] object) is an object whose state cannot be modified after it is created Wikipedia (https://...
65
votes
5answers
25k views

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 ...
0
votes
1answer
137 views

PHP: simple class for storing read-only data structure, array alternative

I need to share some associative data between different parts of my application with two requirements: immutability (so read-only) safety against mispelling or unavailable index inside the data ...
3
votes
3answers
514 views

Does variance make sense in a fully immutable language?

In many OOP programming languages, types can be made co-, contra- or in- variant. Most (if not all of these languages) are able to let variables be mutated in place, i.e. they are not fully immutable ...
7
votes
3answers
530 views

Avoiding side effects in immutable class constructor

I rewrote a very long method in which some data is queried from a database, based on info about a particular account, which is queried first. I split out the account info into an immutable inner ...
0
votes
0answers
99 views

Need some design advice on my ORM for Immutable objects & ref passing

I'm adding Immutable objects support to my Micro ORM called "Symbiotic" In the case of a create, I need to pass back a newly created version of the value passed in because the object is immutable and ...
13
votes
3answers
2k views

Is it possible to represent mutation of object-graph efficiently with immutable states?

I am practicing using of immutable object in C++. My personal goal is representing generic object graph (in heap) with sequence of immutable graphs. Building the multi-version graph itself isn't that ...
1
vote
1answer
127 views

Should a function implementer or function caller handle immutability

I know there is no right and wrong answer here, I'm just looking for other opinions on who should handle immutability, dependency inversion and decoupling. Example 1: Here each function caller has ...
5
votes
5answers
341 views

Immutable vs mutable mobile object

I need to model a very simple mobile object (Robot) on a rectangular grid. Its state is described by position (basically a pair of integers) and direction (e.g., N, E, S, W). It only has two methods (...
12
votes
6answers
4k views

What are the drawbacks of immutable types?

I see myself using more and more immutable types when the instances of the class are not expected to be changed. It requires more work (see example below), but makes it easier to use the types in a ...
8
votes
8answers
2k views

How does immutability remove the need for locks when two threads are trying to update the shared state?

Okay so I read through this: Does immutability entirely eliminate the need for locks in multi-processor programming? And this was the main takeaway for me: Now, what does it get you? Immutability ...
6
votes
2answers
650 views

Encapsulating moving parts in OO vs Minimizing moving parts in FP

I am from OO background just started learning FP paradigm. Came across quote by Michael Feathers - "OO makes code understandable by encapsulating moving parts. FP makes code understandable by ...
5
votes
2answers
692 views

Make classes immutable or compatible with standard containers

Making types immutable is often desireable, especially for multi-threaded applications. There's no need to worry about concurrent access and no need for any synchronization. The standard containers ...
4
votes
4answers
702 views

Optimal Immutable Data Structure for Highly Dynamic Particle System

Trying to see if immutability is a good fit for highly dynamic objects like game entities (that constantly are moving around and changing) or particle systems, each which might have thousands or ...
5
votes
3answers
850 views

Is it reasonable for an interface to specify that its implementations should be immutable?

Let's say I have an interface A. Instances of classes implementing this interface will be passed on to an algorithm that relies on these instances being immutable. For instance, it'll be invoking A....
3
votes
2answers
2k views

Changing immutable objects how does this work?

I have seen examples where even though the object is immutable there are situations in which we need to update some fields and as I understand it, the object is passed to a constructor which makes a ...
1
vote
2answers
115 views

In some IOC contexts, could constructor of polymorphic immutable object be required in its interface?

Generally, constructors in an interface are considered as being an anti-pattern. Some languages even don't allow that. Then I don't understand how we're supposed to deal with polymorphic immutable ...
2
votes
3answers
3k views

How do you create immutable objects with many parameters?

I need to create an immutable object but it requires several parameters to work like: class FooRepo { public string ConnectionStringName { get; } public string SchemaName { get; } public ...
1
vote
1answer
1k views

C++ immutable struct

I would like to be able to implement immutable data in C++. In short, given a C++ object in which I would like to modify a member variable, instead of modifying that member in place I would like to ...
1
vote
1answer
154 views

OrderedMap or List for Redux structure?

I'm implementing a Redux store for a React app using redux-saga for API calls. I've used Immutable.js before for other Redux-using React apps but the nature of previous projects always evidently ...
69
votes
4answers
8k views

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 ...
1
vote
0answers
28 views

Data Storage Solutions for a BlockChain

The blockchain stores basically an immutable permanent record of every transaction. This means the data is constantly increasing. Wondering if one has a model for easily roughly estimating data usage. ...
1
vote
1answer
228 views

How do languages ensure 'interior immutability'?

In C#, i can declare an immutable field on a class using readonly. This works for value types, but if its a reference type, there is nothing stopping me calling methods on the object to change its ...
13
votes
1answer
3k views

Why does appending to a List in Scala have O(n) time complexity?

I just read that the execution time of the append operation for a List (:+) grows linearly with the size of the List. Appending to a List seems like a pretty common operation. Why should the ...
9
votes
6answers
3k views

what can go wrong in context of functional programming if my object is mutable?

I can see the benefits of mutable vs immutable objects like immutable objects take away lot of hard to troubleshoot issues in multi threaded programming due to shared and writeable state. On the ...
2
votes
2answers
98 views

Creating a modified copy of an instance of an abstract interface

Say I have an interface Interface, which only contains getters for various fields. This interface has multiple implementations (say Foo and Bar), each of which adds various fields. All these ...
2
votes
3answers
359 views

Reducing mutability in an event-driven system

I'm building a game with the LibGDX game engine. The engine works by registering an implementation of an interface that receives callbacks on the game loop. The interface looks like this: public ...
1
vote
1answer
119 views

What does it mean “state complects value and time”?

I'm rewatching Rich Hickey great talk "Simple Made Easy" And around min 35:40 when talking about state, mentions that State complects value and time, but I'm not sure I'm understanding this ...
0
votes
1answer
348 views

java.util.function.Function as an interface for an mutable object

I have an interface with only a single method that could be replaced by java.util.function.Function from the JDK: interface DataModel { boolean apply(Map<String, Integer> input); } The ...
24
votes
8answers
996 views

How does one decide if a data object type should be designed to be immutable?

I love the immutable "pattern" because of its strengths, and in the past I have found it beneficial to design systems with immutable data types (some, most or even all). Often when I do so, I find ...
18
votes
7answers
7k views

At what point do immutable classes become a burden?

When designing classes to hold your data model I've read it can be useful to create immutable objects but at what point does the burden of constructor parameter lists and deep copies become too much ...
53
votes
9answers
5k views

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 ...
6
votes
1answer
2k views

Does immutability reduce the scope of unit testing?

Recently, I worked on an Android project with Kotlin. As an architecture, there were immutable data classes and functions (not methods which are members of a class), where the functions generate new ...