The lazy properties have been used in many languages before, including Java. Many developers are keen to use these properties. In Kotlin too, developers have found a way to customize the properties after installation. Such properties are the lazy ones! Developers tend to delay costly changes after installation. In Kotlin, delegated properties and lazy properties come into the picture and make the job easy for developers. In Java and broadly, Android app development, developers can use some approaches to build a lazy creation. Especially in Kotlin app development, the lazy properties have been introduced as part of the language. There are new syntax and different options for initialization plus the situations where they can be useful. Let’s take a look at those properties with an app built with Kotlin technology.
But, before we continue to tell you about those properties with an app build with Kotlin technology, Let’s see Some delegated properties in Kotlin:
Delegated properties– There are some properties that we can implement manually, but it would be nice if we apply once and for all, and put them into a library. You can declare the property and the delegate that it uses by using delegated properties. Here are some of the examples-
- Lazy properties– The value gets computed only on first access.
- Observable Properties– Whatever changes made to this property, listeners will get notified about every change.
- Instead of storing properties in a separate field, each property must be stored in a map.
To cover these and other cases, Kotlin supports delegated properties:
The Syntax is val/var <property names>: <Type> by <expressions>. The “expression” is the delegate, because get() (and set () ) corresponding to the property will be delegated to its getValue () and setValue () methods. You don’t need to implement delegate property in any interface, but they have to implement to give a getvalue() function (and setValue()- for vars).
Standard Delegated Properties
The standard library of Kotlin comes with a set of standard delegates and Here are the names of the following standard delegated properties-
- Lazy- The lazy delegate allows the value of a property to be computed only on first access and then cached.
- Observable- The value of the property changes whenever the observable delegate allows for a lambda to be triggered.
Now, Take a look at those properties with an app build with Kotlin technology:
Creating Lazy Property
In Kotlin, lazy property can be created simply and in a secure way. Using “by lazy,” a developer can define it and provide a function name. At the time of access, it is initialized by the function name, and then on future usage, the cached value will come to as output. However, you can add additional properties as much as you require. But, try not to use delegated properties!
Creating a lazy property is simple and easy. But, it is not always the case that it will be as simple as calling an initializer. It may require more coding to set lazy properties. In these situations, developers move the lazy function’s content to a factory method on the outer class.
There are numerous situations when lazy properties are required and are to be used. To merely the call sites, developers tend to use an extension function. While using Android ViewModel, all the Activities need to recoup their View Model. This can be done using an extension function on a standard base class.
How ‘by lazy’ Does Work?
There are many different delegated property types, and developers can write their own delegate property too. After the “by” keyword, the expression a developer writes is a delegate that will be used. While in lazy property, a laze delegate can control the access of the developer’s property, which is initialized on first access and return caught value.
If we summarize lazy() in the standard library reference of Kotlin then here is how lazy works-
- Lazy creates an instance that stored lambda initializer
- The first call of getter executed a lambda and passed to lazy(), storing its result.
- Finally, calls to get () returns the stored value.
The lazy function carries an argument with a default value that controls its synchronization behavior. A lazy property can be accessed from multiple threads at the same time. That’s why the synchronization process will need to be handled by choosing a “LazyThreadSafetyMode.”
Every non-nullable class property in Kotlin, which is declared in the class, gets assigned in the constructor. Else, developers tend to get a compiler error. However, there are cases where a variable can be assigned dynamically.
If a developer wants to defer the initialization of the variable, he/she can specify that field as lateinit. By this, we tell the compiler that the variable will be assigned later, and the compiler makes sure that the variable gets initialized in later stages.
Other Benefits of Lazy function
A lazy function can be used wherever a developer wants to delay initialization rather than doing it at the same point. If a developer doesn’t want to do it like that, other options like a “lateinit” property, which promises to be initialized before it is used.
Acquire Function Dependency
If a developer wants to delay the initialization, a lazy approach can be efficient and better than its alternatives. It can be helpful for acquiring dependencies of the functions whose boot is out of our control like Android View Models from an Activity or Fragment.
Reading Values Efficiently
Many times, a developer needs to read a value from somewhere in code, but he/she doesn’t want the value to be retrieved each time when it is required. Reading Bundle extras from an Intent is a solid example of it, In it, you can use a lazy property on first access where the value is read from the Intent and cached for quicker access for later use.
Kotlin’s Delegated Properties
No doubt, sometimes, lazy-initialization from Kotlin can cause a problem to occur. It also sometimes makes debugging difficult by generating an average value and bypassing the control-flow under normal conditions. However, if you are concerned and careful about these cases, the lazy-initialization can make us freer from concerns about thread-safe and performance.
The lazy-initialization is a result of the operator by and a function lazy. Many delegations are there that we can implement, such as Observable and notNull. You can also implement some interesting delegated properties if required.
In Android app development, many developers tend to build their apps with Kotlin technology. Kotlin app development has been a talking point amongst Android developers, especially for its lazy and delegate properties usage. Lazy properties and delegated properties are one of the most used plus great features to have in a developer’s coding bag. Developers tend to use this feature more often than not.
This gives them the flexibility to initialize and access the properties. They believe it is great to have a lazy property in their code without altering the way it is used in calling code.