Anyone have preferences for `sealed class/interfac...
# codingconventions
m
Anyone have preferences for
sealed class/interface
over top-level or nested inside declaration? 1️⃣
Copy code
sealed interface StoragePermissionsFileType {
    object Image : StoragePermissionsFileType
    object Video : StoragePermissionsFileType
    object Audio : StoragePermissionsFileType
    object Document : StoragePermissionsFileType
}
2️⃣
Copy code
sealed interface StoragePermissionsFileType 
object Image : StoragePermissionsFileType
object Video : StoragePermissionsFileType
object Audio : StoragePermissionsFileType
object Document : StoragePermissionsFileType
2️⃣ 1
1️⃣ 11
g
Mostly 1️⃣ , but it really depend on case. For this particular example I would definitely choose 1️⃣
🙏 1
👍 5
g
Hello Andrey, would you mind explaining a bit why you would definitely choose 1 in this case ? Im pretty new to kotlin so im having hard time to choose between sealed class/interface
l
On this one I don't particularly see any reason
So I could go either way depending on how I see these objects in the future
I think I like the second option more, as they'll introduce less spaces and brackets
🙏 2
g
as they’ll introduce less spaces and brackets
I think most prefer option 1 not because of spaces and brackets, but because of naming, So when you see/write in your code StoragePermissionsFileType.Image it clearly explains what it does, it’s storage permission for images. But if you just see Image it’s unclear what does it mean. There are valid use cases when you actually don’t want to have internal class because of naming reasons
between sealed class/interface
But both of those cases are sealed interfaces
👌 1
g
Thank you for your explanation Andrey!
m
I think I like the second option more, as they’ll introduce less spaces and brackets
In the other hands, I do consider about moving this class or renaming. When doing the nested ones option 1️⃣, changes affecting the whole caller-side, otherwise not for option 2️⃣
m
@gildor it would be nice if you could declare it at the top level without having to nest it. e.g.
class StoragePermissionsFileType.Image
g
It would be quite a strange declaration syntax imo This also works, though looks less readable
StoragePermissionsFileTypeImage
d
I guess that it is a simplification, if it is literally like this, an
enum
could also work