An Interest In:
Web News this Week
- April 20, 2024
- April 19, 2024
- April 18, 2024
- April 17, 2024
- April 16, 2024
- April 15, 2024
- April 14, 2024
October 25, 2021 10:12 pm GMT
Original Link: https://dev.to/mcsee/code-smell-96-my-objects-5cij
Code Smell 96 - My Objects
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 workingwith 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 Boek 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
Software Engineering Great Quotes
Maxi Contieri Dec 28 '20 13 min read
#codenewbie #programming #quotes #software
This article is part of the CodeSmell Series.
Original Link: https://dev.to/mcsee/code-smell-96-my-objects-5cij
Share this article:
Tweet
View Full Article
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To