<@U2E974ELT> any good articles discussing “Kotlin-...
# codingconventions
h
@elizarov any good articles discussing “Kotlin-style design and prefer extensions over members”?
c
main principle is, make your classes lean - with only basic properties and functions, and implement additional module specific functionality via extension functions in respective modules.
For example if you have a
User
and you want to calculate a discount for one, you could write a member function
calculateDiscount
or in the mentioned style, you would have
user-pricing
module where you'd have
User.calculateDiscount
extension function.
This way you're able to manage concerns separation much better than without it. In Java to achieve the same you'd use services and/or utility classes, which would not be as readable:
user.calculateDiscount(TODAY)
vs
DiscountCalculator.calculateDiscount(user, TODAY)
This is only a synthetic example, but I hope it conveys the main idea.
Here's a much more elaborate and complete explanation of when to use extension functions: https://stackoverflow.com/questions/35317940/when-should-one-prefer-kotlin-extension-functions?answertab=votes#tab-top
w
That makes sense. What about in the context of writing a DSL?
h
That stackoverflow thread is the best I’ve found in the topic as well