DEV Community

Daniel Pertu
Daniel Pertu

Posted on

Upgrading must not shrink anyone's share, so I checked 27,404 ways to split a day, and the rounding never fires

Nakodo gives each plan a number of new outreach emails a day: Free 10, Pro 25, Business 75. On plans with more than one campaign, that day is split between the campaigns, and each campaign owns a share the user chose. When someone upgrades, every share has to grow with the day, or the user pays for 75 and gets 25 spread the same way.

The function that does it is small enough to quote whole:

export function widenShare(share: number, fromDay: number, toDay: number): number {
  return toDay > fromDay && fromDay > 0 ? Math.floor((share * toDay) / fromDay) : share;
}
Enter fullscreen mode Exit fullscreen mode

A share grows in proportion, so a campaign with 10 of Pro's 25 gets 30 of Business's 75. It rounds down, and the comment explains why: the shares must still add up to no more than the new day. A smaller day leaves a share alone, because after a downgrade it can only be lowered, never raised past the room that is free.

I had never asked how much the Math.floor costs. This week I ran the question.

The experiment

For each pair of plans, I generated every possible way to split the old day across one, two, three or four campaigns, with shares from zero up to what is left, pushed each through widenShare, and recorded three things: whether the new shares ever add up to more than the new day, whether any share shrank, and how much of the exact proportional amount the floor threw away.

Plans Splits tried Over the new day A share shrank Lost to the floor
Pro to Business 27,404 0 0 0
Free to Pro 1,364 0 0 up to 2 emails
Free to Business 1,364 0 0 up to 2 emails

The Pro to Business row tells the story. 25 times 3 is 75, so every share scales by an exact integer, and the floor has nothing to throw away. Not in one of 27,404 splits. The Free rows lose something, because 25 over 10 is 2.5: a share of 3 becomes 7, not 7.5. In the worst four-campaign split the loss is two emails.

And then the call site

That table made me open widenCampaigns in src/lib/pipeline/launch.ts, which is the only caller:

// Only between plans that split their day: a one-campaign plan gives its
// campaign the whole day, whatever share it has.
if (before.campaigns > 1 && limits.campaigns > 1) {
Enter fullscreen mode Exit fullscreen mode

Free allows one campaign, so Free never enters that branch. A one-campaign plan has no shares to widen. The only pair that does is Pro (5 campaigns) to Business (25), where the ratio is exactly 3.

So the rounding in widenShare is never exercised in production today. The Free rows in my table are a calculation about a path the app does not take. The code is correct and has its own unit test. It carries a decision, "round down so we never promise more than the day", that costs nothing until somebody changes a plan's day to a number that is not a multiple of the plan below it.

Is that dead code?

I do not think so. A one-line function with the right rule is cheaper than a future surprise. What I took from the run is narrower:

  1. The invariant, not the function, is what to test. My experiment is the right test: for any split and any pair of plans, the widened shares add to no more than the new day, and no share shrinks. It runs in a moment, and the existing unit test checks examples rather than all of them.
  2. "Lost to the floor" is a pricing input. If a plan with an odd ratio is ever added, a table like this says how many emails a customer silently loses on upgrade, and "up to 2 on a day of 25" is a number you can decide about.
  3. Read the call site before you trust the table. Half of my interesting results were about a case the guard in the caller rules out.

Check it yourself

The plan limits are on /pricing, and every number there comes from the same PLANS object the function reads. The experiment is a 30-line loop with a generator that walks the splits. It needs nothing but node and the file itself, because daily-shares.ts imports nothing.

Top comments (0)