Function Best Practices
| Use ✅ | Don’t use ❌ |
|---|---|
| arrow functions | function expression / declaration |
| async/await | promise chaining |
| named exports | default exports |
Do one thing
Section titled “Do one thing”- A function should focus on a single responsibility.
- Easier to test, reuse, and refactor when isolated.
- If it’s doing multiple steps (validate + save + notify), break it down.
Avoid duplication
Section titled “Avoid duplication”- Duplicated logic means duplicated bugs.
- If you see the same code in 3+ places, extract a helper.
Arguments (keep it small)
Section titled “Arguments (keep it small)”- 1–2 arguments is ideal.
- 3 is a warning sign → consider splitting into smaller functions.
- If many arguments are truly needed, use an options object and destructure for clarity:
function createMenu({ title, body, buttonText, isCancellable }) { ... }No boolean flags
Section titled “No boolean flags”-
Boolean params usually mean the function does more than one thing.
-
Calls like
createFile("foo.txt", true)are unclear. -
Prefer:
- Two explicit functions (
createFile,createTempFile), or - A descriptive options object (
{ isTemporary: true }).
- Two explicit functions (