Skip to content
Latchkey

What Is a Side Effect? When Code Changes the World

A side effect is any observable change a function causes beyond returning its result, such as writing to disk, calling an API, or mutating shared state.

A function with no side effects only computes and returns a value. The moment it does anything else observable, writes a file, prints output, sends a request, changes a global, it has a side effect. Side effects are how programs actually do useful work, but they also make code harder to test, reason about, and run safely in parallel.

What counts as a side effect

  • Writing to disk, a database, or the network.
  • Mutating a variable or object outside the function.
  • Printing to the console or logging.
  • Reading mutable external state like the clock or a random source.

Why side effects complicate things

A function with side effects depends on and changes the world around it, so its behavior can vary with hidden context. That makes it harder to test (you need to set up and verify the effect) and harder to reason about, since order and environment now matter.

Side effects and testing

Pure logic is easy to test; effectful code needs mocks, fixtures, or real resources. Tests that perform real side effects (hitting a live service, touching a shared file) are slower and more prone to flaking on shared CI infrastructure.

Managing side effects

A common discipline is to isolate side effects at the boundaries of your program and keep the core logic pure. That way most code is easy to test, and the small effectful shell is the only part that needs heavier integration testing.

A quick example

A function that computes a total and returns it is effect-free; one that also writes that total to a log file has a side effect, the file write, which a test must now account for.

Side effects in CI

Uncontrolled side effects, shared files, real network calls, leftover global state, are a top cause of order-dependent, flaky tests in CI. Isolating effects keeps tests deterministic. Latchkey auto-retries genuinely transient failures while you remove side-effect-driven flakiness.

Key takeaways

  • A side effect is any observable change a function makes beyond its return value.
  • Side effects do the useful work but make code harder to test and reason about.
  • Uncontrolled side effects are a leading cause of flaky, order-dependent CI tests.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →