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
bachhuberdesign
07/06/2017, 4:15 PM
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 😅
bachhuberdesign
07/06/2017, 4:18 PM
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)
bachhuberdesign
07/06/2017, 4:19 PM
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
kevmoturi
07/06/2017, 4:32 PM
Same here I like them at the top... my IDE (Android Studio) throws them at the bottom when I convert my Java usually.
b
bachhuberdesign
07/06/2017, 5:56 PM
Same here, wish there was an option in AS/Kotlin plugin to flip it
z
zak.taccardi
07/07/2017, 10:05 PM
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