Questions tagged [immutability]

Immutability is about having objects that cannot be changed.

Filter by
Sorted by
Tagged with
3 votes
2 answers
427 views

Should I use a namedtuple or dictionary to store immutable values?

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: ...
user avatar
  • 358
7 votes
2 answers
481 views

Why are sort() and reverse() JavaScript methods in-place?

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 ...
user avatar
2 votes
5 answers
421 views

Trying to understand the benefits of immutability in imperative programming

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 ...
user avatar
1 vote
0 answers
38 views

Should I separate a variable that changes often into a different entity although it is contextually should be inside another entity?

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, ...
user avatar
1 vote
3 answers
503 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 ...
user avatar
0 votes
1 answer
121 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 ...
user avatar
  • 167
1 vote
2 answers
345 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 ...
user avatar
5 votes
3 answers
606 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 ...
user avatar
  • 77
20 votes
8 answers
6k 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 ...
user avatar
  • 517
16 votes
10 answers
6k 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 ...
user avatar
  • 517
1 vote
1 answer
112 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, ...
user avatar
1 vote
3 answers
144 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<...
user avatar
  • 215
36 votes
7 answers
9k 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 ...
user avatar
3 votes
3 answers
531 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 ...
user avatar
  • 41
7 votes
3 answers
628 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 ...
user avatar
0 votes
0 answers
116 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 ...
user avatar
0 votes
1 answer
242 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 ...
user avatar
1 vote
1 answer
132 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 ...
user avatar
5 votes
1 answer
303 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 ...
user avatar
  • 2,412
-1 votes
2 answers
239 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://...
user avatar
  • 87
6 votes
2 answers
770 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 ...
user avatar
1 vote
2 answers
130 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 ...
user avatar
  • 383
1 vote
1 answer
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 ...
user avatar
  • 171
1 vote
1 answer
186 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 ...
user avatar
  • 159
1 vote
0 answers
30 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. ...
user avatar
  • 2,412
3 votes
2 answers
3k 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 ...
user avatar
  • 149
1 vote
1 answer
272 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 ...
user avatar
  • 1,043
4 votes
4 answers
780 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 ...
user avatar
  • 2,412
9 votes
7 answers
4k 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 ...
user avatar
2 votes
2 answers
101 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 ...
user avatar
  • 131
1 vote
1 answer
133 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 ...
user avatar
0 votes
1 answer
412 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 ...
user avatar
  • 786
6 votes
1 answer
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 ...
user avatar
  • 9,222
4 votes
3 answers
3k views

Using static to create an immutable object

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

Best way to query immutable MySQL Data

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
0 votes
0 answers
68 views

Is making method parameters final a worthy endeavour? [duplicate]

It is generally a good idea to restrict access to your variables as much as reasonably possible. For example make things private instead of publicand make variables final if they aren't supposed to ...
user avatar
3 votes
1 answer
203 views

Making mutable classes immutable

When trying to turn a class with mutable state into an immutable one I am regularly having a hard time choosing between two alternatives: I can 1) either extract the state into another (immutable) ...
user avatar
  • 161
11 votes
4 answers
11k 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 ...
user avatar
  • 359
5 votes
3 answers
956 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....
user avatar
  • 267
2 votes
3 answers
394 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 ...
user avatar
  • 9,007
-2 votes
1 answer
391 views

HTML5/Canvas Graphics editor and Immutable state (EaselJS and Redux, ngrx/store)

This question is about the architecture approach. How would someone go about creating the immutable state for a graphics editor that would eventually provide an "undo/redo" functionality, considering ...
user avatar
15 votes
4 answers
2k views

When programming in Functional style, do you have a single application state that you weave through the application logic?

How do I construct a system that has all of the following: Using pure functions with immutable objects. Only pass into a function data that the function it needs, no more (i.e. no big application ...
user avatar
2 votes
2 answers
444 views

Automatically convert mutations in imperative code into functional actions?

I wonder if somebody tried creating a language where you can write mutations in a straightforward imperative style, and the compiler transforms the changes automatically to pure, Redux-style actions. ...
user avatar
  • 602
4 votes
4 answers
1k views

Can an immutable class have a zero argument constructor?

Please see the code below: public List<DenominationDTO> CalculateChange( decimal cost, decimal paymentReceived, string currencySymbol) { var currency = ...
user avatar
  • 6,827
3 votes
1 answer
196 views

Efficient algorithm for deducing object type dynamically based on members

I am designing a DSL (in clojure, specifically; though this question is more general than that) in which 'entities' are tracked as immutable hashes/maps, and where the 'concept' membership of an ...
user avatar
  • 316
12 votes
2 answers
3k 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 ...
user avatar
  • 447
4 votes
1 answer
811 views

Is undo and redo possible/feasible with two-way data binding (vs uni-directional data flow)?

I'm developing an experimental application for work that requires fully persistent undos and redos. I decided to use react/redux/immutable JS tech stack to solve this problem because I saw it worked ...
user avatar
4 votes
1 answer
254 views

Making a change-prone class immutable

According to Effective Java's Item 15: Minimize Mutability, it is always better to make classes Immutable. According to that, how would I handle classes that are by nature given to change? Some ...
user avatar
2 votes
1 answer
802 views

Are immutable integers also collected by the Garbage Collector, like immutable strings?

I already got the idea that string variables are immutable, so the they are copied to another place in heap every time we make a change, and since GarbageCollector has to collect them all the time, it ...
user avatar
  • 131
8 votes
3 answers
7k views

Are all the objects in C++ mutable if not stated otherwise?

Is every object in C++ mutable if not stated otherwise? In Python and Javascript I can't change strings, tuple, unicodes. I was wondering if there is something in C++ that is immutable or every ...
user avatar
  • 257