I'll Be Backend
Principles

Immutability: Value Objects Instead of Setters

An immutable object never changes after it is made, and a value object is a small immutable object that is equal to another when its values are equal. A setter on a shared object lets one change leak into every owner. These lessons take a car rental desk where two bookings share one price, remove the setter, compare prices by value, and then add an extra as a new price.

Pick your language

When to use it

Use it when

A small object, like a price, a date range or an address, is passed around and shared.

Skip it when

The object has its own identity, like Ana's booking. Code that holds it must see its changes, and a copy would leave it looking at the old booking.

Spot it by

A setter on an object that more than one owner holds, or defensive copies before every change.