Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.

Questions tagged [immutability]

Immutability is about having objects that cannot be changed.

Filter by
Sorted by
Tagged with
4 votes
2 answers
2k views

I've read somewhere about a pattern that is as follows: if it'll change the state of the instance, the method should be named with a verb and return void; and if the method just returns something (...
Bernardo Benini Fantin's user avatar
12 votes
7 answers
5k views

I'm quite confused about the concept of mutability (or mutation?), especially across different languages. In a language like Python: x = 10 x = 20 I believe this is called shadowing (or rebinding) ...
Ghassen's user avatar
  • 185
0 votes
1 answer
298 views

Lombok is used in the legacy project where I am currently working for since last year. The project is legacy with 10+ years, and POJO/JavaBeans, i.e. @Data annotated classes, have been widely used for ...
Rui's user avatar
  • 1,935
3 votes
4 answers
2k views

I have a Spring service that acts as an adapter for another service in my company. The service receives a request to generate push notifications for a given user and needs to call the other service ...
VariabileAleatoria's user avatar
1 vote
5 answers
398 views

Edit: I'm copying the question but changing the example code. Apparently, I used a bad example earlier that contained an imprue getter. I'm keeping the old example code at the bottom so the first ...
sharbel okzan's user avatar
0 votes
1 answer
1k views

At my org we extensively use @Value.Immutable annotation to generate Immutable classes (usually with builders) for our internal data transfer objects (DTOs). In our codebase I've seen both interfaces ...
y2k-shubham's user avatar
6 votes
2 answers
1k views

While I was learning functional programming I have encounterd two for me very similar terms: immutability and persistence. Also I have read simular questions from stackoverflow, but I am still ...
DimitrijeCiric's user avatar
0 votes
4 answers
2k views

Java never had immutable primitive arrays. However Java does have an immutable List or Map or other collection classes and of course final primitive fields and variables. In Java if you try to make an ...
CPlus's user avatar
  • 1,219
10 votes
9 answers
4k views

The most popular answer is - it is an object whose state does not change after creation. What does it actually mean? My understanding is that any method call on the object should give the same result. ...
kan's user avatar
  • 402
2 votes
0 answers
119 views

None of my team's programs care about multithreading, parallelisation, async, or anything else that benefit from immutability. However, immutability is clearly in fashion at the moment and I'm ...
J. Mini's user avatar
  • 1,015
8 votes
2 answers
2k views

I am looking at the List<T>.AsReadOnly() method. Since List<T> itself is a IReadOnlyCollection<T>. (It implements IReadOnlyList<T> and IReadOnlyList<T> implements ...
Madushan's user avatar
  • 233
1 vote
1 answer
1k views

With Java record being immutable, I don't know how to use them properly in the context of something like a CRUD application. If we take a very simple example of a "Organization" record: ...
Chris's user avatar
  • 131
16 votes
6 answers
7k views

I've heard people say things like "B can't inherit from A because A is immutable and B is mutable". My understanding of inheritance in Object-Oriented Programming is that you use it to add ...
GlenPeterson's user avatar
7 votes
1 answer
536 views

I am reading through "Clean Architecture: A Craftsman's Guide to Software Structure and Design" and it says that: All race conditions, deadlock conditions, and concurrent update problems are ...
Quantum Guy 123's user avatar
1 vote
3 answers
2k views

Is it a good idea to mark a function const from clean code perspective if it change other objects' states? I'd like to know what is the experience with this or is it considered a bad practice for any ...
66Gramms's user avatar
  • 129
5 votes
2 answers
3k views

I have a code that reads a YAML file that consists of several parameters whose values are used throughout the code. For clarity, the YAML file has the following structure: PARAM_1: value_1 PARAM_2: ...
blunova's user avatar
  • 398
8 votes
2 answers
942 views

Which are the technical reasons/considerations for the sort() and reverse() JavaScript array methods to be in-place operations instead of returning a new array without modifying the original one, like ...
MauricioRobayo's user avatar
2 votes
5 answers
3k views

I'm learning about immutability (specifically in C#) and I can't understand the examples out there stating that making an object immutable brings real benefits (besides a shared object in a multi ...
underthevoid's user avatar
1 vote
0 answers
44 views

So, I have an entity called Consultation that is being used as a member variable of the class VideoCallViewModel that goes like this: struct Consultation: Equatable { enum Status: Int, ...
Bawenang Rukmoko Pardian Putra's user avatar
1 vote
3 answers
1k views

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 ...
Learning stats by example's user avatar
0 votes
1 answer
165 views

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 ...
phil-daniels's user avatar
1 vote
2 answers
2k views

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 ...
Enrico Massone's user avatar
5 votes
3 answers
736 views

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 ...
Julian's user avatar
  • 77
20 votes
8 answers
7k views

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 ...
releseabe's user avatar
  • 539
16 votes
10 answers
7k views

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 ...
releseabe's user avatar
  • 539
2 votes
1 answer
324 views

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, ...
Luciano Fiandesio's user avatar
1 vote
3 answers
203 views

I have the following code: const string endPoint = @"foo{0}?pageNum={1}&itemsPerPage={2}"; const int itemsPerPage = xxx; InvoiceCollection response = await _apiClient .GetAsync<...
Anjo's user avatar
  • 215
38 votes
7 answers
9k views

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 ...
Christopher Trotter's user avatar
3 votes
3 answers
607 views

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 ...
ljleb's user avatar
  • 141
8 votes
3 answers
1k views

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 ...
jbruenker's user avatar
0 votes
0 answers
200 views

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 ...
Eric Schneider's user avatar
1 vote
1 answer
677 views

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 mixed ...
Sir_Faenor's user avatar
1 vote
1 answer
157 views

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 ...
Stefanos Chrs's user avatar
5 votes
1 answer
325 views

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 ...
dwjohnston's user avatar
  • 2,769
-1 votes
2 answers
354 views

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://...
Stefan's user avatar
  • 87
7 votes
2 answers
1k views

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 ...
rahulaga-msft's user avatar
1 vote
2 answers
159 views

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 ...
ClemC's user avatar
  • 401
2 votes
1 answer
2k views

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 ...
Rulle's user avatar
  • 181
1 vote
1 answer
228 views

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 ...
timbo's user avatar
  • 159
1 vote
0 answers
52 views

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. ...
Lance Pollard's user avatar
3 votes
2 answers
5k views

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 ...
Jeff's user avatar
  • 149
1 vote
1 answer
418 views

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 ...
richzilla's user avatar
  • 1,153
4 votes
4 answers
975 views

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 ...
Lance Pollard's user avatar
9 votes
7 answers
4k views

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 ...
rahulaga-msft's user avatar
2 votes
2 answers
126 views

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 ...
pintoch's user avatar
  • 159
1 vote
1 answer
209 views

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 ...
alfonsodev's user avatar
0 votes
1 answer
542 views

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 ...
deamon's user avatar
  • 886
6 votes
1 answer
2k views

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 ...
Gulshan's user avatar
  • 9,542
4 votes
3 answers
3k views

I was reading on this software engineering page about the use of static methods. However, I'm confused, and maybe it's about the context in which it talks about static. The idea is, that static is ...
user avatar
3 votes
1 answer
2k views

I have been increasingly using immutable data storing for my web-app, due to some advantages it has (simpler writes, ability to view history / activity). I am doing it using MySQL dabatases, simply ...
user avatar