DEV Community

Cover image for Code Smell 241- Referential Transparency Violation
Maxi Contieri
Maxi Contieri

Posted on

7

Code Smell 241- Referential Transparency Violation

Pure functions produce the same output for the same input and have no side effects

TL;DR: Your functions should be replaceable by the computation result.

Problems

  • Readability

  • Principle of least astonishment violation

  • Testability

  • Coupling

Solutions

  1. Avoid side effects and erratic behavior

Context

Breaking referential transparency occurs when the code introduces side effects or relies on a mutable state.

This violates the principle that an expression or function can be replaced with its value without changing the program's behavior.

Sample Code

Wrong

# Global mutable variable
counter = 0

# Function with side effect
def increment_counter():
    global counter
    counter += 1
    return counter

# Function with implicit dependency and non-deterministic
def get_random_number():
    import random
    return random.randint(1, 100)

# Function with non-deterministic behavior
def get_current_time():
    import time
    return time.time()
Enter fullscreen mode Exit fullscreen mode

Right

import random
import time

# Function without side effects
def increment_counter(counter):
    return counter + 1

# Function without side effects (but not deterministic)
def get_random_number():
    return random.randint(1, 100)

# Function without side effects (can also be injected)
def get_current_time(timesource):
    return timesource.time()
Enter fullscreen mode Exit fullscreen mode

Detection

[X] Semi-Automatic

Many linters warn you when you violate referential transparency

Tags

  • Coupling

Level

[x] Intermediate

AI Assistants

Most AI assistants will avoid violating referential transparency.

Conclusion

Functional programming is known for its ability to enable concise, expressive, and maintainable code, as well as facilitating parallel and concurrent programming due to its emphasis on immutable data and pure functions.

There are many concepts to borrow from it.

Relations

More Info

Disclaimer

Code Smells are my opinion.

Credits

Photo by Wilhelm Gunkel on Unsplash


Referential transparency is a very desirable property: it implies that functions consistently yield the same results given the same input, irrespective of where and when they are invoked.

Edward Garson


This article is part of the CodeSmell Series.

Dynatrace image

Frictionless debugging for developers

Debugging in production doesn't have to be a nightmare.

Dynatrace reimagines the developer experience with runtime debugging, native OpenTelemetry support, and IDE integration allowing developers to stay in the flow and focus on building instead of fixing.

Learn more

Top comments (0)

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

👋 Kindness is contagious

Discover fresh viewpoints in this insightful post, supported by our vibrant DEV Community. Every developer’s experience matters—add your thoughts and help us grow together.

A simple “thank you” can uplift the author and spark new discussions—leave yours below!

On DEV, knowledge-sharing connects us and drives innovation. Found this useful? A quick note of appreciation makes a real impact.

Okay