https://kotlinlang.org logo
#getting-started
Title
# getting-started
a

anlex N

03/24/2024, 8:55 AM
https://kotlinlang.org/docs/sealed-classes.html#constructors these two code styles have the same result. why is white picture's code style better than black picture's one?
h

hfhbd

03/24/2024, 9:02 AM
Please make it clear, what part exactly do you mean. I guess the sealed class: It just act like namespace when putting the definition of the classes into the body of the
Error
class so you don't clutter your package namespace with classes related to
Error
. BTW if the class holds no state, you could also use a
data object
instead.
a

anlex N

03/24/2024, 9:06 AM
docs by itself, have two code styles. I am very crazy.
j

Joffrey

03/24/2024, 9:44 AM
A class can be at the top level or nested inside another class. If it's nested, its name is different: its fully qualified name includes the name of the containing class. Usually, we refer to nested classes using
Containing.Nested
so nesting a class is just a different way to namespace it. Sometimes it makes more sense this way, it depends on the case.
a

anlex N

03/24/2024, 9:52 AM
so it have no little benefits, only personal taste, right?
j

Joffrey

03/24/2024, 11:20 AM
We can consider it taste, but it changes a little bit the experience. For instance, you can have auto-completion in the IDE more easily if you type the containing class name, then a dot
g

gildor

03/26/2024, 7:21 AM
It's not about taste, it depends on different use cases, would you like to group them or not and what kind of name you would like for this case, practice shows that both styles used side by side
1