|
| 1 | +--- |
| 2 | +alwaysApply: true |
| 3 | +description: Guidelines for writing meaningful and maintainable code comments |
| 4 | +--- |
| 5 | + |
| 6 | +# Guidelines for writing meaningful and maintainable code comments |
| 7 | + |
| 8 | +You are an expert in clean code practices, documentation, and code readability. |
| 9 | + |
| 10 | +## When to Comment |
| 11 | + |
| 12 | +- Explain WHY, not WHAT the code does |
| 13 | +- Document complex business logic or algorithms |
| 14 | +- Clarify non-obvious implementations |
| 15 | +- Warn about potential gotchas or side effects |
| 16 | +- Provide context for future maintainers |
| 17 | + |
| 18 | +## Comment Types and Usage |
| 19 | + |
| 20 | +### Good Comments |
| 21 | + |
| 22 | +```javascript |
| 23 | +// Using exponential backoff to avoid overwhelming the API |
| 24 | +// during high traffic periods (see incident report #1234) |
| 25 | +const delay = Math.min(1000 * Math.pow(2, retryCount), 30000); |
| 26 | + |
| 27 | +// WORKAROUND: Chrome bug #123456 requires explicit height |
| 28 | +// Remove when Chrome 120+ adoption reaches 95% |
| 29 | +element.style.height = "auto"; |
| 30 | + |
| 31 | +// Performance: Caching results reduces API calls by ~70% |
| 32 | +// based on production metrics from 2023-Q4 |
| 33 | +const cachedResult = cache.get(key); |
| 34 | +``` |
| 35 | + |
| 36 | +### Bad Comments |
| 37 | + |
| 38 | +```javascript |
| 39 | +// BAD: Obvious comment |
| 40 | +// Increment counter by 1 |
| 41 | +counter++; |
| 42 | + |
| 43 | +// BAD: Outdated comment |
| 44 | +// Send email to user (actually sends SMS now) |
| 45 | +await notificationService.send(user); |
| 46 | + |
| 47 | +// BAD: Commented-out code without context |
| 48 | +// user.setStatus('active'); |
| 49 | +// user.save(); |
| 50 | +``` |
| 51 | + |
| 52 | +## Documentation Comments |
| 53 | + |
| 54 | +### JavaScript/TypeScript |
| 55 | + |
| 56 | +```typescript |
| 57 | +/** |
| 58 | + * Calculates compound interest with monthly contributions. |
| 59 | + * Uses the formula: A = P(1 + r/n)^(nt) + PMT × (((1 + r/n)^(nt) - 1) / (r/n)) |
| 60 | + * |
| 61 | + * @param principal - Initial investment amount |
| 62 | + * @param rate - Annual interest rate (as decimal, e.g., 0.05 for 5%) |
| 63 | + * @param time - Investment period in years |
| 64 | + * @param contribution - Monthly contribution amount |
| 65 | + * @returns Total value after the investment period |
| 66 | + * |
| 67 | + * @example |
| 68 | + * // Calculate 10-year investment with 5% annual return |
| 69 | + * const total = calculateCompoundInterest(10000, 0.05, 10, 500); |
| 70 | + * console.log(total); // 96,859.57 |
| 71 | + */ |
| 72 | +function calculateCompoundInterest( |
| 73 | + principal: number, |
| 74 | + rate: number, |
| 75 | + time: number, |
| 76 | + contribution: number |
| 77 | +): number { |
| 78 | + // Implementation |
| 79 | +} |
| 80 | +``` |
| 81 | + |
| 82 | +### Python |
| 83 | + |
| 84 | +```python |
| 85 | +def process_transaction(transaction: Transaction) -> ProcessResult: |
| 86 | + """ |
| 87 | + Process a financial transaction with fraud detection and validation. |
| 88 | + |
| 89 | + This method performs multiple checks before processing: |
| 90 | + 1. Validates transaction format and required fields |
| 91 | + 2. Checks against fraud detection rules |
| 92 | + 3. Verifies account balance and limits |
| 93 | + 4. Processes through payment gateway |
| 94 | + |
| 95 | + Args: |
| 96 | + transaction: Transaction object containing payment details |
| 97 | + |
| 98 | + Returns: |
| 99 | + ProcessResult with status and any error messages |
| 100 | + |
| 101 | + Raises: |
| 102 | + ValidationError: If transaction format is invalid |
| 103 | + FraudException: If transaction triggers fraud rules |
| 104 | + InsufficientFundsError: If account balance is too low |
| 105 | + |
| 106 | + Note: |
| 107 | + Transactions over $10,000 require additional verification |
| 108 | + per compliance policy FIN-2023-001. |
| 109 | + """ |
| 110 | +``` |
| 111 | + |
| 112 | +## TODO Comments |
| 113 | + |
| 114 | +```python |
| 115 | +# TODO(john): Implement retry logic for network failures |
| 116 | +# TODO(security): Add rate limiting before v2.0 release |
| 117 | +# FIXME: Handle edge case when user has multiple accounts |
| 118 | +# HACK: Temporary fix until database migration completes |
| 119 | +``` |
| 120 | + |
| 121 | +## Best Practices |
| 122 | + |
| 123 | +- Keep comments concise and relevant |
| 124 | +- Update comments when code changes |
| 125 | +- Use consistent comment style across the codebase |
| 126 | +- Avoid humor or cultural references that may not translate |
| 127 | +- Review comments during code reviews |
0 commit comments