In our codebase have a singleton object, at file l...
# codingconventions
k
In our codebase have a singleton object, at file level, that implements a `fun interface`:
Copy code
object AtoBmapper : SomeMapperInterface {
    override fun map(a: A): B { ... }
}
SonarQube throws an issue with this, as it breaks rule kotlin:S6516. We can satisfy Sonar by changing it to this:
Copy code
val aToBmapper = SomeMapperInterface { ... }
But this has different semantics: instead of a singleton object, it's now a property (and it also has a different name). Is this suggested change good practice?
y
Semantics wise, it should be pretty similar I think. I personally would prefer the first, although it doesn't matter much
d
Alternatively, you could just make "fun mapAtoB(a: A): B { ... }" rather than an object.
The use-site would be a little different, but not by much.
Although I think I would rather disable/suppress that warning if possible, it doesn't seem that important for this particular use.
c
I personnally prefer the first because it's easier to have a custom
toString
to make debugging easier.
d
Rather than a custom toString, I'd just use a
data object
, but basically the same point 😉