I was defining a variables const and I have doubts...
# announcements
f
I was defining a variables const and I have doubts about good practices and most scalable design. What's a recommended ways? My Samples:Method 1: Create Global without Wrapper. cons: No context and need added in name of variable. • Method 2: Create in Singleton Object.
Copy code
# Method 1: 
private const val API_PATH = "<http://localhost:8080>"
const val WELCOME_ENDPOINT = "$API_PATH/welcome"

# Method 2:
object EndPoints {
  private const val API_PATH = "<http://localhost:8080>"
  const val WELCOME = API_PATH + "/welcome"
}
v
You can put the context in the package name
y
Currently I'd say that if you do need the context for clarity then use the object method, and if you don't really need it then just have an appropriately-named package. In the future, however, `namespace`s will be added and so you can define this just as:
Copy code
namespace EndPoints {
  private const val API_PATH = "<http://localhost:8080>"
  const val WELCOME = API_PATH + "/welcome"
}
😀 1
f
@Youssef Shoaib [MOD], I just read about namespaces and I liked them. Do you know if Jetbrains is working to implement it in Kotlin?
Currently I'd say that if you do need the context for clarity then use the object method, and if you don't really need it then just have an appropriately-named package.
I'm going to continue adding context to the name for now to see if I run into complications.
y
They are planning on implementing them according to this Reddit AMA comment and

this Youtube video by Roman Elizarov

m
In general, consider if you really need global constants (top level or object), and if they can be encapsulated by the class that needs them, so as to reduce coupling. https://www.yegor256.com/2015/07/06/public-static-literals.html