DEV Community

Malte Riechmann for visuellverstehen

Posted on • Updated on • Originally published at happy-coding.visuellverstehen.de

Avoid defective nouns when naming things

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.

❌ Wrong: newss/news

newss.forEach(function(news) {
    news.doSomething();
});
Enter fullscreen mode Exit fullscreen mode

❌ Wrong: newsList/news

newsList.forEach(function(news) {
    news.doSomething();
});
Enter fullscreen mode Exit fullscreen mode

❌ Wrong: news/singleNews

news.forEach(function(singleNews) {
    currentNews.doSomething();
});
Enter fullscreen mode Exit fullscreen mode

✅ Correct: posts/post

posts.forEach(function(post) {
    post.doSomething();
});
Enter fullscreen mode Exit fullscreen mode

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.

Top comments (0)