14
votes
Accepted
Can functional programming languages have deadlock conditions?
The quote is correct in principle. If you don't have any mutable state or side effects then you don't need to lock anything and without locks you don't get deadlocks.
But in reality, most programs, ...
12
votes
Should I expose an instance's state with methods?
It seems upon updates by the OP that I glommed onto the wrong part of the text and the CQS aspect of this is not terribly relevant to the real question. I'm going to leave the original answer since it ...
12
votes
Is the meaning of `const` still thread-safe in C++11?
The video you cited is for an advanced and expert audience. Herb Sutter tries to bring this audience to a consensus on how to best communicate the intent of these keywords to other people, in this age ...
10
votes
Accepted
How could a computer program do anything if everything is immutable?
Mutable:
public void DoubleArray(int[] array)
{
for (int i=0; i<array.length; i++)
array[i] *= 2;
}
Immutable:
public int[] DoubleArray(int[] array)
{
int[] result = new int[array....
8
votes
How could a computer program do anything if everything is immutable?
Here's what most people seem to miss about immutability. Consider the following function:
def factorial(n: Int): Int = if (n == 0) 1 else n * factorial(n - 1)
This function is completely immutable. ...
7
votes
Is the meaning of `const` still thread-safe in C++11?
Is it good practice while designing a thread-safe application to mark every member function const and every member variable mutable?
No, that is not good practice.
A const member function signals ...
6
votes
Accepted
Is private global mutable state ever appropriate, namely when used to prevent API misuse?
Global state is not necessarily evil, even in functional languages.
However:
all this does is enforce a consistent ID generation per process. Multiple processes could still clash. You have to decide ...
5
votes
Is there a natural architecture design where I should use mutable member variable(c++)?
Mutable member variables are useful if a class wants to save internal data, and the content is not relevant to the status of the class as seen from the outside (its user).
There are many situations ...
5
votes
Accepted
Should a method modifying object passed as a parameter return the modified object?
I think you have this backwards. The only thing that
private void normalizeDimensions(Request request);
can do is to modify the object (or have other side effects), so it's clear that's what this ...
5
votes
Accepted
Would an mutable to immutable transpiler be possible?
No, this is not possible in the general case since the data dependencies in a system can be very tricky.
As a simple example, consider the following setting:
var person = {...};
var someOtherObject = {...
4
votes
How could a computer program do anything if everything is immutable?
You are correct that perfectly immutable program, one that relies only on it's input when started wouldn't be much useful.
But the idea of immutability is not to make program completely immutable, it ...
4
votes
Should I expose an instance's state with methods?
Should you use methods over properties?
Properties are kind of methods already. But, practically, if you do this you will run into problems with serialisation libraries and the like (vue observables) ...
2
votes
Should a method modifying object passed as a parameter return the modified object?
In the functional world Philip Kendall is entirely correct. And in the OOP world we occasionally write functional code as well. So if you're going to make Request immutable then:
private Request ...
1
vote
How to avoid global mutable variables within a class?
tl;dr– If you need to share a mutable state between different methods in the same class, then having that mutable state stored in a field/property seems entirely reasonable. However, you may ...
1
vote
Is there a natural architecture design where I should use mutable member variable(c++)?
@Aganju has given some good examples. I'm just going to add one more, but with a substantial difference. The examples s/he gave were of cases where a class might reasonably consider using a mutable ...
1
vote
Is the meaning of `const` still thread-safe in C++11?
As already stated by others, a const function implies it should be thread-safe to call it from multiple threads simultaneously, without calling a non const function at the same time. As Herb Sutter (...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
mutable × 28immutability × 12
design × 5
c++ × 4
java × 3
object-oriented × 3
python × 3
functional-programming × 3
c# × 2
multithreading × 2
state × 2
globals × 2
const × 2
out-parameters × 2
design-patterns × 1
javascript × 1
programming-practices × 1
programming-languages × 1
coding-style × 1
data-structures × 1
refactoring × 1
interfaces × 1
coding-standards × 1
language-agnostic × 1
class-design × 1