Computed Property Vs Lazy Property

abdul ahad
3 min readMar 9, 2024

--

Photo by Erik Mclean on Unsplash

In Swift, both computed properties and lazy properties are used to define properties that are not stored directly in the class or struct. However, they serve different purposes and have different behaviors. Understanding the differences between them is crucial for choosing the right approach for your specific needs.

Computed Properties

A computed property is a property that does not store a value. Instead, it provides a getter and an optional setter to compute the value. The value is calculated every time it is accessed, and it can be based on other properties of the class or struct.

Key Characteristics:

  • No Storage: Computed properties do not store a value. They calculate the value on the fly.
  • Dynamic: The value can change over time, depending on the state of other properties.
  • Read-Only or Read-Write: Computed properties can be read-only (with only a getter) or read-write (with both a getter and a setter).

Example

The diameter property will be computed every time the value of radius changes as shown in the test the value gets updated to 80 when radius changes to 40

Lazy Properties

A lazy property is a property whose initial value is not calculated until the first time it is used. After the initial calculation, the value is stored and reused for subsequent accesses. This is particularly useful for properties that are expensive to calculate or that depend on external resources.

Key Characteristics:

  • Lazy Initialization: The value is calculated only once, the first time it is accessed.
  • Stored: After the first access, the value is stored and reused.
  • Read-Only: Lazy properties are always read-only. They cannot have a setter.

Example

let's take the previous example and compare it.

as you can see changing the radius doesn’t change the lazyDiameter property as lazy properties are calculated once only when they are accessed and the value is stored and reused after the first access.

here is the source code:

 struct Circle {
var radius: Double
var diameter: Double {
get {
return radius * 2
}
set(newDiameter) {
radius = newDiameter / 2
}
}

lazy var lazyDiamter: Double = {
return radius * 2
}()


}

//TEST TARGET
class TestProperties:XCTestCase{
func test1(){
var circle = Circle(radius: 10)
XCTAssertEqual(circle.diameter, 20)
XCTAssertEqual(circle.lazyDiamter, 20)

circle.radius = 40

XCTAssertEqual(circle.diameter, 80)
XCTAssertEqual(circle.lazyDiamter, 80)


// XCTAssertEqual(circle.lazyDiamter, 80)
}

}

In summary,

the choice between a computed property and a lazy property depends on the specific requirements of your property, such as whether its value needs to be recalculated every time it’s accessed, whether it’s expensive to calculate, and whether it needs to be mutable.

--

--

abdul ahad
abdul ahad

Written by abdul ahad

A software developer dreaming to reach the top and also passionate about sports and language learning

No responses yet