I don\'t understand why is possible to write a function outside a class in Kotlin ? Is that a good practice ?
For example, it\'s possible in Kotlin to write a funct
Yes, it is a good practice to create package-level functions if the function logic is independent of properties and lifecycle of a class. Example:
The main benefit of a package-level function is simplicity (ergo better maintainability): callers of your function don't need to declare and create an object to call the function. (If your package-level function needs to be called from Java code, this benefit is lost because the Java calling code has to use a class name that is generated by Kotlin.)
IMPORTANT: Although you don't have a class lexical scope for your function, the Single-Responsibility Principle (SRP) still applies. Do not create a Kotlin source file, say Util.kt, and bloat it up with functions that lack cohesion, that is, functions that do unrelated things.