# What Is Referential Transparency?

In functional programming, referential transparency is generally defined as the fact that an expression, in a program, may be replaced by its value (or anything having the same value) without changing the result of the program. This implies that methods should always return the same value for a given argument, without having any other effect. This functional programming concept also applies to imperative programming, though, and can help you make your code clearer.

To understand this article you should know how to write simple classes and methods in Java. Knowing what a side effect is helps but if you don’t, you’ll learn it on the way.

## Referential Transparency

The expression referential transparency is used in various domains, such as mathematics, logic, linguistics, philosophy and programming. It has quite different meanings in each of these domain. Here, we will deal only with computer programs, although we will show analogy with maths (simple maths, don’t worry). Note, however, that computer scientists do not agree on the meaning of referential transparency in programming. What we will look at is referential transparency as it is used by functional programmers.

### Referential Transparency in Maths

In maths, referential transparency is the property of expressions that can be replaced by other expressions having the same value without changing the result in any way. Consider the following example:

``````x = 2 + (3 * 4)
``````

We may replace the subexpression `(3 * 4)` with any other expression having the same value without changing the result (the value of `x`). The most evident expression to use, is of course `12`:

``````x = 2 + 12
``````

Any other expression having the value `12` (maybe `(5 + 7)`) could be used without changing the result. As a consequence, the subexpression `(3 * 4)` is referentially transparent.

We may also replace the expression `2 + 12` by another expression having the same value without changing the value of `x`, so it is referentially transparent too:

``````x = 14
``````

You can easily see the benefit of referential transparency: It allows reasoning. Without it, we could not resolve any expression without considering some other elements.

### Referential Transparency in Programming

In programming, referential transparency applies to programs. As programs are composed of subprograms, which are programs themselves, it applies to those subprograms, too. Subprograms may be represented, among other things, by methods. That means method can be referentially transparent, which is the case if a call to this method may be replaced by its return value:

``````int add(int a, int b) {
return a + b
}

int mult(int a, int b) {
return a * b;
}

int x = add(2, mult(3, 4));
``````

In this example, the `mult` method is referentially transparent because any call to it may be replaced with the corresponding return value. This may be observed by replacing `mult(3, 4)` with `12`:

``````int x = add(2, 12)
``````

In the same way, `add(2, 12)` may be replaced with the corresponding return value, `14`:

``````int x = 14;
``````

None of these replacements will change the result of the program, whatever it does. Note that we could use any other expression having the same value, which is useful when refactoring.

On the other hand, consider the following program:

``````int add(int a, int b) {
int result = a + b;
System.out.println("Returning " + result);
return result;
}
``````

Replacing a call to the `add` method with the corresponding return value will change the result of the program, since the message will no longer be printed. In that case, it would only remove the side effect but in other cases, it might change the value returned by the method:

``````public static void main(String... args) {
printFibs(10);
}

public static void printFibs(int limit) {
Fibs fibs = new Fibs();
for (int i = 0; i < limit; i++) {
System.out.println(fibs.next());
}
}

static class Fibs {
private int previous = -1;
private int last = 1;

public Integer next() {
last = previous + (previous = last);
return previous + last;
}
}
``````

Here, the `next` method can’t be replaced with anything having the same value, since the method is designed to return a different value on each call.

Using such non referentially transparent methods requires a strong discipline in order not to share the mutable state involved in the computation. Functional style avoids such methods in favor of referentially transparent versions.

### Referential Transparency in Imperative Programming

Both imperative and functional programming use functions. Although functional programming uses only functions, imperative programming uses:

• pure functions: methods returning values and having no other effects
• pure effects: methods returning nothing but changing something outside of them)
• functions with side effects: methods both returning a value and changing something

As we all know, it is good practice to avoid functions with side effects. This leaves imperative programmers with pure functions and pure effects. Referential transparency is then a powerful tool for imperative programmers to make their programs easier to reason about, and easier to test.

## Summary

Referential transparency means that a function call can be replaced by its value or another referentially transparent call with the same result. It makes reasoning about programs easier. It also makes each subprogram independent, which greatly simplifies unit testing and refactoring. As an additional benefit, referentially transparent programs are easier to read and understand, which is one reason for why functional programs need less comments.

### What is the significance of referential transparency in functional programming?

Referential transparency is a fundamental concept in functional programming. It ensures that a function, given the same input, will always return the same output without causing any side effects. This property makes the function predictable and easier to reason about. It also allows for various optimizations like memoization, where previous results are cached and reused, thus improving the performance of the program.

### How does referential transparency relate to pure functions?

Referential transparency is closely related to the concept of pure functions in functional programming. A pure function is one that, given the same input, will always produce the same output and does not have any observable side effects. Therefore, all pure functions are referentially transparent. However, not all referentially transparent expressions are pure functions, as they can involve constants or variables.

### Can you provide an example of a referentially transparent function?

Sure, let’s consider a simple function in JavaScript that adds two numbers:

`function add(a, b) {`
`return a + b;`
`}`

This function is referentially transparent because for any given values of `a` and `b`, it will always return the same result, and it does not cause any side effects.

### What are the benefits of referential transparency?

Referential transparency offers several benefits. It makes the code easier to reason about and debug because you can predict the output based on the input. It also allows for more robust testing and improved modularity, as referentially transparent functions can be freely refactored and moved around without changing the program’s behavior. Moreover, it enables various optimizations, such as memoization and lazy evaluation.

### How does referential transparency affect concurrency?

Referential transparency greatly simplifies concurrency. Since referentially transparent functions don’t have side effects, they can be executed in any order or even in parallel without causing any concurrency issues. This makes it easier to write concurrent and parallel code, which is particularly important in today’s multi-core and distributed computing environments.

### What is the difference between referential transparency and idempotency?

While both referential transparency and idempotency involve functions producing the same result given the same input, there is a key difference. Referential transparency is concerned with the absence of side effects and the ability to replace a function call with its result without changing the program’s behavior. On the other hand, idempotency refers to the property of certain operations in which performing the operation multiple times has the same effect as performing it once.

### Can you have referential transparency with mutable data?

In theory, you can have referential transparency with mutable data, but it’s challenging to maintain in practice. If a function modifies a mutable data structure, it’s not referentially transparent because it has a side effect. To ensure referential transparency, you should avoid mutating data and instead return a new data structure with the desired changes.

### How does referential transparency relate to recursion?

Referential transparency and recursion often go hand in hand in functional programming. Recursive functions can be referentially transparent if they always produce the same output for the same input and don’t have side effects. This makes recursive functions easier to reason about and allows for optimizations like tail call optimization.

### What is the role of referential transparency in memoization?

Referential transparency plays a crucial role in memoization, an optimization technique used in functional programming. Since a referentially transparent function always returns the same result for the same input, we can cache the result of the function call and reuse it for subsequent calls with the same input. This can significantly improve the performance of the program, especially for computationally intensive functions.

### Can object-oriented programming have referential transparency?

While object-oriented programming (OOP) and functional programming have different paradigms, it’s possible to achieve referential transparency in OOP. This can be done by ensuring that methods of an object do not mutate the state of the object and always return the same result for the same input. However, this is not typically how OOP is used, and it requires a disciplined approach to maintain referential transparency.

Pierre-Yves Saumont
View Author

R&D software engineer at Alcatel-Lucent Submarine Networks, author of Functional Programming in Java (Manning Publications) and double bass jazz player

functional programmingnicolaipquick-tip