Improve Code Quality
Immutability & Purity
Immutable Objects & Pure Methods
Enforce immutability patterns and functional purity for safer concurrent code.
At runtime, a program's states are instance and static field values. Controlling how, when, and why states are changing is one of the most challenging programming tasks. This becomes incredibly difficult in multi-threaded environments because potential side-effects due to state changes get too complicated to understand.
Why Immutability Matters
Object-Oriented developers are increasingly inspired by the functional programming approach where most states are immutable. An object is immutable if its state doesn't change once it has been created. A method is pure if its execution doesn't change any field state. Immutable objects and pure methods are two efficient ways to limit unexpected side-effects.
How CppDepend Helps
Through dedicated Code Quest conditions and rules, CppDepend lets you assert immutability and purity on your classes and methods. You can:
- Detect mutable fields in supposedly immutable classes
- Identify impure methods that modify object state
- Enforce purity constraints across your codebase
- Generate reports on immutability compliance
The best place to start with this feature is to look at the list of default Code Quest constraints and read the associated documentation. CppDepend includes built-in rules for detecting immutable types, pure methods, and side-effect violations.
