0

I recognize that there are situations in which "my" is semantically useful, but I have met multiple professional programmers that have a habit of using this everywhere that it's not - "my_var", "myTemp", etc. I recently discovered that my friend's professor is using this in his assignments, which made me wonder if there is actually some historical reason for this.

My own pet peeve aside, is there a historical reason for this?

3
  • 4
    You think a professor is a professional programmer? Well, I guess you have an answer now. :)
    – freakish
    Commented May 25 at 18:38
  • 2
    use of names prefixed by 'my' (class MyClass, void MyFunction(), int myInt; etc, are a great way to avoid using metaphorical names and contrived examples which might initially appear to be "real-world-related" but instead end up conveying completely the wrong semantics that actually mislead and confuse the reader. For example, articles which use names like Vehicle or Animal frequently cause all kinds of confusion when readers start to take the metaphors literally and simply causes confusion when readers end up focusing more on the metaphor rather than the programming concepts. Commented May 26 at 10:52
  • I'd also say, as the answer below suggests, the "my" prefix comes from practice examples and homework and such. But some people might be using this in a very specific way. In the professional world, there's also a common practice to prefix class member fields with m_ or _ - a form (and a relic) of at one time widely spread Hungarian notation. I'm guessing there are people who are using my as a variant of this (where, in methods, "my" indicates the object is referring to a variable it owns). Commented May 26 at 14:48

2 Answers 2

13

It comes from small, trivial pieces of code used in programming examples when there is no context and the actual purpose of the variable is irrelevant.

var myThing = new Thing()

The only two important aspects of the variable myThing is that it is an instance of Thing, and that "I" created it. It has no purpose and no meaning apart from that, so it is impossible to give it a meaningful name. Calling it "myThing" draws attention to its only two important aspect. In that sense the name is (minimally) meaningful.

That's the historical reason.

However because of this, and the lack of any meaningful information conveyed by the name about content or use, the use of my* variables should be entirely restricted to programming examples. Any use in a production environment should be shunned, and anybody who practices it there should be beaten with sticks and sent on a remedial programming course.

In the example above a professor is highly likely to be giving students programming examples and its use is fine there. Anywhere else it is anathema.

2
  • I just want to point out the caveat where "my" could refer to something in the application domain. E.g. ticket trackers often have a "My Tickets" page or similar where you might see the "my" prefix used in a non-example scenario. But edge case aside, yeah I agree with this answer.
    – Flater
    Commented May 27 at 0:34
  • 1
    And even then var thing = new Thing() is an improvement
    – inaba
    Commented May 28 at 9:37
1

The use of my has origins that predate modern higher order programming languages, some early OO languages even adopted this as a reserved keyword. In general, my as both a keyword and a concept, indicates that the variable is scoped to the local context of that code.

Similar to Hello World, when learning to code (or teaching to code), my is used as a prefix to differentiate between arguments passed or referenced or existing code in a pre-templated work example, and the code that is written by the student themselves.

  • This is a useful convention to explain many examples of early code without the student needing to fully understand inheritance or other code composition complexities. For many students it would be hard to explain those concept without reducing the variables down to mine (my) vs yours or theirs.

You would not expect to see my prefix in production code but in learning contexts it is a lesser evil.  

In the related question Use of "my" prefix in object naming there is an answer suggesting the origin is Win98 and the use of the My folders for the current user's profile folders like My Documents. This actually started in Windows 95, but the convention of using this in learning to code pre-dates even this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.