All courses

Text, time and money: classic data traps

Spot agent code that is wrong for real-world data: string length, slicing or case-folding that breaks on non-ASCII text, naive datetimes and DST or time-zone shifts, and floating-point arithmetic for money. Name the safe type or library in each language (for example Decimal or BigDecimal, integer cents, zone-aware datetimes).

Builds on: Why agents fill gaps with plausible guesses

Read it card by card Start the whole course

Green tests, tidy fixtures

You asked the agent for an order summary: a 40-character preview of the customer's note, the order total, and a due date 30 days out. It finished in two minutes. Tests are green, and its summary says: 'Handles formatting and rounding.' The fixtures use notes like `"Please ring twice"`, prices like `10.00` and dates in June. Real customers write emoji and accented names, buy items at `19.99`, and order on the night the clocks change. Which of these real cases did the green tests ever touch?

Typical code is right for typical data

Public code, tutorials and fixtures are full of ASCII names, round prices and dates far from a clock change. So the most typical continuation is `len(name)`, `float(price)` or `datetime.now()`, which is correct for that data. Your tests often use the same data, so they go green. That green is only a proxy signal: a visible stand-in for 'works for real users' that can pass while the real goal fails. The agent cannot see your users' names, locales or time zones. Later cost: mangled names, totals that are a cent off and won't reconcile, and reminders an hour off twice a year. All of it fails silently.

Three tells and their safe types

- **Text**: `len`, `.length` or slicing on user text. Go and Rust count UTF-8 bytes (Rust panics, i.e. crashes, if a slice cuts a character). JS and Java count UTF-16 code units; Python counts code points. None counts graphemes. Case-insensitive matching: Python `casefold()`, Go `strings.EqualFold`, Java `toLowerCase(Locale.ROOT)`. - **Time**: naive `datetime.now()`, `LocalDateTime`, or `+ 86400` seconds as 'a day'. Use UTC instants plus a named zone (`zoneinfo`, `ZonedDateTime`, `time.LoadLocation`). Add days with calendar math (`plusDays`, Go `AddDate`). - **Money**: `float`, `double`, JS `number`. Use `Decimal` or `BigDecimal` built from strings, or integer cents (common in Go, Rust, JS).

A 'fix' that keeps the float

You report totals that are a cent off. The agent replies: 'Switched to BigDecimal; rounding is now exact.' The diff has `new BigDecimal(item.getPrice())`, but `getPrice()` still returns `double`. The tell is the type where the price enters the code, not the class name: `new BigDecimal(0.1)` is exactly 0.1000000000000000055511…. Later cost: half-cent ties still round wrong. `new BigDecimal(1.015)` is just below 1.015, so it rounds to 1.01, not 1.02, and the bug now hides behind a reassuring type. BETTER: read the price as text or SQL DECIMAL into `new BigDecimal("19.99")`, keep it `BigDecimal` throughout, and round once with `setScale(2, RoundingMode.HALF_EVEN)`.

The order summary, three traps

This one diff has three tells. First, `.length` and `slice(0, 40)` count UTF-16 code units, so an emoji that straddles position 40 becomes a lone surrogate, shown as �. Second, `price * qty` multiplies and sums binary floats. `toFixed(2)` hides the drift on screen, yet `(1.005).toFixed(2)` is `"1.00"`, not `"1.01"`. Third, `30 * 86_400_000` ms is exactly 720 hours, but across a DST change 30 local days are 719 or 721 hours. An order placed at 00:00 comes due at 01:00, or at 23:00 the day before. BETTER: truncate by graphemes with `Intl.Segmenter`, sum integer cents, and add 30 calendar days in a named zone with `Temporal.ZonedDateTime` or a zone-aware date library.
// TypeScript
--- a/src/orders/summary.ts
+++ b/src/orders/summary.ts
@@ -12,1 +12,8 @@ export function summarize(
-  return { note, total: 0 };
+  const preview = note.length > 40
+    ? note.slice(0, 40) + "…" : note;
+  const total = items.reduce(
+    (s, i) => s + i.price * i.qty, 0);
+  const due = new Date(
+    created.getTime() + 30 * 86_400_000);
+  return { preview, due,
+    total: total.toFixed(2) };

Ask three questions of every diff

Typical code goes wrong on real text, time and money, and green tests on tidy fixtures won't show it. In every diff, ask three things. What does this length count? Which zone is this time in? What type holds this money? The safe answers are graphemes (or bytes, on purpose), zone-aware instants with calendar math, and `Decimal`/`BigDecimal` or integer cents.

On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.