domains / app-frameworks / tj/commander.js
Prefer built-in methods
When a standard language feature already does the job, use it instead of a manual or custom reimplementation — this is clearer, less error-prone, and easier to maintain. Examples observed in review:
When a standard language feature already does the job, use it instead of a manual or custom reimplementation — this is clearer, less error-prone, and easier to maintain. Examples observed in review:
- Use
Array.isArray(x)instead of a hand-rolledisArray()helper. - Use
str.endsWith('...')instead ofstr.slice(-3) === '...'plus a redundant length check. - Use
str.startsWith('--no-')instead offlags.indexOf('--no-') !== -1, sinceindexOfcan match the substring anywhere, not just at the start. - Use default function parameters instead of manually checking for
undefined:// instead of allowExcessArguments(allowExcess) { if (allowExcess === undefined) allowExcess = true; ... } // prefer allowExcessArguments(allowExcess = true) { ... } - Use
Object.prototype.hasOwnProperty.call(obj, key)instead of theinoperator when you only want to check the object’s own properties (theinoperator also matches inherited properties). - When adding case-insensitivity or other variants to a regex, prefer a targeted character class (e.g.
[eE]) over a blanket/iflag, so the intent of what varies is explicit rather than silently affecting the whole pattern.
Before writing custom logic, check whether a native method or language feature already covers the case; it communicates intent more clearly to readers and avoids subtle bugs from incomplete reimplementations.