Clean code naming - make functions read like sentences

Clean code naming - make functions read like sentences

TL;DR: Express intent in names. Prefer specific verbs and nouns, avoid noise words, and keep booleans phrased as questions. Enforce with a small checklist in reviews.

Why names fail

  • Too generic: handleData
  • Too specific: handleDataFromOrdersCsv
  • Inconsistent terms across modules

Rules of thumb

  • Functions: verb + object. Example: validateToken.
  • Booleans: answer a question. Example: isAuthorized.
  • Parameters: carry units and meaning. Example: timeoutMs.

Before and after

- function doStuff(d) { return x(d) }
+ function normalizePrices(order) { return convertToCents(order) }
- let flag = false
+ let isRetryEnabled = false

Linter tips

  • Enable no-shadow and no-unused-vars to catch sloppy names.
  • Use a naming convention plugin if available.

Review checklist

  • Does the name match the behavior
  • Is the scope small enough that the name is not misleading
  • Are related names consistent across files

FAQ

How long should names be
As long as needed to be clear. Do not abbreviate meaning to save a few characters.

Do good names replace comments
Good names reduce the need for comments, but non-obvious decisions still deserve a short comment.

Cast this in your project

Image credit: Photo by Pixabay: https://www.pexels.com/photo/brown-wooden-floor-48889/

Back to blog

Leave a comment

Please note, comments need to be approved before they are published.