this just looks very wrong and too verbose for Kot...
# codereview
o
this just looks very wrong and too verbose for Kotlin, how can i make it better? https://hastebin.com/share/wotuquvuxi.kotlin
j
What's
ThumbnailFeedItem
? Is it another class within the sealed class?
o
externally defined interface that should've been sitting in this class
Copy code
interface ThumbnailFeedItem {
    val id: String
    val imageUrl: String?
    val title: String
    val subtitle: String?
    val apiType: String
}
c
FeedItem really should be an interface.
but to really make this better you would need to know more about the use case
j
One first thing that I would do is turn the
open
constructor properties of
FeedItem
into abstract properties instead, so there is less repetitions in super-constructor calls for the subtype declarations. Second is I would either remove the
FeedItem
suffix from the nested classes, or unnest those classes:
Copy code
sealed class FeedItem : Serializable {
    abstract val id: String
    abstract val title: String
    abstract val status: FeedItemStatus
    /** The GraphQL type (needed for analytics tracking) */
    abstract val apiType: String

    data class ThumbnailWithUrl(
        override val id: String,
        override val status: FeedItemStatus,
        override val title: String,
        override val subtitle: String?,
        val url: String,
        override val imageUrl: String,
        override val apiType: String
    ) : FeedItem(), ThumbnailFeedItem

    data class Blog(
        override val id: String,
        override val status: FeedItemStatus,
        override val title: String,
        override val subtitle: String?,
        val eventTitle: String?,
        val eventSubtitle: String?,
        val eventImage: String?,
        val actionText: String?,
        val actionUrl: String?,
        override val imageUrl: String,
        val authorName: String,
        val contentHtml: String,
        val createdAt: OffsetDateTime,
        val items: List<CollectionItem>,
        override val apiType: String
    ) : FeedItem(), Serializable, ThumbnailFeedItem
}
But as Christopher said, it could really be a sealed interface instead, and that would also solve the problem:
Copy code
sealed interface FeedItem : Serializable {
    val id: String
    val title: String
    val status: FeedItemStatus
    /** The GraphQL type (needed for analytics tracking) */
    val apiType: String

    data class ThumbnailWithUrl(
        override val id: String,
        override val status: FeedItemStatus,
        override val title: String,
        override val subtitle: String?,
        val url: String,
        override val imageUrl: String,
        override val apiType: String
    ) : FeedItem, ThumbnailFeedItem

    data class Blog(
        override val id: String,
        override val status: FeedItemStatus,
        override val title: String,
        override val subtitle: String?,
        val eventTitle: String?,
        val eventSubtitle: String?,
        val eventImage: String?,
        val actionText: String?,
        val actionUrl: String?,
        override val imageUrl: String,
        val authorName: String,
        val contentHtml: String,
        val createdAt: OffsetDateTime,
        val items: List<CollectionItem>,
        override val apiType: String
    ) : FeedItem, Serializable, ThumbnailFeedItem
}
o
thank you this is great, gonna look at how theyre used and see if i can slim down some repetitive stuff
and shouldn't the fact that FeedItem itself being Serializable mean that subclasses don't need to also implement Serializable?
c
true, but why is it even serializable?
o
to be sent as arguments
j
yes, I guess you could remove that interface from
Blog
.
Is this
java.io.Serializable
?
o
yea
c
why not kotlinx.serializable instead?
j
I believe this
java.io.Serializable
interface is very likely not what you want. Are you using Java binary serialization anywhere here?
c
without knowing anything about this code my guess is that java.io.serializable is the biggest problem here
thats a heuristic thats probably always true 🙂
o
makes sense!
c
kotlinx.serialization works great with sealed classes. you could just use json because having readable data is always great, but protobuf is also great
c
TIL hastebin
o
yea toptal bought them a while ago