Importance of Immutability

In object-oriented and functional programming, immutable objects are objects whose state cannot be changed or altered after they are initiated. Therefore, a mutable object stays the same until the end of its life cycle, when it is de-initialized. In contrast, a mutable object can be altered countless times by other objects after it is initiated.

Immutable objects improve readability and runtime efficiency, and using them simplifies our applications.

This chapter will cover the concept of immutability by discussing the following topics with coding examples:

  • Immutability
  • Benefits of immutability
  • Cases for mutability
  • An example with approach comparisons
    • Side-effects and unintended consequences
    • Testability
  • Copy ...

Get Swift Functional Programming - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.