Hi all not really sure if this is the right place ...
# getting-started
h
Hi all not really sure if this is the right place to ask but I have some design questions/ best practice questions that I have. So essentially I was told that singleton / static variables should not be used for the business logic in my application. So essentially what I have is something like
Copy code
class someClass(){

lateinit var one: String,
lateinit var two: String,
lateinit var three: customObject

}
So what I'm doing is throughout my code I'm updating these variables based on some conditions/criteria and I have another class that calls
someClass
and I will use getters to retrieve the
lateinit var
. Instead of these singleton variables, what should I be using? Should I be using a custom
data class
to store the state of the variables? The logic I have for these variables: one - Does the data already exist? yes -> one = existingData.one no -> one = generate some value and set two - Does the data already exist? yes -> two = existingData.two no -> two = generate some value and set && Are we done? yes -> no nothing. no -> generate some value and set three - Just update consistently to get the most recent Object to be passed into different class
a
It's good practice--in Kotlin especially--to practice immutability. You can get most of the way there with
lazy
delegated properties. These properties will evaluate the
lazy
block the first time they're accessed, and cache the result for future accesses.
Copy code
class SomeClass {
  val one by lazy {
    "foo" + "bar" + "baz"
  }
}
Rather than have other modules mutate the properties of
SomeClass
, you might inject whatever dependencies you need into
SomeClass
so it can compute the values itself. https://kotlinlang.org/docs/delegated-properties.html
h
What do you mean by inject? Like autowiring?
s
yes, injecting dependencies and autowiring dependencies is the same.