Skip to content

fix: guard zero category totals in --percent tabular output - #746

Merged
boyter merged 1 commit into
boyter:masterfrom
YuriNachos:YuriNachos/quick-percent-nan-guard
Aug 11, 2026
Merged

fix: guard zero category totals in --percent tabular output#746
boyter merged 1 commit into
boyter:masterfrom
YuriNachos:YuriNachos/quick-percent-nan-guard

Conversation

@YuriNachos

Copy link
Copy Markdown
Contributor

Problem

The wide (fileSummarizeLong) and short (fileSummarizeShort) --percent rows divide each category total with no zero guard. A project whose files have no blank/comment/complexity lines therefore prints NaN% for those columns:

$ scc --percent a.json
JSON               1        1         0         0          1          0
Percentage       100.0%    100.0%      NaN%       NaN%     100.0%       NaN%

The sibling JSON path (addLanguagePercentages) and the Complexity/Lines total both already guard total != 0 — only the tabular percentage rows were inconsistent.

Fix

Factor the division into a pct(value, total) helper that returns 0 when total == 0, matching the JSON behaviour; each empty category now reports 0.0%.

Test plan

go test ./... — green, including a new TestPercentNoNaNOnZeroCategory that feeds a single file with Blank=Comment=Complexity=0 and asserts the short and wide output contain 0.0% and no NaN. On master the same input emits NaN% (the test fails).

I licence this contribution under the MIT licence.

The wide (fileSummarizeLong) and short (fileSummarizeShort) --percent rows
divided each category total with no zero guard, so a project whose files have
no blank/comment/complexity lines printed NaN% for those columns. The JSON
path (addLanguagePercentages) and the Complexity/Lines calc already guard
total != 0; only the tabular percentage rows were inconsistent.

  $ scc --percent a.json
  JSON   1   1   0   0   1   0
  Percentage   100.0%   100.0%   NaN%   NaN%   100.0%   NaN%

Factor the division into a pct(value,total) helper that returns 0 for total==0,
matching the JSON behaviour; each empty category now reports 0.0%. Regression
test feeds a file with Blank=Comment=Complexity=0 and asserts no NaN in either
the short or the wide output.
@pr-insights pr-insights Bot added L/complexity Low complexity M/size Normal or medium sized change labels Aug 10, 2026
@boyter

boyter commented Aug 11, 2026

Copy link
Copy Markdown
Owner

Odd. I thought I had caught all of those. Thanks.

@boyter
boyter merged commit 50ea91a into boyter:master Aug 11, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

L/complexity Low complexity M/size Normal or medium sized change

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants