Where should companion objects go in the file? Top...
# codingconventions
k
Where should companion objects go in the file? Top or the bottom? Also coming from java... how are we supposed to treat files? should i just keep all related classes in one file?
b
kevmoturi: Treat files exactly the same way you would as Java (i.e. separate when it makes sense, together when it makes sense). I keep companion objects at the top but it’s personal preference, I know a lot of Kotlin developers like having them at the bottom. Just don’t keep them in the middle 😅
If I was writing Java my statics/constants would always be at the top — so I like to have the same style in Kotlin (except they are part of the companion object)
For extension functions, I like to have a separate file for each class that is being extended (same as Swift). So
ActivityExtensions.kt
,
ContextExtensions.kt
,
ViewExtensions.kt
, etc etc. Much easier to go through if someone else is looking at your code
k
Same here I like them at the top... my IDE (Android Studio) throws them at the bottom when I convert my Java usually.
b
Same here, wish there was an option in AS/Kotlin plugin to flip it
z
I would say bottom because you get visual separation of static vs instance members. primary constructor is always at top and that’s instance based