Kotlin Code Smell 31 - Not Polymorphic

Kotlin Code Smell 31 - Not Polymorphic

A Path to Clearer Code and Stronger Design

Problems

  • Missed Polymorphism
  • Coupling
  • IFs / Type check Polluting.
  • Names are coupled to types.

Solutions

  1. Rename methods after what they do.
  2. Favor polymorphism.

Sample Code

Wrong

class Array {
    fun arraySort() { ... }
}

class List {
    fun listSort() { ... }
}

class Set {
    fun setSort() { ... }
}

Right

interface Sortable {
    fun sort()
}

class Array : Sortable {
    override fun sort() { ... }
}

class List : Sortable {
    override fun sort() { ... }
}

class Set : Sortable {
    override fun sort() { ... }
}

Conclusion

Naming is very important. We need to name concepts after what they do, not after accidental types.

Credits