Hello! Is it considered bad practice to define ext...
# announcements
g
Hello! Is it considered bad practice to define extension functions for native types, such as
String
? Example, putting a function like this globally scoped in like a
StringUtils.kt
class:
Copy code
fun String.getLinkIfPresent(): String? {
    val m: Matcher = urlPattern.matcher(this)
    if (m.find()) {
        return m.group().trim()
    }

    return null
}
c
Personally, I see no issue with extensions on
String
. Being one of the most-used and most-flexible classes, it makes sense to wrap common String use-cases into an extension. Extensions on number or boolean types feels a bit more dirty to me
I’d probably avoid extensions on numbers or booleans in a library, but they might make more sense in application code, where there might be more of a business-logic reason for it
j
Extensions are fine. Depends on a use case. In my opinion in general I would rather avoid extensions that adds logic, it should be more like utilities. If seems like your extension is using local or global variable
urlPattern
. This may lead to Singletons which are bad practice.
g
Got it. Thank you guys