Are there conventions for where to put extension f...
# announcements
p
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
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
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
What kind of purposes do you group, generally?
a
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
Thanks for your answers 🙂
👍 1