Questions tagged [immutability]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
7
votes
3answers
409 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
51 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 ...
0
votes
1answer
73 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 ...
1
vote
1answer
109 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 ...
2
votes
0answers
128 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 ...
0
votes
1answer
99 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://...
7
votes
2answers
372 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 ...
1
vote
2answers
85 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 ...
1
vote
1answer
518 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
103 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 ...
1
vote
0answers
21 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. ...
3
votes
2answers
830 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
1answer
163 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 ...
5
votes
4answers
531 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 ...
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
97 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 ...
1
vote
1answer
106 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 well. Is ...
0
votes
1answer
275 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 ...
6
votes
1answer
1k 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 ...
2
votes
3answers
1k 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 ...
2
votes
1answer
909 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 ...
0
votes
0answers
55 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 ...
3
votes
1answer
144 views

Making mutable classes immutabe

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) ...
4
votes
3answers
4k views

C# - Initialize DTOs in constructor or via properties?

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 via a REST-API. Here are two variants I can think of: ...
5
votes
3answers
630 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....
1
vote
3answers
247 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 ...
-2
votes
1answer
285 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 ...
12
votes
4answers
1k 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 ...
0
votes
2answers
333 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. ...
4
votes
4answers
537 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 = ...
3
votes
1answer
181 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 ...
11
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 ...
2
votes
1answer
630 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 ...
4
votes
1answer
203 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 ...
1
vote
1answer
531 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 ...
8
votes
3answers
4k 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 ...
1
vote
1answer
127 views

Is there a name for infered strong typing?

In JavaScript, you can have a loosely typed language. So doing the following is acceptable: var iterator = new TokenIterator(this.session, cursor.row, cursor.column); var matchType; var found = ...
5
votes
5answers
316 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 (...
1
vote
0answers
316 views

Dependency Injection with global objects (sessions)

TL;DR: My question is about how to use in conjunction: the dependency injection design pattern with global / immutable objects (ex: a session that references a customer object) I get the idea of ...
23
votes
5answers
2k views

How to model a circular reference between immutable objects in C#?

In the following code example, we have an class for immutable objects that represents a room. North, South, East, and West represent exits into other rooms. public sealed class Room { public Room(...
5
votes
3answers
678 views

Can a function be pure if it depends on an immutable instance field?

In the following c# code example, the instance field [name] is readonly, and therefore is immutable after class construction. public sealed class Example { public readonly string name; ...
5
votes
1answer
486 views

How to properly extend an interface with immutable properties to offer mutability via another interface

The following code snippets are simplified to demonstrate the context! The actual interfaces and classes are POCOs having additional properties. The types are part of library I am working on, the ...
2
votes
1answer
128 views

Preserving log of immutable Data Structure changes

I have large and complex immutable data structure (language is F#, but should be applicable to any language really) where I defined a lot of functions making changes and returning new instances of ds ...
1
vote
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 ...
0
votes
2answers
102 views

Is it a different object or the same object with an altered property (mutable or not)?

When I design the architecture of an application, I usually think about the nature of the objects and the interactions between them. The way I currently think about mutability is to ask myself if ...
1
vote
1answer
2k views

Why is ImmutableDictionary<TKey, TValue> implementing IDictionary<TKey, TValue>?

It looks like a weird design choice, because a Dictionary and an ImmutableDictionary are quite different write-wise. (Read-wise, they both implement IReadOnlyDictionary, which is fine*) In fact, the ...
1
vote
2answers
115 views

Passing and returning domain object in create method

We have this bean with two properties: ContainerImage + name (final, not null) + id (null) Then there is this method: ContainerImage create(ContainerImage containerImage); It receives ...
2
votes
3answers
911 views

Should I return Iterable or Enumeration in Java

In order to achieve better immutability, should my API return java.lang.Iterable<T> or java.util.Enumeration<E>? Since Iterable's iterator has a remove() method, one could say Enumeration ...
6
votes
8answers
999 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
1answer
5k views

Is it pythonic to use properties to limit the mutability of class attributes (variables and methods)?

Some Explanation I'm somewhat new to python and to programming (I've been at it for a little over a year). I just recently discovered python properties, and I've been using them to limit the ...