0

Often in code snippets or examples, the prefix My is used for classes, methods, and variables.

We see this all the time, from Google's gcm example to numerous questions on stackoverflow. It can be found in documentation for PHP, Javascript, C#, and really just about every other language.

So, given it's extreme prevalence, why shouldn't I put MyClass with myMethod using myVar1 into production? Why is this bad practice, and what should I do instead?

Community
  • 1
  • 1
MirroredFate
  • 12,396
  • 14
  • 68
  • 100
  • 1
    The examples use "MyClass" because they can't use just "Class" -- it's a reserved word. Same for "Var", "Function" etc. And since they are samples, they say things like: "Let's assume you have a class called MyClass....." In YOUR code, name your class for what it represents. If it's a user you are modeling, the class name can be "User". If it's a car, maybe "Car", or more generically, "Vehicle". See? – Matt Runion Sep 15 '15 at 19:42

1 Answers1

1

Name are important

Everything else comes down to that one simple fact.

Having clear, meaningful variable names is an important factor in writing understandable and maintainable code. - Computational Fairy Tales

When you write a piece of code, you want anyone reading it (which includes you in three months!) to know what it does almost immediately. In order to remember, you would probably need to add a comment about it. However...

The proper use of comments is to compensate for our failure to express our self in code - Express Names in Code: Bad vs Clean

To help with this, your name should be both descriptive and concise. Generally, when the prefix My is added, it replaces a prefix that could be more informational. Instead of MyService, why not GcmListenerService?

What if I make it descriptive, but I just add My? Like MyGcmListenerService?

That breaks the consice part of the rule. What do you gain from adding My? Is the My adding value to the name? Even if you were attempting to take possession of the code (which would be better done and is feasible using vcs), My is meaningless. Who wrote My? Well, I did, obviously. It says it right there, "My".

If I really shouldn't use My, why is it in so many examples?

Really, it's just a placeholder.

It's like the metasyntactic variables "foo" and "bar" - it's usually used as a placeholder for a real name. - Opinions on using My as a class name prefix

Unfortunately, most experienced programmers just assume that people looking up examples will know this, and they will replace My with a good variable name when they actually use the code. However, for those new to programming, if you see it all over the place, instead of knowing it is a placeholder, you may think it is actually a standard, and best practice to use My.

Ok, then what do I use instead of My?

There are a lot of really good guides about naming variables and classes out there. You can start with wikipedia, but if you google around a bit you can find lots of articles about good vs bad names. At the heart of it all, though, is one rules: make names descriptive, and keep them concise.

If I read the name of a class, method, or variable and immediately know its purpose, it is a good name.

Community
  • 1
  • 1
MirroredFate
  • 12,396
  • 14
  • 68
  • 100