DEV Community

Cover image for Code Smell 96 - My Objects
Maxi Contieri
Maxi Contieri

Posted on • Originally published at maximilianocontieri.com

3 1

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

  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;
Enter fullscreen mode Exit fullscreen mode

Right

MainWindow salesWindow = Application.Current.MainWindow as MainWindow;

/*

Since window is instanciated, we are currently working
with a specialized window playing a special role

*/
Enter fullscreen mode Exit fullscreen mode

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.

Billboard image

Deploy and scale your apps on AWS and GCP with a world class developer experience

Coherence makes it easy to set up and maintain cloud infrastructure. Harness the extensibility, compliance and cost efficiency of the cloud.

Learn more

Top comments (0)

Heroku

This site is powered by Heroku

Heroku was created by developers, for developers. Get started today and find out why Heroku has been the platform of choice for brands like DEV for over a decade.

Sign Up

👋 Kindness is contagious

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

Okay