This document explains the inline comments added to index.html for new developer onboarding. Comments follow enterprise standards for clarity, maintainability, and team collaboration.
/* ============================================================================
* SECTION TITLE
* ============================================================================
* Brief description of what this section does and why it exists.
*
* Additional implementation details, gotchas, or important notes.
* ========================================================================= */Used for:
- State variable declarations
- Major function groups
- Critical subsystems
/* ============================================================================
* FUNCTION NAME: Purpose
* ============================================================================
* Detailed explanation of what the function does.
*
* @param {type} paramName - Parameter description
* @returns {type} - Return value description
*
* Implementation Details:
* - Key point 1
* - Key point 2
*
* Called by: FunctionA(), FunctionB()
* Calls: FunctionC(), FunctionD()
* ========================================================================= */seconds = workerSeconds; // Update global seconds variable
updateDisplay(); // Update UI with new timeUsed for:
- Non-obvious variable assignments
- Important function calls
- State changes
// PREVENT starting new task if timer is already running
if (isTimerRunning) {
return alert("Timer is already running!");
}Used for:
- Validation checks
- Guard clauses
- Business logic decisions
/* ============================================================================
* APPLICATION STATE VARIABLES
* ============================================================================Documents:
- All global state variables
- Purpose of each variable
- Expected values/ranges
- Relationship to other variables
Example:
let seconds = 900; // Current countdown seconds (15 min = 900 sec)
let completedSessions = 0; // Number of completed sessions (0-4)/* ============================================================================
* HELPER FUNCTION: Duplicate Task Detection
* ============================================================================Documents:
- Function purpose
- Parameter types
- Return value
- Implementation approach (case-insensitive, etc.)
- Edge cases handled
/* ============================================================================
* AUDIO CONTEXT UNLOCKING
* ============================================================================Documents:
- Why audio unlocking is needed (browser autoplay policies)
- When it's called
- How it works (play → pause immediately)
/* ============================================================================
* WEB WORKER INITIALIZATION
* ============================================================================Documents:
- Purpose of Web Worker
- Message types handled
- Fallback behavior
/* ============================================================================
* STATE PERSISTENCE: Save to LocalStorage
* ============================================================================Documents:
- When function is called
- What data is saved
- Why each field is important
- State structure
-
Explain WHY, not just WHAT
// GOOD isTimerRunning = true; // Prevent starting multiple timers simultaneously // BAD isTimerRunning = true; // Set flag to true
-
Document Business Logic
// GOOD if (completedSessions >= 4) { // ENFORCE break: 4 sessions = 1 hour work → mandatory 10-min break } // BAD if (completedSessions >= 4) { // Show modal }
-
Clarify Non-Obvious Code
// GOOD audio.pause(); // Immediately pause to avoid actual playback audio.currentTime = 0; // Reset to beginning for next use // BAD audio.pause(); audio.currentTime = 0;
-
Link Related Functions
// Called by: startTimer(), extendTask(), onTimerComplete()
-
State the Obvious
// BAD let x = 5; // Set x to 5
-
Redundant Comments
// BAD // Increment counter counter++;
-
Outdated Comments
// BAD (if code changed but comment didn't) // Check if count is less than 5 if (count >= 10) { ... }
startTimer()- Initiates 15-minute countdownstartFallbackTimer()- Backup timer using setIntervalonTimerComplete()- Handles timer completion logic
saveTimerState()- Persists state to localStoragerestoreTimerState()- Recovers state after reloadclearTimerState()- Removes saved state
updateSessionCounter()- Updates progress displayshowBreakModal()- Displays break promptstartBreak()- Initiates 10-minute break
addActiveTaskToLog()- Adds task to historyupdateActiveTaskStatus()- Updates task statusisTaskAlreadyCompleted()- Checks for duplicates
extendTask()- Handles task extensionsshowFocusWarning()- Displays warning modals
let isTimerRunning = false; // "is" prefix for boolean
let audioUnlocked = false; // Past tense for state flags
let isBreakMode = false; // "is" prefix for mode flagslet completedSessions = 0; // Clear noun describing what's counted
let consecutiveExtensions = 0; // Adjective + noun for specificity
let iterationCount = 0; // Counter nounlet currentActiveRow = null; // "current" prefix for active reference
let timerWorker = null; // Type suffix for clarity
let startTime = null; // Action + type naming'timerState' // Complete timer state object
'completedSessions' // Pomodoro cycle progress
'theme' // User theme preference
'focusLog' // Task history array// Validate input before proceeding
if (!task) return alert("Please enter your focus task.");
// Prevent duplicate actions
if (isTimerRunning) {
return alert("Timer is already running!");
}// Work → Break transition
isBreakMode = true;
completedSessions = 0; // Reset cycle
updateSessionCounter();try {
// Attempt Web Worker initialization
timerWorker = new Worker('timer-worker.js');
} catch (error) {
// Fallback to setInterval
console.warn('Web Worker not available, using fallback timer');
}- ✅ Read through state variable comments (lines 363-389)
- ✅ Understand timer flow:
startTimer()→onTimerComplete() - ✅ Study Pomodoro cycle:
completedSessionstracking - ✅ Review state persistence:
saveTimerState()/restoreTimerState() - ✅ Examine validation:
isTaskAlreadyCompleted(), timer lock logic
index.html- Main application (single-file architecture)timer-worker.js- Background timer threadREADME.md- Feature overview and usageIMPLEMENTATION_SUMMARY.md- Technical documentation
Can you answer these questions after reading the comments?
- Why do we use Web Workers instead of setInterval?
- What happens if user reloads page during timer?
- When does the break modal appear?
- How are duplicate tasks prevented?
- Why is audio unlocking needed?
MUST Update:
- When changing function behavior
- When modifying state logic
- When adding new parameters
- When changing business rules
SHOULD Update:
- When renaming variables
- When adding edge cases
- When improving error handling
Example:
// BEFORE
// Check if sessions done
if (completedSessions === 4) { ... }
// AFTER (code changed to >=)
// Check if break is needed (after 4+ sessions)
if (completedSessions >= 4) { ... }- Comments match actual code behavior
- No outdated function/variable names
- Business logic clearly explained
- Edge cases documented
- Related functions cross-referenced
- Clarity Over Brevity - Better to over-explain than under-explain
- Consistency - Follow established comment patterns
- Context - Explain the "why" behind decisions
- Maintenance - Update comments when code changes
- Cross-References - Link related functions/sections
Comments are code documentation—treat them as first-class citizens!