|
| 1 | +--- |
| 2 | +description: 'Guidelines for GitHub Copilot to write comments to achieve self-explanatory code with less comments. Examples are in JavaScript but it should work on any language that has comments.' |
| 3 | +applyTo: '**' |
| 4 | +--- |
| 5 | + |
| 6 | +# Self-explanatory Code Commenting Instructions |
| 7 | + |
| 8 | +## Core Principle |
| 9 | +**Write code that speaks for itself. Comment only when necessary to explain WHY, not WHAT.** |
| 10 | +We do not need comments most of the time. |
| 11 | + |
| 12 | +## Commenting Guidelines |
| 13 | + |
| 14 | +### ❌ AVOID These Comment Types |
| 15 | + |
| 16 | +**Obvious Comments** |
| 17 | +```javascript |
| 18 | +// Bad: States the obvious |
| 19 | +let counter = 0; // Initialize counter to zero |
| 20 | +counter++; // Increment counter by one |
| 21 | +``` |
| 22 | + |
| 23 | +**Redundant Comments** |
| 24 | +```javascript |
| 25 | +// Bad: Comment repeats the code |
| 26 | +function getUserName() { |
| 27 | + return user.name; // Return the user's name |
| 28 | +} |
| 29 | +``` |
| 30 | + |
| 31 | +**Outdated Comments** |
| 32 | +```javascript |
| 33 | +// Bad: Comment doesn't match the code |
| 34 | +// Calculate tax at 5% rate |
| 35 | +const tax = price * 0.08; // Actually 8% |
| 36 | +``` |
| 37 | + |
| 38 | +### ✅ WRITE These Comment Types |
| 39 | + |
| 40 | +**Complex Business Logic** |
| 41 | +```javascript |
| 42 | +// Good: Explains WHY this specific calculation |
| 43 | +// Apply progressive tax brackets: 10% up to 10k, 20% above |
| 44 | +const tax = calculateProgressiveTax(income, [0.10, 0.20], [10000]); |
| 45 | +``` |
| 46 | + |
| 47 | +**Non-obvious Algorithms** |
| 48 | +```javascript |
| 49 | +// Good: Explains the algorithm choice |
| 50 | +// Using Floyd-Warshall for all-pairs shortest paths |
| 51 | +// because we need distances between all nodes |
| 52 | +for (let k = 0; k < vertices; k++) { |
| 53 | + for (let i = 0; i < vertices; i++) { |
| 54 | + for (let j = 0; j < vertices; j++) { |
| 55 | + // ... implementation |
| 56 | + } |
| 57 | + } |
| 58 | +} |
| 59 | +``` |
| 60 | + |
| 61 | +**Regex Patterns** |
| 62 | +```javascript |
| 63 | +// Good: Explains what the regex matches |
| 64 | +// Match email format: username@domain.extension |
| 65 | +const emailPattern = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/; |
| 66 | +``` |
| 67 | + |
| 68 | +**API Constraints or Gotchas** |
| 69 | +```javascript |
| 70 | +// Good: Explains external constraint |
| 71 | +// GitHub API rate limit: 5000 requests/hour for authenticated users |
| 72 | +await rateLimiter.wait(); |
| 73 | +const response = await fetch(githubApiUrl); |
| 74 | +``` |
| 75 | + |
| 76 | +## Decision Framework |
| 77 | + |
| 78 | +Before writing a comment, ask: |
| 79 | +1. **Is the code self-explanatory?** → No comment needed |
| 80 | +2. **Would a better variable/function name eliminate the need?** → Refactor instead |
| 81 | +3. **Does this explain WHY, not WHAT?** → Good comment |
| 82 | +4. **Will this help future maintainers?** → Good comment |
| 83 | + |
| 84 | +## Special Cases for Comments |
| 85 | + |
| 86 | +### Public APIs |
| 87 | +```javascript |
| 88 | +/** |
| 89 | + * Calculate compound interest using the standard formula. |
| 90 | + * |
| 91 | + * @param {number} principal - Initial amount invested |
| 92 | + * @param {number} rate - Annual interest rate (as decimal, e.g., 0.05 for 5%) |
| 93 | + * @param {number} time - Time period in years |
| 94 | + * @param {number} compoundFrequency - How many times per year interest compounds (default: 1) |
| 95 | + * @returns {number} Final amount after compound interest |
| 96 | + */ |
| 97 | +function calculateCompoundInterest(principal, rate, time, compoundFrequency = 1) { |
| 98 | + // ... implementation |
| 99 | +} |
| 100 | +``` |
| 101 | + |
| 102 | +### Configuration and Constants |
| 103 | +```javascript |
| 104 | +// Good: Explains the source or reasoning |
| 105 | +const MAX_RETRIES = 3; // Based on network reliability studies |
| 106 | +const API_TIMEOUT = 5000; // AWS Lambda timeout is 15s, leaving buffer |
| 107 | +``` |
| 108 | + |
| 109 | +### Annotations |
| 110 | +```javascript |
| 111 | +// TODO: Replace with proper user authentication after security review |
| 112 | +// FIXME: Memory leak in production - investigate connection pooling |
| 113 | +// HACK: Workaround for bug in library v2.1.0 - remove after upgrade |
| 114 | +// NOTE: This implementation assumes UTC timezone for all calculations |
| 115 | +// WARNING: This function modifies the original array instead of creating a copy |
| 116 | +// PERF: Consider caching this result if called frequently in hot path |
| 117 | +// SECURITY: Validate input to prevent SQL injection before using in query |
| 118 | +// BUG: Edge case failure when array is empty - needs investigation |
| 119 | +// REFACTOR: Extract this logic into separate utility function for reusability |
| 120 | +// DEPRECATED: Use newApiFunction() instead - this will be removed in v3.0 |
| 121 | +``` |
| 122 | + |
| 123 | +## Anti-Patterns to Avoid |
| 124 | + |
| 125 | +### Dead Code Comments |
| 126 | +```javascript |
| 127 | +// Bad: Don't comment out code |
| 128 | +// const oldFunction = () => { ... }; |
| 129 | +const newFunction = () => { ... }; |
| 130 | +``` |
| 131 | + |
| 132 | +### Changelog Comments |
| 133 | +```javascript |
| 134 | +// Bad: Don't maintain history in comments |
| 135 | +// Modified by John on 2023-01-15 |
| 136 | +// Fixed bug reported by Sarah on 2023-02-03 |
| 137 | +function processData() { |
| 138 | + // ... implementation |
| 139 | +} |
| 140 | +``` |
| 141 | + |
| 142 | +### Divider Comments |
| 143 | +```javascript |
| 144 | +// Bad: Don't use decorative comments |
| 145 | +//===================================== |
| 146 | +// UTILITY FUNCTIONS |
| 147 | +//===================================== |
| 148 | +``` |
| 149 | + |
| 150 | +## Quality Checklist |
| 151 | + |
| 152 | +Before committing, ensure your comments: |
| 153 | +- [ ] Explain WHY, not WHAT |
| 154 | +- [ ] Are grammatically correct and clear |
| 155 | +- [ ] Will remain accurate as code evolves |
| 156 | +- [ ] Add genuine value to code understanding |
| 157 | +- [ ] Are placed appropriately (above the code they describe) |
| 158 | +- [ ] Use proper spelling and professional language |
| 159 | + |
| 160 | +## Summary |
| 161 | + |
| 162 | +Remember: **The best comment is the one you don't need to write because the code is self-documenting.** |
0 commit comments