DELAPARRA
Index
Color
NL
Category: Programming paradigms

Referential Transparency

Explained by Updated

What is Referential Transparency?

Referential Transparency: An expression can be replaced with its value without changing the program's behaviour. If getUser(1) always returns the same User, you can cache or reason about it freely. Referential transparency means an expression can be replaced by its value without changing how the program behaves. If price(42) always returns 19.95, you can replace the call with 19.95 anywhere and nothing changes.

Definition source Coders Lingo, CC BY 4.0

Frequently asked questions about Referential Transparency

How is Referential Transparency used in practice?

That property only holds for pure functions, which have no side effects and do not depend on changing state such as the time or a database.

What should you keep in mind with Referential Transparency?

It makes reasoning and refactoring much safer, because you can move, cache or reorder such calls without fear. Compilers and frameworks can optimise them too. Most application code mixes pure and impure parts. Keeping the calculations referentially transparent, and the database reads and writes in a separate thin layer, gives you code that is both easy to test and easy to change.

At a glance

CategoryProgramming paradigms
Explained by
Updated

More Programming paradigms terms

All terms starting with R

Skills

Download all skills (zip)

Manuals

Related services

Need any help?

Stuck on one of these in your own project? Tell me what you are working on and I will tell you how I would approach it.