agentsclimarketplace

Divide by zero rate guard

Skill kjuhwa/skills-hub/skills/backend/divide-by-zero-rate-guard

Guard rate/percentage calculations against zero denominators to prevent NaN from poisoning downstream metrics and serialization.From its SKILL.md

Install
npx -y skills add kjuhwa/skills-hub --skill divide-by-zero-rate-guard

Assembled from the repository path, not quoted from the project. Check it against their README if it does not work.

One thing to look at

  • 0 stars0 stars. Stars are a popularity signal and not a quality one, but at this level it is likely that nobody has read this closely except its author, and you would be relying on your own review.

SKILL.md

1.9 KB, 393 tokens by cl100k_base, as published. Nobody here has run it

Divide-by-Zero Rate Guard

Rate and percentage metrics (errorRate, successRate, cacheHitRatio, …) must check for a zero denominator before dividing.

Why

In Java, integer x/0 throws; double x/0.0 yields NaN or ±Infinity. NaN then propagates silently through sum, avg, and other aggregates, corrupting downstream metrics, and serializes as "NaN" in JSON — which many clients reject outright. The bug usually surfaces only when a new or idle tenant has zero traffic in a bucket, so early tests miss it.

Pattern

double errorRate = (servicedCount != 0)
        ? ((double) errorCount / servicedCount) * 100.0
        : 0.0;

Helper form:

static double safeRate(long num, long den) {
    return den == 0 ? 0.0 : (double) num / den;
}

Steps

  1. Identify every rate/percentage field in the aggregation.
  2. Wrap each division with an explicit zero-denominator check.
  3. Unit-test the zero-traffic bucket — most "NaN in dashboard" bugs ship because no test covers the empty case.
  4. If upstream values can themselves be NaN, guard at the source, not just at the last division.

Counter / Caveats

  • Returning 0.0 is a choice. For some metrics null is more honest: "no data" ≠ "0%". Pick per metric, and document in the DTO comment.
  • In reactive pipelines, one NaN element can poison a reduce/collect — guard at the producer, not only at the consumer.
  • Beware doubles summed from other doubles that are already NaN — one guard doesn't protect the chain.

What ships with it

Read from the repository

Just SKILL.md. No reference files, no scripts.

Keep looking

Skills are one crate of 326,835. Ordering is by how many stacks a row turns up in, so the top of any crate is what has actually been picked rather than what has the most stars.