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 ‘…


This content originally appeared on DEV Community and was authored by Maxi Contieri

You don't own objects.

TL;DR: don't use my as a name prefix.

Problems

  • Lack of context

  • Bijection Fault

Solutions

  1. Remove my prefix.

  2. 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

This article is part of the CodeSmell Series.


This content originally appeared on DEV Community and was authored by Maxi Contieri


Print Share Comment Cite Upload Translate Updates
APA

Maxi Contieri | Sciencx (2021-10-25T22:12:37+00:00) Code Smell 96 – My Objects. Retrieved from https://www.scien.cx/2021/10/25/code-smell-96-my-objects/

MLA
" » Code Smell 96 – My Objects." Maxi Contieri | Sciencx - Monday October 25, 2021, https://www.scien.cx/2021/10/25/code-smell-96-my-objects/
HARVARD
Maxi Contieri | Sciencx Monday October 25, 2021 » Code Smell 96 – My Objects., viewed ,<https://www.scien.cx/2021/10/25/code-smell-96-my-objects/>
VANCOUVER
Maxi Contieri | Sciencx - » Code Smell 96 – My Objects. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/10/25/code-smell-96-my-objects/
CHICAGO
" » Code Smell 96 – My Objects." Maxi Contieri | Sciencx - Accessed . https://www.scien.cx/2021/10/25/code-smell-96-my-objects/
IEEE
" » Code Smell 96 – My Objects." Maxi Contieri | Sciencx [Online]. Available: https://www.scien.cx/2021/10/25/code-smell-96-my-objects/. [Accessed: ]
rf:citation
» Code Smell 96 – My Objects | Maxi Contieri | Sciencx | https://www.scien.cx/2021/10/25/code-smell-96-my-objects/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.