Ritenta
Back to roadmap

Lesson 2 of 7 · 2 min read

Readable code under change

A reminder runs at the wrong time and another reports success after a failed write. Refactor around decisions that can be named, observed and tested.

Name the decision and its units

Replace an ambiguous delay argument with delayMs and name the actual deadline dueAtMs. If a notification is due at exactly that instant, write the comparison accordingly and test one millisecond before, at and after the boundary. A default parameter only replaces undefined, so validate what null means at the input boundary. Preserve zero if immediate scheduling is valid. A consistent formatter helps readers scan the file while domain names explain the meaning.

Isolate decisions from effects

Let decideReminder compare the event, preference and clock value and return a tagged result such as skip with a reason or schedule with a deadline. Pass time explicitly so the rule can be tested without changing a global clock. The caller loads data, runs the decision and persists the schedule. If several fields belong together, a named options object may clarify the call. Avoid extracting a helper whose only contribution is an extra jump. Keep a comment that explains a compatibility constraint, such as why old schedules keep the timezone captured at enrollment.

Make failure visible

A duplicate schedule can be an expected result, while an unavailable store is a dependency failure. Represent them separately. Catch where code can recover or translate the failure, keep the original cause when wrapping an unexpected error, and emit success only after the required write. Tests should assert that a storage failure produces the declared error and prevents later steps. A branch coverage percentage alone cannot establish either behavior.

Review which work may run together

Reading a course title and reading a learner’s preferences can run concurrently if their results need no shared snapshot. A schedule write depends on both. Promise.all reports the first rejection and does not automatically cancel other operations already started. Keep irreversible actions behind their prerequisites, pass cancellation only where the operation supports it, and bound concurrency for a whole batch. An apparently faster refactor is wrong if it can send before verifying consent.

Make domain meaning visible in names and results. Test time boundaries, failed effects and the order of dependent work.

Long code lines scroll sideways →
function deliveryTimeMs(nowMs, delayMinutes) {
  const minutes = delayMinutes ?? 30;
  if (!Number.isInteger(minutes) || minutes < 0) {
    throw new RangeError('delayMinutes must be a nonnegative integer');
  }
  return nowMs + minutes * 60_000;
}

Sources