what is the good way to writing this code: ```over...
# codereview
k
what is the good way to writing this code:
Copy code
override fun getOrderInfoList(status: String, chainID: String, debugTag: String): Flowable<Result<List<OrderInfo>>> {
    logger.d(TAG, "Getting order list for status %s", status)
    val fetchFromStorageObservable = storageDataSource.getOrderList(debugTag)
    val observeStorageObservable = storageDataSource.observeOrderList(debugTag)
    // function which fetches from the local storage and takes a throwable that might be re-thrown if there's no data found locally
    val fetchFromStorageOrThrowObservable = fun (t : Throwable) : Observable<Result<List<OrderInfo>>> {
        fetchFromStorageObservable.map {
            if(it.isEmpty()) {
                // re-throw the exception from the Remote source since we got no data from the Local source
                throw t
            }
            it
        }
    }
    val fetchFromRemoteObservable = remoteDataSource.getOrderInfoList(status, chainID).toObservable().onErrorResumeNext { t: Throwable ->
        logger.d(TAG, "order list not received from server")
        // fallback to the local source
        fetchFromStorageOrThrowObservable(t)
    }.flatMap {
        if (it is Result.Success) {
            persisOrderInfoList(it.data)
        }
        fetchFromStorageObservable
    }
    return Observable.concat(fetchFromRemoteObservable, observeStorageObservable).toFlowable(BackpressureStrategy.LATEST)
}
k
There are a lot of classes and classes where I don't have any information about
Like what is the
it
in the
fetchFromStorageObservable.map
. Is
it
the list?
And what type is
fetchFromStorageObservable
?
Also the function for
fetchFromStorageOrThrowObservable
doesn't have a return statement?
Also
Result.Success
doesn't exist. Is this a self-defined type?
k
“what is the
it
in the
fetchFromStorageObservable.map
. Is
it
the list? ” yes it is List<OrderInfo> “what type is `fetchFromStorageObservable`”? Observable “Also the function for
fetchFromStorageOrThrowObservable
doesn’t have a return statement”? true, my bad, forgot to add return statement
"Result.Success
doesn’t exist. Is this a self-defined type”? yes it is self-defined type which is like:
Copy code
sealed class Result<out T : Any> {

    data class Success<out T : Any>(val data: T) : Result<T>()
    data class Error(val exception: Exception) : Result<Nothing>()

    override fun toString(): String {
        return when (this) {
            is Success<*> -> "Success[data=$data]"
            is Error -> "Error[exception=$exception]"
        }
    }
}
m
k
Because
getOrderInfoList
returns a
Result
I think, which is not possible with a Kotlin
Result
.
It seems I made all the right guesses. Though does this mean that
map
is an extension-function defined on
Observable<Result<T>>
?
k
yup true Robbe
my point here is any other way to write better code than what i shared?