Kotlin functions, An alternative to interfaces?

A. A.
ProAndroidDev
Published in
3 min readMar 22, 2019

--

So we all know interfaces and we make use them a lot when we want to implement a callback. So the code below is familiar to us.

In the above code, we simply define an interface and let UserClass send an implementation of that interface to us. This is the way we used to do it in Java for years but now Kotlin is here, should we do the same in Kotlin?

What’s different in Kotlin?

In Kotlin functions are first-class which means that they can be stored in variables and data structures, passed as arguments to and returned from other higher-order functions. A higher-order function is a function that takes functions as parameters, or returns a function. Read more in official doc here goo.gl/GqArF9

Look at the code below

This code does exactly the same thing as previous code but with only using functions. So instead of passing an implementation of interfaces, we pass functions themselves to the class. Also, note that by providing an empty method as default value with = {} we easily made the parameter optional.

Directly using functions has the following advantages

  • Using functions is simpler and uses less boilerplate code which is always good.
  • A common problem using interfaces is that the client may not use all the callbacks but is still forced to implement them all and this goes against one of the SOLID principles.

Interface-segregation principle
the interface-segregation principle (ISP) states that no client should be forced to depend on methods it does not use.

Of course, there are workarounds to solve this like giving interface default implementation or split an interface to two or more interface but with functions, it is much easier, we can make all callbacks optional and let the client code (which may be developed in future by some guy we don’ t know) decide for itself witch one of our callbacks it wants to use and which one to ignore!

  • Forwarding the callback is much easier. In an application sometimes the UserClass doesn’t need to do anything except forwarding the event to another class or another layer. Let’s say in our example we have another class called LogicHandlingClass that is responsible for doing the work. by using functions we can do something like this

If we wanted to use an interface we had two choices, implement the interface in UserClass and then call logicHandlingClass.doThis() I don’t like this because this is clearly boilerplate code! or we could let LogicHandlingClass to implement the interface, I don’t like this either because now our LogicHandlingClass has to know about the interface! ‌But in the code above, we don’t have any of these problems.

What about the disadvantages?

  • Directly using functions instead of interfaces may result in functions and constructors with so many parameters and that may be confusing in some cases.
  • Using interface is a more explicit way of coding since an interface has a clear name and an identity.
  • Sometimes it's better to group functions that relate to each other and an interface is a perfect place for that.

Conclusion

To conclude I want to emphasize that, this post is not meant to discourage you to use interfaces but rather provide an alternative and in my opinion perfectly valid solution you can consider while coding.

Also, don’t forget to Clap and share your ideas in the comment section below. Follow Me if you’re interested to read my other posts as well.

--

--