Chapter 14. Immutable Data

WHAT'S IN THIS CHAPTER?

  • Understanding Why Immutability Is Good

  • Managing State and Mutation Responsibly

  • Passing References

  • Enhancing Performance

The term functional is similar to object oriented in that it represents a collection of language design choices and associated design methodologies. In this chapter, you'll explore the data side of functional programming in F# through a combination of language features and methodology. When you arrive on the other side, you'll know why immutable data structures are so important and have ways of handling mutation in a safe way.

THE PROBLEM WITH STATE

For years, the vast majority of programmers have combined data in mutable form and operations on that data in constructs known as classes. Along with the numerous advantages of this methodology come a number of disadvantages that are not often discussed. Most of these disadvantages stem from the use of these classes as black boxes that each encapsulate some part of the state of your program. This state is hidden by a mess of unspecified behavior, implicit in the method definitions of the classes.

When consuming these classes, you find yourself unable to directly understand their implementation. The same call to a class method may produce different results, and often does. This problem is not restricted to a single class because many classes mutate each other or are interlinked with complex networks of events. A single method call can cause a ripple effect of state changes throughout ...

Get Professional F# 2.0 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.