https://kotlinlang.org logo
c

Cicero

07/21/2021, 12:15 PM
I came here to cry a little bit about interfaces.
I'm currently in a world where Interfaces are being used just to make references to classes instead of being used to assure implementation of the interface, let me present you an example
Copy code
interface DumbInterface {

    fun one()

    fun two()
}

class DumbInterfaceImpl : DumbInterface {

    override fun one() {
        println("something")
    }

    override fun two() {
        println("else")
    }
}
And when I have something like this is acceptable:
Copy code
interface NetworkErrorManager {

    fun handleRegistrationError(statusCode: Int, errorBody: ResponseBody?): ApiException

    fun handleResetPasswordError(statusCode: Int, errorBody: ResponseBody?): ApiException

    fun handleLoginError(statusCode: Int, errorBody: ResponseBody?): ApiException

    fun handleGetUserProfileError(statusCode: Int, errorBody: ResponseBody?): ApiException

    fun handleUpdateUserProfileError(statusCode: Int, errorBody: ResponseBody?): ApiException

    fun handleResendConfirmationEmailError(statusCode: Int, errorBody: ResponseBody?): ApiException

    fun handleRefreshUserTokenError(statusCode: Int, errorBody: ResponseBody?): ApiException

    fun handleLogoutError(statusCode: Int, errorBody: ResponseBody?): ApiException

    fun handleDeleteUserError(statusCode: Int, errorBody: ResponseBody?): ApiException
}
I've been trying to get people not to use interfaces when they are not being used as interfaces and just class references -- EXAMPLE: class JustDoingSomethingDumb(iNeedThisOtherDumbClass: DumbInterface ) instead of DumbInterfaceImpl -- and to use interfaces when we have easily recognizable patterns like this above and being receive with poker faces. When for exanmple, for the last example, I proposed something like:
Copy code
interface Network{
    val statusCode: Int
    val errorBody: ResponseBody?
    val errorMessage: String
        get() = errorBody?.toString() ?: NetworkErrorManagerImpl.UNKNOWN_ERROR_MESSAGE

    fun get(): ApiException
}
sealed class NetworkImpl: Network{

    data class HandleRegistrationError(
        override val statusCode: Int,
        override val errorBody: ResponseBody?,
        override val errorMessage: String
    ) :NetworkImpl() {
        override fun get(): ApiException {
            return when (statusCode) {
                403 -> InvalidClient(
                    statusCode,
                    errorMessage
                )
                else -> handleOtherErrors(statusCode, errorMessage)
            }
        }
    }
}
Not saying the last one is the best but I needed some compromises as this NetwrokImpl holds handleOtherErrors which is just interesting for the realm of NetworkImpl
PS: NetworkErrorManager methods are not being implemented they are being used individually like NetworkErrorManagerImpl.handleRegistrationError(dumbParametersHere)
m

marlonlom

07/21/2021, 10:13 PM
are you trying to create an backend-like api frmo the android side of things?
c

Cicero

07/22/2021, 8:18 AM
No, just annoyed by the way people use interfaces.
5 Views