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 changing a member changes the object altogether. A car can be repainted, but if you change the manufacturer it's another car.
Now, this problem arose while designing a simple application which draws a small animation. The animation is a line that grows in width up to some point, and then remains the same.
Assume there is a Line object, with a width member and drawing functionality. Now I have 2 approaches:
for (int newWidth = 10; newWidth < 1000; newWidth++) {
line.setWidth(newWidth);
line.draw();
}
, which means that width is a mutable property of the line, or:
for (int newWidth = 10; newWidth < 1000; newWidth++) {
line = Line(newWidth);
line.draw();
}
, which creates a new object (i.e. a different line) for every width.
As an overall performance, the second version looks more inefficient, but which one is better in terms of design?