pure function
A function whose result depends only on its arguments and which has no side effects, so the same inputs always give the same output.
In more detail
Pure functions are the easiest code to test: call them with inputs and check the result, with no database or clock to prepare. Side effects such as reading the clock or writing to storage are pushed out to injected collaborators instead.
Examples
The course keeps business decisions pure and injects time, storage and network access.React requires components and reducers to be pure during rendering: changing an outside variable, sorting a prop array in place or starting a request during render makes results unpredictable.In this course: Pure functions are the easiest code to test, because there is nothing to set up and nothing to clean up. Real code also reads clocks, files and networks; much of testing is about pushing those inputs to the edges so the core stays pure.Related terms
- side effect — Anything a piece of code does besides returning a value, such as writing to a database, sending a message or changing a variable outside it.
- unit test — An automated check of one small piece of code, such as a single function, that runs quickly and on its own.
- deterministic — Always producing the same result from the same inputs, with nothing left to chance or timing.
- idempotency — The property of an operation that gives the same result whether you run it once or many times, because a repeat run finds nothing left to do.
- Strict Mode — A development-only React wrapper that runs extra checks, such as rendering components twice and running every Effect setup and cleanup an extra time.
- mutation — A change to data, and in TanStack Query or tRPC specifically, a request that creates, updates or deletes data on the server.
Taught in
- Designing Testable Code — Modern Java: Virtual Threads & High-Throughput Services
- Tying It All Together — Modern Java: Virtual Threads & High-Throughput Services
- JSX Transpilation: createElement, JSX Runtime, React Elements — React 19 & Modern Component Architecture
- Component Purity: Pure Functions, Idempotence And Side Effects — React 19 & Modern Component Architecture
- Complex State with useReducer: State Machines And Action Types — React 19 & Modern Component Architecture
- useRef Mechanics: DOM References And Mutable Persistent State — React 19 & Modern Component Architecture
- Rules of Hooks: Call Order, Hook Lists, And Fiber Storage — React 19 & Modern Component Architecture
- The React Compiler: Automated Memoization Under The Hood — React 19 & Modern Component Architecture
- Arrange, Act, Assert — Automated Testing, TDD & Quality Engineering