https://kotlinlang.org logo
#codereview
Title
# codereview
t

Tuang

05/22/2020, 9:46 AM
hi guys, i needs some review for my code.
Copy code
fun A(infoA: Info, conn: Connection, userId: Int): Result {
        val overSettingOfA: OverSetting = AppSetting.instance.retrieveA(infoA, conn, userId)
        // some code
        return Result(overSettingOfA)
    }



    fun B(infoB: Info, conn: Connection, userId: Int, someInfo: SomeInfo): Result {
        val overSettingOfB: OverSetting = AppSetting.instance.retrieveB(infoB, userId, conn, someInfo)
        // some code
        return Result(overSettingOfB)
    }
Both of 
fun A
 and 
fun B
 have the same logic, what they are doing is all the same. I m trying to do likes high order function but the problem is the argument is not the same, 
fun A
 has 3 arg and 
fun B
 has 4. Is there any a magical way? 😀
m

Milan Hruban

05/22/2020, 9:49 AM
is the type of overSettingOfA and overSettingOfB same?
t

Tuang

05/22/2020, 9:49 AM
yeah they have the same type
b

Bornyls Deen

05/22/2020, 10:02 AM
Could
A
and
retrieveA
accept
someInfo
as an optional/nullable parameter?
Assuming
retrieveA
and
retrieveB
are duplicated in a similar way as `A`/`B` here.
t

Tuang

05/22/2020, 10:05 AM
@Bornyls Deen
Could 
A
 and 
retrieveA
 accept 
someInfo
 as an optional/nullable parameter?
unfortunately No. because what
retrieveA
and
retrieveB
doing is not the same, i means they are doing different things
that’s why the parameter is different
👍 1
b

Bornyls Deen

05/22/2020, 10:06 AM
Copy code
fun A(infoA: Info, conn: Connection, userId: Int, someInfo: SomeInfo? = null): Result {
    return if (someInfo == null) {
        Result(AppSetting.instance.retrieveA(infoA, conn, userId))
    } else {
        Result(AppSetting.instance.retrieveB(infoA, conn, userId, someInfo))
    }
}
👍 1
maybe something like this would work then
t

Tuang

05/22/2020, 10:08 AM
umm,,,not too bad idea 😀
b

Bornyls Deen

05/22/2020, 10:09 AM
Just a thought. It might be better to update the retrieve method to handle the case of
SomeInfo
being present/missing though in a similar way if it is under your control 😄
👍 1
t

Tuang

05/22/2020, 10:10 AM
yeah, i think so…. thanks
b

Bornyls Deen

05/22/2020, 10:12 AM
no problem 🙂
3 Views