- Resources & Library
- For Developers & Technical Teams
- Code Quality & Practices
Clear Names Are Documentation That Lives with the Code
A good variable or function name exposes intent immediately and reduces time spent interpreting code or following stale comments.
Omar Alalwi Article
Code communicates with developers before it instructs a computer. An expression such as totalAmount = price × quantity exposes intent, while names such as x, a, and b force the reader to search the surrounding context for the same information.
Name a function for the result it achieves: generateInvoicePdf instead of generateFile, and sendOrderConfirmationEmail instead of processOrderEmail. Use plural names for collections and singular names for elements. Make Boolean names read as natural questions, commonly beginning with is, has, can, or should.
Avoid unfamiliar abbreviations and generic names such as data, info, and process. Comments should explain a reason or constraint that the code cannot express, not repeat what a good name already says. Naming takes a little time once and repays it in every later reading, review, and change.
Share your perspective
I’d be glad to hear your perspective. Leave a comment on the original article on social media.