advanced

Avoiding hidden side effects

Keep I/O, mutation, global state, time, randomness, and retries visible at boundaries so behavior is testable and diagnosable.

Hidden side effects—silent I/O, mutation, global state, time, randomness, logging, retries—make code hard to test and debug. Pure core logic with effects pushed to the edges is a practical clean-code goal. Name effectful functions honestly and isolate non-determinism behind injectable clocks or random sources.

On interviews: walk through a getter that secretly writes to storage or a validator that sends email. Explain how you would refactor for predictability.

Common pitfalls: static singletons for clients; implicit retries in utility functions; shared mutable module state in serverless handlers.

The trade-off is flexibility versus complexity—know when the simpler path is enough.

Checklist:

  • Label functions that read/write external systems.
  • Inject time, IDs, and randomness in tests.
  • Keep domain calculations free of network calls.
  • Document retry and cache behavior at boundaries.