DEV Community

Yonatan Karp-Rudin
Yonatan Karp-Rudin

Posted on • Originally published at yonatankarp.com on

Kotlin Code Smell 25 - State as Properties

When an object changes its state, the best solution is to modify the attribute, right?

Problems

  • Mutability

  • Attribute Polluting

  • Setters

Solutions

  1. Use immutable objects

  2. The model states as mathematical set inclusion.

  3. Separate the state from the object as it is accidental.

Examples

  • State diagrams

Sample Code

Wrong

sealed class OrderState
data object OrderStatePending : OrderState()
data object OrderStateConfirmed: OrderState()

class Order(private val items: List<Int>) {

    private var state: OrderState = OrderStatePending

    fun changeState(newState: OrderState) {
        state = newState
    }
}

Enter fullscreen mode Exit fullscreen mode

Right

sealed class OrderState
data object OrderStatePending : OrderState()
data object OrderStateConfirmed: OrderState()

data class Order(val items: List<Int>, val state: OrderState)

val items = listOf(1, 2, 3)
val pendingOrder = Order(items, OrderStatePending)
val confirmedOrder = pendingOrder.copy(state = OrderStateConfirmed)

Enter fullscreen mode Exit fullscreen mode

Exceptions

  • Over Design

  • Performance issues (if a serious benchmark supports it).

Conclusion

This technique is elegant but can lead to overdesign. For example, changing a visual component's color should be a counterexample of this smell.

We should be aware and very cautious as with any other smell.

These are hints and not rigid rules.


Stay updated with my latest thoughts and ideas by registering for my newsletter. Connect with me on LinkedIn or Twitter. Let's stay connected and keep the conversation going!


Credits

Top comments (0)