Skip to content

fix: guard rating calculation when ratingCount is zero (fixes #780)#784

Open
dolliecoder wants to merge 1 commit intoAOSSIE-Org:masterfrom
dolliecoder:fix/rating-division-780
Open

fix: guard rating calculation when ratingCount is zero (fixes #780)#784
dolliecoder wants to merge 1 commit intoAOSSIE-Org:masterfrom
dolliecoder:fix/rating-division-780

Conversation

@dolliecoder
Copy link

@dolliecoder dolliecoder commented Mar 2, 2026

Description

Fixes #780

Guards the user rating calculation in explore_story_controller.dart to prevent invalid numeric results when ratingCount is zero or null.

Previously, the rating was calculated as:

userData['ratingTotal'] / userData['ratingCount'];

If ratingCount was 0 or null, this could produce NaN or Infinity, leading to incorrect values being displayed in the UI.

The calculation now safely returns 0.0 when ratingCount is not greater than zero.

Type of change
Bug fix (non-breaking change which fixes an issue)

How Has This Been Tested?

Manually tested with ratingCount = 0
Manually tested with ratingCount = null
Verified valid division when ratingCount > 0
Confirmed no NaN or Infinity values are produced

Checklist:

My code follows the style guidelines of this project
I have performed a self-review of my own code
I have made corresponding changes to the documentation
I have added tests that prove my fix is effective
My changes generate no new warnings

Summary by CodeRabbit

  • Bug Fixes
    • Fixed a potential crash when calculating user ratings in the explore section by adding proper validation to prevent division by zero errors.

@dolliecoder dolliecoder requested a review from M4dhav as a code owner March 2, 2026 13:20
@github-actions
Copy link
Contributor

github-actions bot commented Mar 2, 2026

🎉 Welcome @dolliecoder!
Thank you for your pull request! Our team will review it soon. 🔍

  • Please ensure your PR follows the contribution guidelines. ✅
  • All automated tests should pass before merging. 🔄
  • If this PR fixes an issue, link it in the description. 🔗

We appreciate your contribution! 🚀

@coderabbitai
Copy link

coderabbitai bot commented Mar 2, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info

Configuration used: defaults

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between bf1dbe2 and 3f2d4da.

📒 Files selected for processing (1)
  • lib/controllers/explore_story_controller.dart

📝 Walkthrough

Walkthrough

The changes fix a division by zero bug in explore_story_controller.dart by introducing safe computation of userRating. Before dividing ratingTotal by ratingCount, the code now checks if ratingCount > 0, defaulting to 0.0 if the check fails. This prevents invalid calculations (NaN or Infinity) when rating count is zero.

Changes

Cohort / File(s) Summary
Division by Zero Prevention
lib/controllers/explore_story_controller.dart
Updated convertAppwriteDocListToUserList and convertMeilisearchResultsToUserList methods to safely compute userRating by checking if ratingCount > 0 before division; defaults to 0.0 otherwise, preventing NaN/Infinity results.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐰 A rabbit hops through numbers, safe and sound,
No division by zero shall come around!
When ratings are zero, we don't divide,
Just return 0.0 with mathematical pride ✓

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and specifically describes the main bug fix: guarding the rating calculation against zero division, directly referencing issue #780.
Linked Issues check ✅ Passed The code changes fully satisfy issue #780 requirements by safely computing userRating with zero guards and conditional division to prevent NaN/Infinity values.
Out of Scope Changes check ✅ Passed All changes are directly scoped to fixing the rating calculation logic in convertAppwriteDocListToUserList and convertMeilisearchResultsToUserList, with no extraneous modifications.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@dolliecoder
Copy link
Author

@M4dhav please review this pr

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Potential invalid userRating calculation when ratingCount is zero

1 participant