Clean code tip: use the same name for the same concept
Just a second! π«·
If you are here, it means that you are a software developer. So, you know that storage, networking, and domain management have a cost .
If you want to support this blog, please ensure that you have disabled the adblocker for this site. I configured Google AdSense to show as few ADS as possible - I don't want to bother you with lots of ads, but I still need to add some to pay for the resources for my site.
Thank you for your understanding.
- Davide
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 { }
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 { }
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!
π§