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

pindab0ter

06/15/2019, 4:17 PM
Are there conventions for where to put extension functions and properties? I have a lot of calculated properties and functions for classes I can’t change because they’re part of a framework and such, spread over multiple sub-packages. Currently I have one big “Utilities.kt” file where they are all bunched together, separated into categories with only comments. This is file is starting to feel too big and bloated, but I can’t immediately think of a decent alternative. I don’t like the idea of cluttering my packages with lots and lots of tiny files with just a few extensions in htem. How do others deal with this?
d

Dominaezzz

06/15/2019, 4:22 PM
I tend to do one big file per class (or file, if the file has multiple classes). If I own the class then the extensions probably go in the same file.
a

Adam Powell

06/15/2019, 4:51 PM
I tend to organize them by purpose, what concepts they connect the receiver class to, etc.
If each file full of extensions has a small and focused set of imports that's usually a good sign, if the imports include a lot of assorted stuff from many different packages it probably wants to be broken up a bit
p

pindab0ter

06/15/2019, 8:50 PM
What kind of purposes do you group, generally?
a

Adam Powell

06/15/2019, 8:58 PM
right now I'm working on some code to do ssdp to talk to sonos and a few other things around the house, so I split out the actual I/O code to write/read the message types to/from okio BufferedSink/BufferedSource
and in another file I've got some code that opens a discovery session and establishes a suspending scope function for working within all of that
p

pindab0ter

06/16/2019, 2:19 PM
Thanks for your answers 🙂
👍 1
4 Views