Thoughts on using emojis for functions you want to...
# codingconventions
c
Thoughts on using emojis for functions you want to discourage developers from using but still having them there just in case? Running into a brick wall with some #mockk related things where we can’t properly mock some fields for a class we don’t control, so falling back to reflection which does work. Was thinking something like this:
Copy code
private inline fun <reified T : Any> T.`💩`(fieldName: String, fieldValue: Any) {
        val attribute = this::class.java.getDeclaredField(fieldName)
        attribute.isAccessible = true
        attribute.set(this, fieldValue)
    }

// call site
agent.`💩`("id", agentId)
💩 7
k
Can't you make it deprecated or something like that?
c
Yeah but there’s no intention to ever really remove it
A more mature member of our dev team suggested
setPrivateField
which is probably the most ideal. You don’t use
set
in Kotlin, and you don’t want to set a
private field
so it should hopefully raise enough red flags in code reviews to make sure we’re only using it as a last resort.
k
Deprecation is just the closest thing the language has: it'll be crossed out in the IDE and there are warnings for it. You can customize the message to explain what's going on.
b
If you are doing interop with java you might also want to add the
JvmSynthetic
annotation. That way it can’t be accessed from java.
g
Also, there is DeprecationLevel.Error, which even prevents it from to be compiled (works only for Kotlin code)
t
Have you considered just not mocking the class you don’t own (in itself that’s a bad idea), and putting a thin wrapper around it that you do control?
c
@tddmonkey - these models are auto-generated from an Open API Spec. We are converting those over to domain models that can be passed around the rest of our app so we have a wrapper, the main goal is to ensure we are mapping to the correct values so they are fairly small tests. MockK was unable to mock an
attributes
property so reflection ended up being the quickest route