Skip to content

HowProgrammingWorks/ReferentialTransparency

Repository files navigation

Referential Transparency

Traditional definition

Referential Transparency is the property of a function, expression, or object that can be replaced by its resulting value without changing the program's behavior.

Alternative definition

We can pass a reference to any immutable object, pure function, closure (which does not change state) to any part of the program without fear of race conditions or data corruption, even with concurrency and asynchrony.

Meaning

  • Same explicit inputs gives same result
  • No hidden state, no mutation of shared data (Immutable state)
  • No I/O
  • Pure functions

This principle simplifies code reasoning, debugging, and optimization. Can be used in Functional, Object-Oriented Programming, other paradigms.

For Object-Oriented Programming

In OOP, referential transparency is achieved when object’s state is immutable. "State changes" are represented by returning a new object rather than mutating existing state.

  • Return a new object from methods with changed state
  • Return the same one for methods not changing state

Related ideas

Copy-on-write: A memory optimization technique where multiple references can share the same data until one of them needs to modify it, at which point a copy is created. This allows efficient sharing of immutable data structures while preserving referential transparency.

Ownership: A memory management concept (prominent in Rust) where each value has a single owner responsible for its lifecycle. When the owner goes out of scope, the value is automatically cleaned up. This prevents data races and memory issues while maintaining referential transparency.

About

Referential Transparency in Functional and Object-Oriented Programming

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published