Urgent.News

What's breaking now, across thousands of outlets.

Tech

Record One Nested Decision, Then Extract a Single Predicate

A pricing function still folds region rules, bulk surcharges, and coupon stacking into one nested block. A teammate asks an assistant to tidy that module before a tax change lands next week. The first generated patch rewrites four helpers, renames two exceptions, and flips a surcharge for twelve-item carts. Review then spends more time reconstructing prior behavior than evaluating the one extract…

A nested pricing function consolidates multiple rules, including region-specific policies, bulk surcharges, and coupon stacking into a single block of code. A colleague suggests refactoring this module ahead of an upcoming tax change. The initial attempt splits the code into four helper functions, renames exceptions, and modifies a surcharge for carts containing 12 items.

However, during review, more time is spent reconstructing previous behavior than evaluating the actual extraction needed. The issue lies in treating the failure as a process problem rather than focusing on the clean code aspect. The goal is to identify one nested decision on a hot path, record it before any changes, and only allow a single predicate extraction afterwards.

The failure patterns observed repeatedly in reviews include silent branch collapse, exception reshaping, and rounding drift. To address these issues, a record should be created using a triple consisting of canonical input, outcome tuple, and a stable hash of the pair. This hash acts as a review signal, and any mismatch should immediately stop the extraction process.

If two consecutive runs produce different results, the path is considered too noisy to modify. The recorder should be a compact tool focusing on a single hot path, ignoring style changes and mutations in the module. By freezing the nested decision instead of the entire module, the review process becomes more manageable and focused on preserving the desired outcomes.

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

Stop exporting design tokens to JSON for your web apps

A PR review last Thursday morning triggered this thought. A developer had written a 40-line utility function in our main web repository.

  • Excessive JSON use for design tokens causes unnecessary complexity.
  • CSS variables are optimized for reading, cascading, and updating design values.
  • Design System Sync Figma plugin exports tokens directly to CSS variables.

Read-only user impersonation in Rails

Many support tools eventually grow a "log in as this customer" feature. Ours had a problem the usual gems don't solve. The people on the other side of the button are customers of a service containing…

  • ViewingAs gem restricts administrators from sensitive actions during impersonation
  • Prevents non-GET/HEAD requests and logs write attempts at error level
  • Consent lambda determines impersonation based on customer preferences

More from Wednesday 23 September →