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

3

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 🐧

Sentry image

See why 4M developers consider Sentry, β€œnot bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay