Urgent.News

What's breaking now, across thousands of outlets.

Tech

Freeze the Clock Before You Refactor: Characterization Tests for Impure Code

Legacy code is rarely untestable. It is usually just unseeded. A characterization test that reads the wall clock will flake on day two. So freeze the inputs first, then pin the outputs. This is the order that survives review. Below is a reproducible loop: inventory impurity, insert one text-only boundary, record behavior, freeze it, prove the test can fail, then make the smallest safe change. The…

Characterization tests for impure code focus on freezing behavior, rather than desired behavior. These tests are crucial for dealing with unseeds like wall clock dependencies. To begin, inventory impurity through text search rather than profiling. This is faster and more precise. Count the hits and record the number in the commit message.

Each hit requires a strategy to pin it down. For process-local impurity like clock, RNG, UUIDs, and environment variables, in-process patching suffices. For external impurity such as network, database, and filesystem, recorded fixtures are necessary. The inventory doubles as a review checklist. Any unlisted impurity indicates a gap to watch for later.

Next, insert a boundary module without altering the original code. Create a thin module that substitutes calls to the impure functions with their frozen counterparts. Implement the following methods: now_date() returns a fixed date string; new_id() returns a fixed UUID; pick(seq) returns the first item from the sequence; and env(key, default) returns the environment variable value or a default.

Substitute these methods mechanically in the code. Run the code to ensure the substitution behaves identically to the original. Record the output as a golden file and assert against it in a test. This two-step process ensures the frozen behavior is verified before any changes are made. Finally, create a fixture to freeze the environment, ensuring the test environment remains consistent across runs.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

SteamVR improves streaming image quality — and what about scientific VR?

SteamVR improves streaming image quality — and what about scientific VR? Valve has released a beta update of SteamVR which improves the image quality transmitted to all autonomous headsets compatible…

  • SteamVR beta update improves image quality for autonomous VR headsets like Meta Quest.
  • Three key factors: resolution per eye, latency, stability, color and depth fidelity.

Beyond Binary: A Layered Architecture of Clinical Decision Support

Beyond Binary: A Layered Architecture for Clinical Decision Support Summary Diagnostic errors and missed findings are among the leading causes of preventable harm in hospitals, yet most AI-powered…

  • Diagnosis errors and missed findings cause most preventable hospital damages
  • Current AI-based CDSS tools provide binary outputs without uncertainty quantification
  • Proposed three-layer CDSS architecture addresses these problems

More from Monday 9 November →