iOS Interview: Lazy Property [What and when not to use Lazy property]

Ravi Ranjan
CodeX
Published in
3 min readDec 12, 2022

In Swift, a lazy variable is a variable whose initial value is not calculated until the first time it is accessed. Lazy variables are useful when the initial value of a variable is expensive to compute, or when it is not needed until it is actually used in the code.

Here’s an example of how to create a lazy variable in Swift:

class HeavilyComputation {
// A heavy object that takes a long time to initialize
}

class MyClass {
lazy var heavilyComputationObject = HeavilyComputation()

func doSomething() {
// Use the heavy object
heavyObject.doSomething()
}
}

n this code, we define a HeavilyComputation class that represents an object that takes a long time to initialize. We then define a MyClass class that has a HeavilyComputation property, which is of type HeavilyComputation. We use the lazy keyword to mark the HeavilyComputation property as a lazy variable.

When an instance of the MyClass class is created, the HeavilyComputation property is not initialized until it is accessed for the first time. This means that the HeavyObject instance is not created until the doSomething() method is called, and the HeavilyComputation instance is only created if it is actually needed.

With this implementation, we can avoid the overhead of initializing the HeavilyComputation property unless it is actually needed. This can improve the performance and efficiency of our code, and it can make our code more readable and maintainable.

When we should not use Lazy Property?

Lazy properties are a useful feature in Swift, but they should be used with caution. In general, you should not use lazy properties when the initial value of the property is not computationally expensive, or when the property is accessed frequently and it would be more efficient to compute its initial value upfront.

Here are some reasons why you might not want to use lazy properties:

1. Lazy properties add an extra level of indirection to your code, which can make it more difficult to understand and debug.

2. Lazy properties can introduce unexpected behavior and race conditions if they are accessed from multiple threads simultaneously.

3. Lazy properties can affect the performance of your code if they are accessed frequently since the initial value of the property will be computed every time it is accessed.

4. Lazy properties can make your code less predictable since the initial value of the property will not be computed until it is actually accessed in the code.

In general, you should only use lazy properties when the initial value of the property is computationally expensive, and when it is not needed until it is actually accessed in the code. Otherwise, you should use a regular property and compute its initial value upfront.

If you liked this, click the 💚 and give a clap on this post as much as you can below so other people will see this here on Medium. If you have any queries or suggestions, feel free to comment or hit me on Twitter, or Linkedin.

--

--