[Clean code] Securely write code through referential transparency

Seong-Am Kim
4 min readJul 3, 2020

There are many techniques to make a program safe. Today we are going to use referential transparency to find out how to make programs safe.

Photo by Adam Nieścioruk on Unsplash

First of all, the definition of referential transparency is as follows.
codes that neither changes the external state nor relies on the external state.

Why should we write code like this? Here are some practical reasons.

  1. A function that do many things at once and affect other places make code difficult to understand.
  2. Keeping referencial transparency makes it easier to write a test code.
  3. Maintaining referencial transparency gives the system flexibility to design and change.

Now let’s look at the details below 😁

1. A function that do many things at once and affect other places make code difficult to understand.

When a function does a lot of work, the program becomes very complex.

photo by EC-Council on EC-Council|Blog

I think this is in line with what many programmers are saying not to cause a side effect.

In the book Clean Coder I am reading, the following words appear.

The function must do one thing. It has to do one thing well. It has to do only one thing. — Robert C. Martin

The following is an example of a bad code opposite to a referential transparency.

class UserValidator {
private val cryptographer: Cryptographer
fun checkPassword(userName: String, password: String): Boolean {
val user = UserGateway.findByName(userName)
if (user !== User.Null)
{
val codedPhrase = user.getPhraseEncodedByPassword()
val phrase = cryptographer.decrypt(codedPhrase, password)
if ("Valid Password" == phrase)
{
// The code below has a side effect.
Session.initialize()
return true
}
}
return false
}
}

The above function is a function that authenticates the user.

However, invoking a function called Session.initialize() affects sessions at the same time as user authentication.

When the session is initialized, the existing session information is erased. Unintentional situations can occur when using only the name of the function.

Therefore, it is correct that the corresponding session.initialize() is removed separately to maintain referential transparency.

2. Keeping referencial transparency makes it easier to write a test code.

Photo by CDC on Unsplash

The test described here is a unit test.

It is very important to reduce external dependencies to create clean codes and to write test codes for TDD.

In a highly dependent environment, it is difficult for programmers to control their own code.

There are many good articles about this place, so please refer to the link below.

3. Maintaining referencial transparency gives the system flexibility to design and change.

Photo by James Pond on Unsplash

A function with referential transparency is only affected by the parameter of the function and the result is also predictable.

Do you know Components-Based Development(CBD)?

I felt how important it was to keep reference transparency as I developed using Flutter and React.

Good components are interchangeable like parts and can use the necessary functions at any time.

This increases efficiency by speeding up development.

The above three are the benefits of keeping referential transparency and I think this is not separate concepts but one integrated concept connected.

I want programmers to develop and maintain more easily by keeping referential transparency.

Happy Coding, everyone! 😁

--

--