Pure Function: A function with no side effects: given the same inputs, it always returns the same output and never modifies external state. add(2, 3) is always 5 — no matter when or how often you call it. A pure function always returns the same output for the same input and has no side effects. It does not change global variables, write to databases, send emails or depend on the current time. add(2, 3) is always 5.
Pure functions are the easiest code to understand and test. You pass input and check output, without setup, mocks or cleanup. They are also safe to cache and to run in parallel.
What should you keep in mind with Pure Function?
Real applications need side effects, but you can push them to the edges. Keep the core calculations pure: price rules, discount logic, date calculations and validation. Then let a thin outer layer read from the database, call the pure functions and save the result. That structure makes the most important logic thoroughly testable and easy to change.
Implement proven backend architecture patterns including Clean Architecture, Hexagonal Architecture, and Domain-Driven Design. Use this skill when designing clean architecture for a new microservice, when refactoring a monolith to use bounded contexts, when implementing hexagonal or onion architecture patterns, or when debugging dependency cycles between application layers.