is it bad or good practice to use global variables representing "services", like this one:
Copy code
val esClient: RestHighLevelClient by lazy {
createEsClient(
hosts = arrayOf(HttpHost("localhost", 9200, "http"))
)
}
t
Timmy
02/11/2019, 12:37 PM
Usually global variables are considered bad practice. The main reasons is testability, i.e. what if you need a different client for your unit tests? Another argument against global variables is scope - you have no control who uses it.
Often some sort of DI is used instead (either with a framework or manually). This page has some more in depth reasons and alternatives: http://wiki.c2.com/?GlobalVariablesAreBad