DEV Community

Cover image for Clean code tip: use the same name for the same concept
Davide Bellone
Davide Bellone

Posted on • Originally published at code4it.dev on

Clean code tip: use the same name for the same concept

As I always say, naming things is hard. We've already talked about this in a previous article.

By creating a simple and coherent dictionary, your classes will have better names because you are representing the same idea with the same name. This improves code readability and searchability. Also, by simply looking at the names of your classes you can grasp the meaning of them.

Say that we have 3 objects that perform similar operations: they download some content from external sources.

class YouTubeDownloader {    }

class TwitterDownloadManager {    }

class FacebookDownloadHandler {    }
Enter fullscreen mode Exit fullscreen mode

Here we are using 3 words to use the same concept: Downloader, DownloadManager, DownloadHandler. Why??

So, if you want to see similar classes, you can't even search for "Downloader" on your IDE.

The solution? Use the same name to indicate the same concept!

class YouTubeDownloader {    }

class TwitterDownloader {    }

class FacebookDownloader {    }
Enter fullscreen mode Exit fullscreen mode

It's as simple as that! Just a small change can drastically improve the readability and usability of your code!

So, consider also this small kind of issue when reviewing PRs.

Conclusion

A common dictionary helps to understand the code without misunderstandings. Of course, this tip does not refer only to class names, but to variables too. Avoid using synonyms for objects (eg: video and clip). Instead of synonyms, use more specific names (YouTubeVideo instead of Video).

Any other ideas?

πŸ‘‰ Let's discuss it on Twitter or on the comment section below!

🐧 Previously seen on Code4IT 🐧

Top comments (0)