DEV Community

Yonatan Karp-Rudin
Yonatan Karp-Rudin

Posted on • Originally published at yonatankarp.com on

Kotlin Code Smell 15 - Helper Classes

TL;DR: Helpers are non-cohesive and messy subroutines that don't actually help.

Problems

  • Lack of readability

  • Violation of the Least Surprise Principle

  • Issue with Bijection

  • Usage of static methods

Solutions

  • Choose an appropriate name.

  • If the helper is a library, divide all the services into different methods.

  • Methods should always be associated with objects. Avoid using static methods, as they are also considered a code smell.

  • Refrain from extracting helpers into Anonymous Functions.

Sample Code

Wrong

object UserHelper {
    fun getFullName(user: User) =
        "${user.firstName} ${user.lastName}"

    fun getCategory(user: User) =
        if (user.points > 70) 'A' else 'B'
}
Enter fullscreen mode Exit fullscreen mode

Note the use of static methods:

data class User(
    val firstName: String,
    val lastName: String,
    val points: Int
)

fun main() {
    val alice = User(
        firstName = "Alice",
        lastName = "Gray",
        points = 78
    )

    val fullName = UserHelper.getFullName(alice)
    val category = UserHelper.getCategory(alice)
}
Enter fullscreen mode Exit fullscreen mode

Right

class FullNameFormatter(private val user: User) {
    val fullName: String
        get() = "${user.firstName} ${user.lastName}"
}

class CategoryCalculator(private val user: User) {
    val displayName: Char
        get() = if(user.points > 70) 'A' else 'B'
}

fun main() {
    val alice = User(
        firstName = "Alice",
        lastName = "Gray",
        points = 78
    )

    val fullName = FullNameFormatter(alice).fullName
    val category = CategoryCalculator(alice).displayName
}
Enter fullscreen mode Exit fullscreen mode

Alternatively, the former Helper can be made stateless for reuse...

class FullNameFormatter {
    fun fullName(user: User): String =
        "${user.firstName} ${user.lastName}"
}

class CategoryCalculator {
    fun displayName(user: User): Char =
        if (user.points > 70) 'A' else 'B'
}

fun main() {
    val alice = User(
        firstName = "Alice",
        lastName = "Gray",
        points = 78
    )

    val fullName = FullNameFormatter().fullName(alice)
    val category = CategoryCalculator().displayName(alice)
}
Enter fullscreen mode Exit fullscreen mode

Conclusion

The usage of helper classes is a well-established cultural practice and a legacy habit from structured programming.

However, these names are causing harm and should be reconsidered.

It is essential for developers to let go of old habits and be aware of the negative impact these names can have.


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)