Skip to content

Function Best Practices

Use ✅Don’t use ❌
arrow functionsfunction expression / declaration
async/awaitpromise chaining
named exportsdefault exports
  • 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.
  • Duplicated logic means duplicated bugs.
  • If you see the same code in 3+ places, extract a helper.
  • 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 }) { ... }
  • 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 }).