Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin benifits of writing helper/util methods without wrapping in class

There are can be two ways of writing helper method in Kotlin

First is

object Helper {
    fun doSomething(a: Any, b: Any): Any {
        // Do some businesss logic and return result
    }
}

Or simply writing this

fun doSomething(a: Any, b: Any): Any {
    // Do some businesss logic and return result
}

inside a Helper.kt class.

So my question is in terms of performance and maintainability which is better and why?

like image 930
Abhishek Avatar asked Aug 16 '18 14:08

Abhishek


People also ask

Why do we use helper classes?

In object-oriented programming, a helper class is used to assist in providing some functionality, which isn't the main goal of the application or class in which it is used. An instance of a helper class is called a helper object (for example, in the delegation pattern).

How do I write a utility class in Kotlin?

TL;DR: You can create utils class by putting methods inside an object , or use package-level functions. If you're integrating with Java code and need a true static utils method, you can put this method inside an object and annotate it with @JvmStatic .

What is package-level function in Kotlin?

Package-level functions are also known as top-level functions. They are declared directly inside a file without creating any class for them.

When would you use Kotlin extension function?

In particular, Kotlin extensions let you add functions to a class that you cannot modify. By using them, you will be able to call these new functions as if they were part of the original class. Similarly, you can use this mechanism to add new properties to existing classes. You can also extend Kotlin companion objects.


2 Answers

In general, your first choice should be top-level functions. If a function has a clear "primary" argument, you can make it even more idiomatic by extracting it as the receiver of an extension function.

The object is nothing more than a holder of the namespace of its member functions. If you find that you have several groups of functions that you want to categorize, you can create several objects for them so you can qualify the calls with the object's name. There's little beyond this going in their favor in this role.

object as a language feature makes a lot more sense when it implements a well-known interface.

like image 123
Marko Topolnik Avatar answered Oct 12 '22 13:10

Marko Topolnik


There's a third and arguably more idiomatic way: extension functions.

fun Int.add(b: Int): Int = this + b

And to use it:

val x = 1
val y = x.add(3) // 4

val z = 1.add(3) // 4

In terms of maintainability, I find extension functions just as easy to maintain as top-level functions or helper classes. I'm not a big fan of helper classes because they end up acquiring a lot of cruft over time (things people swear we'll reuse but never do, oddball variants of what we already have for special use cases, etc).

In terms of performance, these are all going to resolve more or less the same way - statically. The Kotlin compiler is effectively going to compile all of these down to the same java code - a class with a static method.

like image 30
Todd Avatar answered Oct 12 '22 12:10

Todd