Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
April 30, 2021 08:01 am GMT

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.

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.


Original Link: https://dev.to/visuellverstehen/avoid-defective-nouns-when-naming-things-47jm

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To