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

oday

11/13/2018, 2:36 PM
how can I fix this? 😄 https://pastebin.com/9iLG5E6B
a

Andreas Sinz

11/13/2018, 4:26 PM
@oday what do you mean by fix? make it more idiomatic?
o

oday

11/13/2018, 4:43 PM
One minute
Ok @Andreas Sinz well yes I know there's no glaring issue in the code and it isn't wrong to do this, but it seems so exhaustive, and imagine like this method there are many others, each handling tracking
And it just looks ugly
For example is there a way that I can look for an object's properties and find a property by a string?
If this property's name is "country_code"
Im not sure really how to improve on this but it's definitely too exhaustive looking and ugly
a

Andreas Sinz

11/13/2018, 5:21 PM
you could look for properties based by their name with reflection, but it looks like you aren't even using any properties of lets say
mpProperties
o

oday

11/13/2018, 5:24 PM
it’s the issue that the name of the property matches the name of the field i will send to mixpanel
yea…not much value added if there’s no loop, and you can’t loop if you’re only selecting a limited number of properties from the object
i’ll come back with a clearer problem
a

Andreas Sinz

11/13/2018, 5:58 PM
you can make the code cleaner with
with(mpProperties) { ... }
g

gildor

11/14/2018, 2:01 AM
For example is there a way that I can look for an object’s properties and find a property by a string?
Only using reflection or code generation. Also kotlin.serialization can do this for you
So this is only for fully automatic solution
but you can improve your code just using apply on
mpProperties
or even writing some dsl like:
Copy code
mpProperties.putAll {
   "car_id" to listing.id
}
o

oday

11/14/2018, 12:18 PM
I see
ok, yea either full automatic or not, i noticed
i will ask more about this DSL if you dont mind, later 🙂
I actually asked this in #getting-started but I don’t think it falls into what they can help with how can I print the name of any variable?
var1.javaClass.getFieldByName(String)
is what im using right now but im not sure what to feed
getFieldByName
im in the process of fixing what you suggested the other day
g

gildor

11/18/2018, 5:27 AM
Do you want to use reflections for that?
Not sure what exactly do.ypu want to achive, because there are many different ways to implement such mapping with reflections
3 Views