Android Dev Tips: Kotlin get

Michael Febrianto
2 min readFeb 20, 2023
Photo by Isabella and Zsa Fischer on Unsplash

Software engineers like simple code. Simpler code means they are efficient, faster to run and more importantly is easier to read.

Any fool can write code that a computer can understand. Good programmers write code that humans can understand.- Martin Fowler

Kotlin as de facto code for android development has several tricks to shorten code. However, we need to really understand the different purpose for each of them. We do not want inadvertently produce bug.

I am sorry for the pop culture reference. getRepulsorsStatus method can be shorten to val repulsorsStatus. Another way to shorten the code is by using get() method as shown below.

Both of them will work. What is the different then ? Why we need get ? You will know when you run the method. Code at IronManMk2 will run without issue. However, if I add another method to debug IronManMk1 code

After that I call the shoot method. It will return with this response:

Jarvis we have a bug. Iron Man need to return to Avengers base to be fixed. What is actually happen ?

When we initiate IronMan class it will initiate the repulsorsStatus variable. This call is part of Kotlin class initialization. Therefore, It is important to put get() as it will make sure the code only called when it is needed and not produce bug.

That’s all for now. Hope you enjoy the article. God bless you all.

--

--

Michael Febrianto

Bible History buff, food vlogger and Software Engineer