QamoosTech
Programming FundamentalsFunctions and scopeIntermediate

Referential Transparency

PronunciationREF-er-EN-shul trans-PAIR-en-see

Definition

An expression is referentially transparent if you can replace it with its result anywhere without changing the program's behaviour. Pure functions have this property.

Where you hear it

In functional programming talks, discussions on caching and testing, and comparisons of Haskell with Python.

Examples

  • add(2, 3) can always be replaced by 5, so it is referentially transparent.
  • random() is not referentially transparent.
  • A pure call such as area(2) can be replaced by its result, 12.56, anywhere in the code.

Common mistake

Hiding a clock, a global or a database read inside a function that looks pure. Then replacing it with a value changes behaviour.

Don't confuse with

A pure function, which is the practical form of the idea. Referential transparency is the property you reason with.

Say it at work

  • In a meeting or chatThat makes it safe to memoize.
  • In an email or ticketIs this call referentially transparent?