Search code examples
tddimmutabilitysmalltalkpharo

Smalltalk, TDD, and immutable objects


Like many people, I've wanted to really learn smalltalk and TDD. For fun, I've been going through the excellent Tetris TDD tutorial at: https://github.com/orfjackal/tdd-tetris-tutorial/blob/tutorial/README.md

and converting the code to Smalltalk.

I've gotten to the third set of tests (https://github.com/orfjackal/tdd-tetris-tutorial/blob/tutorial/src/test/java/tetris/RotatingTetrominoesTest.java) which wants the objects representing the pieces to be immutable.

I'm not sure how to do this. Every attempt I make breaks the earlier tests. I did create a TetrisTetrominoe class as a subclass of TetrisPiece.

Using Pharo 3.0.

Thanks.


Solution

  • You have to take into account the dramatic difference between languages. Some time ago I've wrote a blog post with thoughts about immutability.

    I think that if you want to follow test's for another language, you should try to find some for python or ruby, because they are also dynamic languages.

    If you want to maintain immutability, I suggest you going the functional way, where each method that has intent to modify object creates a new one, with required properties. I doubt that this will satisfy java tests as java is not functional as much as it's not dynamic.