This content originally appeared on DEV Community and was authored by Malte Riechmann
Naming things is hard
You always have to keep in mind, software developers spend more time reading source code than actually writing it. A lot of other people will someday read the source code you are writing today. Better make sure to write it clear, consistent, and well structured. I can recommend reading Clean Code by Robert C. Martin.
Naming things is one part of writing clean code and often it is quite hard to find the right names. In the following, I just want to show you one easy step to improve your source code.
Avoid defective nouns
Defective nouns are nouns without singulars, nouns without plurals, or nouns that have the same singular and plural (e. g. information, music, or news). See Wikipedia for more details.
Usually, in software development, you will need a noun that differs in singular and plural. So you are well-advised not to use defective nouns.
Example
Think about the list of posts on DEV. How would you name those? There are multiple options, but you should definitely avoid a defective noun.
❌ Bad: newss/news
newss.forEach(function(news) {
news.doSomething();
});
❌ Bad: newsList/news
newsList.forEach(function(news) {
news.doSomething();
});
❌ Bad: news/singleNews
news.forEach(function(singleNews) {
currentNews.doSomething();
});
✅ Good: posts/post
posts.forEach(function(post) {
post.doSomething();
});
Bonus: Irregular plural nouns
I will tell you a little secret. This might be a bit fussy, but I try to avoid irregular plural nouns, too. I do not like medium and media, child and children, or radius and radii. I just like good ol' regular plural nouns like post and posts, user and users, list and lists.
This content originally appeared on DEV Community and was authored by Malte Riechmann
Malte Riechmann | Sciencx (2021-04-30T08:01:22+00:00) Avoid defective nouns when naming things. Retrieved from https://www.scien.cx/2021/04/30/avoid-defective-nouns-when-naming-things/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.