Kotlin / kotlin-style-guide

Work-in-progress notes for the Kotlin style guide
288 stars 14 forks source link

Using extension functions #7

Open yole opened 8 years ago

yole commented 8 years ago

Use extension functions liberally. Every time you have a function that works primarily on an object, consider making it an extension function accepting that object as a receiver. To minimize API pollution, restrict the visibility of extension functions as much as it makes sense. As necessary, use local extension functions, member extension functions, or top-level extension functions with private visibility.

cypressious commented 8 years ago

In my experience, making the receiver of extension functions nullable has little benefitbecause you can always call the function using the safe-call operator. This has the added benefit of making the call site easier to read because it makes obvious that the return value is nullable.

Exceptions:

cypressious commented 8 years ago

Related SO question: http://stackoverflow.com/questions/35317940/when-should-one-prefer-kotlin-extension-functions