https://kotlinlang.org logo
#announcements
Title
# announcements
l

LeoColman

11/20/2019, 4:14 PM
Hey guys! What's your usual take on having more than one class in the same file? I want to extract a piece of code from a complex class to a less-complex class and call it another service. However, this class will only be used by the class I'm extracting, and I don't really want to pollute the package namespace with a class that's not useful anywhere else. Should I keep it inside the same file?
👌 9
m

Milan Hruban

11/20/2019, 4:16 PM
I definitely would.
p

Perrankana

11/20/2019, 4:17 PM
l

LeoColman

11/20/2019, 4:17 PM
I'd usually do that as well, but I have people complaining about having 2 classes inside the same file
Placing multiple declarations (classes, top-level functions or properties) in the same Kotlin source file is encouraged
(...)
I think this sums my question up xD
👏 1
p

Perrankana

11/20/2019, 4:18 PM
hahaha yeah
c

czuckie

11/20/2019, 4:19 PM
people often feel uncomfortable when you break from tradition, but if you’d only ever delete both types at the same time 🤷‍♂️ why not be honest and keep them together
2
c

Casey Brooks

11/20/2019, 4:24 PM
I pretty much look at this in the same way I do with static inner classes in Java. I just move them to the top-level instead of keeping them inside the associated class
l

LeoColman

11/20/2019, 4:32 PM
What about their test classes? Would you keep both test classes at the same file as well?
c

Casey Brooks

11/20/2019, 4:37 PM
Yeah, probably. Typically, additional classes I create like this are just data classes and I don’t usually make unit tests for those
l

LeoColman

11/20/2019, 4:42 PM
For my specific case I have a part of the main flow of a class that has way too much rules and I'll extract those rules into another smaller and testable class
v

Vinicius Araujo

11/20/2019, 5:20 PM
A lot of people leave java, but Java doesnt leave them
😄 1
💯 3
v

Vishnu Haridas

11/21/2019, 4:36 AM
I write many classes in a single file. For example, a
MovieModels.kt
file will contain all the model classes for Movie, other related classes, and the movie list API request and response models.
3 Views