How should one store constants in a Kotlin idiomatic style?

Hello. Some background: I am learning Kotlin and using it now to build a small command-line app at work. I do not have a Java background.

I’ve been reading about how to store constants in a project. There are several threads on different websites that recommend storing constants in the classes where they’ll be used, for example. I’ve also seen suggestions to store constants in a singleton object.

I get the feeling that a lot of the suggestions come from users who have Java backgrounds. Some of what they say is a bit confusing to me. For instance, what is the benefit of storing constants in a singleton object in Kotlin? If I were to organise my project into meaningful packages, and put the constants in those packages, wouldn’t I be able to easily reference them without the need for a singleton? Furthermore, Kotlin doesn’t require that every object be associated with a class, but perhaps I am missing something here due to a lack of experience.

The most general thread I’ve seen so far appears to be this one on Stack Overflow (even if it is categorised under Java). Would the answer by the user Chris K be what’s recommended for Kotlin too?

My concern is with storing constants in a manner that is idiomatic to the language; if it happens to overlap with how it’s done in Java, then that’s fine too.

Thanks!

I don’t think there’s a single answer, but a few points worth considering:

  • Scope: how much code will need to access these values? One class? One file? One package? One module? Multiple modules?
  • Variability: how often might these values need to change? Are they guaranteed to remain the same for ever, or might you need to tweak them every few years, or more often? Might they vary between platforms/languages/etc.? Might there be cases where other users would benefit from being able to change them?
  • Structure: are any of these values connected in any way, or are they all independent?
  • Type: are these simple strings or ints, or anything more complex?

The answers to those will of course influence the best ways to store constants. (That could include storage in a database, application properties or other properties file(s), custom data file(s), Kotlin top-level properties, class properties, maps, enum values, sealed class instances, etc.)

2 Likes

Definitely coming from a Java background, initially when using Kotlin, people try to store variables/functions in objects or classes. (I know, because I did the same thing. :stuck_out_tongue: ) In Kotlin, since you can put variables/functions directly into files, you don’t need some kind of static singleton class where you store all your static constants. Just put them directly in files.

1 Like

If you want to mark the constant as const then you can only do that at the top level of a file, in named objects, or in companion objects. So one of those places.

1 Like

Thanks a lot, @gidds and @Skater901! I’d hoped that the issue would be a more straightforward one, but it seems that there’s plenty to keep in mind. I’ll start by moving my constants out of my singleton object, at least!

I also struggle to find the most idiomatic pattern. However, you might want to consider differences in import statements. If you put your constants in a file as top level fields, the import for it will only contain the package and the field name. An import will never contain the file name. If you want to group some constants together for different functions, you will either need more packages or you will need to move them into eg a singleton object.

1 Like