You don't own objects.
TL;DR: don't use my as a name prefix.
Problems
Lack of context
Bijection Fault
Solutions
Remove my prefix.
Change to a role suggesting name.
Context
Several old tutorials use the word 'my' as a lazy name.
This is vague and lead to context mistakes.
Sample Code
Wrong
MainWindow myWindow = Application.Current.MainWindow as MainWindow;
Right
MainWindow salesWindow = Application.Current.MainWindow as MainWindow;
/*
Since window is instanciated, we are currently working
with a specialized window playing a special role
*/
Detection
[x] Automatic
We can tell our linters and static checkers to search for this prefix and warn us.
Tags
- Naming
Conclusion
Avoid using my. Objects change according to the usage context.
More Info
Credits
Photo by Michał Bożek on Unsplash
Thinking about my experience of modifying code, I see that I spend much more time reading the existing code than I do writing new code. If I want to make my code cheap, therefore, I should make it easy to read.
Kent Beck
![mcsee](https://res.cloudinary.com/practicaldev/image/fetch/s--by_E26Wt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://res.cloudinary.com/practicaldev/image/fetch/s--XvnZDWAZ--/c_fill%2Cf_auto%2Cfl_progressive%2Ch_150%2Cq_auto%2Cw_150/https://dev-to-uploads.s3.amazonaws.com/uploads/user/profile_image/366059/69feb7c3-ab01-4bcd-a6f5-ec38e9300da3.jpeg)
Software Engineering Great Quotes
Maxi Contieri ・ Dec 28 '20
This article is part of the CodeSmell Series.
Top comments (0)