Kotlin Code Smell 31 - Not Polymorphic
A Path to Clearer Code and Stronger Design
Table of contents
Problems
- Missed Polymorphism
- Coupling
- IFs / Type check Polluting.
- Names are coupled to types.
Solutions
- Rename methods after what they do.
- 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.