Naming Variables, Functions, Methods, etc.
Code should tell a story.
Code should read like a book, describing exactly what's happening in plain English. We make this possible by being mindful to assign meaningful names to our variables, functions, methods, directories, files, and anything else. Younger developers tend to struggle with naming, which is understandable. Below are some tips and tricks I use to ensure the name I assign today has meaning to six-months-from-now me.
- Be descriptive
- The name should adequately describe what it is or what it's doing. Keep in mind to describe what it's assigning/returning and, if applicable, from where.
- Be explicit & specific
- Avoid abbreviations
- People abbreviate things differently, so I've found it's best to just avoid abbreviations all together.
- Trust your gut or first instinct
- Don't try to be cute or funny
- Avoid jargon
- Don't overthink it
- The first thing that comes to mind is usually the thing you'll remember in six months.
- Don't hesitate to use a longer name
- If it takes four words to adequately describe it, use four words. If it takes 10, use 10. We're after clarity. It may feel weird at first, but you'll thank yourself six months from now when you work with this section of code again.
- Follow the language-specific guidelines and/or project coding standards for proper casing
Pro Tip:
If the name you think is best is already in use, there's a good chance the code needs to be refactored.
Here's a cool naming cheatsheet:
https://github.com/kettanaito/naming-cheatsheet