Skip to content
BENIBIX Learn

Side Effect

Development🧭 Programming ParadigmsFunctional Programming

Also known as Side Effects

Work a function does on the side. Saving a file is one; so is printing.

Think of it like

Think of it like a cook who also moves your chairs. You asked for soup, not new seats.

Example

Emma's getPrice function also wrote to the log and bumped a counter. Calling it twice in a test gave two different counts.

Why it matters

Hidden side effects are a top cause of bugs that only appear sometimes. Knowing where they live makes code easier to trust.

Definition

A side effect is anything a function does besides giving back its answer. Changing a variable outside the function is one. So is saving a file, printing, sending an email or calling a server. Programs need them to be useful at all. The trouble starts when they hide where nobody looks.

Common mistake

Trying to remove all side effects. A program with none cannot save, show or send anything.

Key takeaways

  1. 1
    Beyond the answer. Any change to the outside world.
  2. 2
    Needed but risky. Programs must have some.
  3. 3
    Keep them visible. In a few known places.