Questions tagged [mutable]
27 questions
57
votes
3 answers
Is it an antipattern to use peek() to modify a stream element?
Suppose I have a stream of Things and I want to "enrich" them mid stream, I can use peek() to do this, eg:
streamOfThings.peek(this::thingMutator).forEach(this::someConsumer);
Assume that mutating the Things at this point in the code is correct…

Bohemian
- 1,956
- 2
- 17
- 24
54
votes
9 answers
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 not modify a dictionary of strings (which are…

Den
- 4,827
- 2
- 32
- 48
8
votes
2 answers
Overriding GetHashCode in a mutable struct - What NOT to do?
I am using the XNA Framework to make a learning project. It has a Point struct which exposes an X and Y value; for the purpose of optimization, it breaks the rules for proper struct design, since its a mutable struct.
As Marc Gravell, John Skeet,…

Kyle Baran
- 436
- 7
- 14
7
votes
1 answer
Can functional programming languages have deadlock conditions?
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 due to mutable variables.
Functional programming languages…

Quantum Guy 123
- 181
- 3
7
votes
4 answers
Is the meaning of `const` still thread-safe in C++11?
I recently came across Herb Sutter's video from about how the meaning of const and mutable has changed in C++11 to mean bitwise const (and thread-safe, as a consequence) instead of the traditional logically const.
Five years later, have programmers…

CK.
- 187
- 1
- 2
6
votes
1 answer
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 mutability of my classes' attributes. It helps me or maybe…

Josie Thompson
- 271
- 2
- 6
6
votes
4 answers
Is using "out" or "ref" parameters in Java methods to return extra values bad?
I happened to create a mutable class like this:
class Mutable {
private T value;
public Mutable() { this.value = null; }
public Mutable(T value) { this.value = value; }
T get() { return this.value; }
void set(T value) {…

hyde
- 3,744
- 4
- 25
- 35
5
votes
3 answers
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 stuff. I have been using C# as a hobby for years…

Julian
- 77
- 4
5
votes
1 answer
Is private global mutable state ever appropriate, namely when used to prevent API misuse?
I am writing a type checker for an ML dialect that involves generating "fresh" (new and unique) "type variables" (values representing unknowns). My strategy, and the strategy that seems to be used in tutorials, is to identify type variables with a…

Del
- 161
- 6
5
votes
1 answer
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 interfaces are public API, the classes internal…

ckerth
- 51
- 4
3
votes
2 answers
Should a method modifying object passed as a parameter return the modified object?
I have some incoming request - it's an instance of class generated from api specification - POJO with public getters/setters.
I would like to normalize some values. For example dimensions (to use metric system).
I have a service class which has…

Shaolin
- 33
- 5
3
votes
1 answer
Encapsulating mutable objects with special structure in Python
I am writing a library for working with special types of trees, called Foo trees. A Foo tree has very special structure. There some operations, called bar and baz, which only make sense on Foo trees. So I'm designing a FooTree class to represent Foo…

jme
- 559
- 1
- 5
- 13
3
votes
4 answers
How to refactor my project to have less mutable objects?
There seems to be a trend towards immutable objects, and functional programming. I recently got aware about the benefits of immutability. However, I am not very familiar with this style of programming.
My current classes may have some private or…

Ahmad
- 1,806
- 3
- 20
- 32
3
votes
2 answers
Why must essential mutable derived data have an inverse function?
I was reading the paper Out of the Tar Pit authored by Ben Moseley and Peter Marks when I came across the following section on page 25 regarding essential mutable derived data:
Essential Derived Data — Mutable
As with immutable essential derived…

Aadit M Shah
- 1,618
- 2
- 11
- 9
3
votes
2 answers
subclass of immutable object not immutable, can this work?
So I'm finishing up refactoring some code to remove a number of previously-mutable objects and add a better generic processing for all the classes in the domain. Just as I thought I was finishing I eralized that there is one sub-class that has some…

dsollen
- 1,123
- 1
- 12
- 28