Skip to main content
4 of 14
added 4 characters in body
Ahmad
  • 1.9k
  • 3
  • 21
  • 36

OOP best practice: Mutable with logic inside or immutable with logic outside

I know immutable objects are preferred to mutable objects for reasoning and maintenance and... but in occasions making a class immutable have some costs, let me explain it with a simple example:

I have a class which draws a text in a box:

class TextDrawer { private string text; private Rectangle box; public TextDrawer(_text, _box) {...} public void DrawTextInBox() { } } 

It is probable that the Text overflows from the box and part of it wasn't written. I should know the remaining unwritten part to draw it in a new box.(in fact I take an snapshot of the old box as a video frame and need to make a new frame for the remaining text to make a video of the text)

Now I have two options:

1- Make the TextDrawer mutable and DrawTextInBox a non-pure function and on each call it draws the remaining part of text in the box.

class TextDrawer { private string text; private Rectangle box; private string remaining; public TextDrawer(_text, _box) { remaining = text = _text; box = _box; } public void DrawTextInBox() { draw as you can update remaining part; } } 

2- Keep the TextDrawer immutable and DrawTextInBox a pure function which returns the remaining part, then the caller should keep track of the remaining and pass it again to the TextDrawer or instantiate a new TextDrawer by the remaining text until there is nothing to write.

class TextDrawer { private string text; private Rectangle box; public TextDrawer(_text, _box) { ... } public string DrawTextInBox() { draw as you can; return remaining part; } } 

The second approach is immutable but actually shifts the responsibility of updating the remaining part outside of the class. The first approach is mutable but just need a call to DrawTextInBox to draw the remaining part.

The logic of the above example was simple but this logic could be more complex, then I don't know to extract this logic from a class in order to make it immutable or let it be mutable and have the logic inside?

Ahmad
  • 1.9k
  • 3
  • 21
  • 36