Kotlin Functions vs. Properties: When to use what?

Sriyank Siddhartha
Smartherd
Published in
3 min readApr 9, 2022

--

Learn when to use functions and when to use read-only properties in your Kotlin class.

If you don’t prefer to read, you can consume this article in the form of a video. 👇👇👇 Or click here to watch

It’s okay to watch videos sometimes. Try it!

Okay, let’s get started.

You should always prefer to use read-only properties (using val keyword) over using functions if your code block meets all the following criteria:

  • No arguments (function parameters) are required
  • Throwing an exception is not required
  • Your code block performs Cheap (simple) computation
  • Your code block returns the same result when executed (provided the object state hasn’t changed)

If any one of the above-mentioned criteria is not met, then blindly use a function.

Let’s understand it using an example.

Let’s try this way. Given below is a class Box that contains two code blocks. Code block 1 is a getVolume function and Code block 2 is a read-only property with a getter.

Your task is to identify if both the code blocks need to change as per the 4 criteria mentioned above or not?

class Box (val width: Int, val height: Int, val length: Int) {    var usedSpace: Int = 0     // Initially box is empty    // code block 1 
fun getVolume(): Int {
return width * height * length
}
// code block 2
val availableSpace: Int
get() = volume – usedSpace
}

Solution

class Box (val width: Int, val height: Int, val length: Int) {    var usedSpace: Int = 0     // Initially box is empty    // Replace code block 1 by this read-only property 
val volume: Int
get() = width * height * length
// Replace code block 2 by this function
fun getAvailableSpace(): Int {
return volume – usedSpace
}
}

Explanation for Code Block 1

Code Block 1 originally was getVolume function. This code meets all the 4 criteria mentioned above. So we replaced it with a read-only property.

  • It had no function parameters.
  • It does not throw any exception.
  • It does a simple calculation to find the volume of box.
  • width, height, and length are initialised during the box object creation. Hence each time the getVolume method is called, it always returns the same result.

Therefore, Code Block 1 should use a read-only property instead of a function.

Explanation for Code Block 2

Code Block 2 was originally a property.

  • It meets the first three criteria but fails to conform to the last criteria. i.e. Each time it is called, it might return a different value depending on the value of usedSpace property.

Hence, we should use a function for this code instead of using a read-only property.

How does making such a change affect your application performance?

Well, it does not. It is just a code convention for better Kotlin programming.

And this article is based on the Coding Convention article on the official kotlinlang.org website.

I hope you enjoyed this article. One clap, two claps, three claps…. Forty!

If you want more such tutorials, subscribe to my youtube channel Smartherd. 100 thousand already did. Don’t wait!

--

--

Sriyank Siddhartha
Smartherd

Tech enthusiast, love coding, and love to make videos on my YouTube channel Smartherd.