What is “referential transparency” that I hear talked about in computer programming?

161 views

[ad_1]

What is “referential transparency” that I hear talked about in computer programming?

In: Technology
[ad_2]

It means that a variable can’t change it’s value within an enviroment.

So stuff like a=a+1 is forbidden. You need to find a new name like a1=a+1

This is important for certain programming paradigms wich are closer to math.

In math a=a+1 is impossible too, because “a” can only have one value.

“Transparent” in this context means that you can act as if the reference wasn’t there, just like light acts as if transparent glass wasn’t there. You “look” straight through the reference onto the underlying value.

For this to be true, it means that you mustn’t change that value within a function. For example, if you start with x = 5, you can’t reassign it to x = 6 within the code, because the reference x **is the same as 5** by definition. This requirement is the referential transparency.

It means that you can replace an expression with its value without changing the behavior of the program. It’s usually used in the context of functions: If we have some function `foo`, and it turns out that calling `foo(1, 2, 3)` returns `5`, then there should be no difference between writing `int x = foo(1, 2, 3)` and `int x = 5`.

This has some implications:

* `foo` must always return the same output for the same input, so it can’t depend on any external value that changes
* `foo` must not produce any side effects, which includes performing I/O or setting external values