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'
}
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)
}
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
}
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)
}
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
- Code Smell 22 - Helpers by @Maxi Contieri
Top comments (0)