Skip to main content

Command Palette

Search for a command to run...

Code Smell 96 - My Objects

Published
1 min read
Code Smell 96 - My Objects
M

I’m a senior software engineer loving clean code, and declarative designs. S.O.L.I.D. and agile methodologies fan.

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

N

Oh, that's a good one Maxi!

I've also seen a lot of NewXXX or CustomXXX, those are evil!

Code Smells

Part 1 of 50

In this series, we will see several symptoms and situations that make us doubt the quality of our developments. We will present possible solutions. Most are just clues. They are no hard rules.