1.6 KiB
refactor: extract shuffleArray to lib/utils, rename correctAnswers to terms
Problem
Two readability issues in gameService.ts:
-
shufflewas defined as a private function at the bottom ofgameService.ts, after the function that calls it. It is a pure generic utility with no dependency on game domain logic, so it had no business living there. -
The variable holding terms fetched from the database was named
correctAnswers. These are word pairs — they only become "correct answers" once options are built around them. The name was premature and misleading.
Options considered
Option A — Move shuffle up in the same file
Simple, no new files. Fixes the ordering issue but keeps a generic utility buried in domain code.
Option B — Extract to lib/utils.ts ✅
Move shuffle (renamed shuffleArray) to apps/api/src/lib/utils.ts and import it. Cleaner separation: domain logic stays in services, generic utilities live in lib/.
Chosen because lib/ already exists, the function is reusable, and it gives future utilities a home.
Solution
- Created
apps/api/src/lib/utils.tswithshuffleArray - Renamed
shuffle→shuffleArrayfor clarity at the call site - Removed the inline
shufflefromgameService.tsand imported fromlib/utils.ts - Renamed
correctAnswers→termsandcorrectAnswer→termthroughoutgameService.ts
Files changed
apps/api/src/lib/utils.ts— createdapps/api/src/services/gameService.ts— removedshuffle, updated import, renamed variables
Commit
refactor: extract shuffleArray to lib/utils, rename correctAnswers to terms