Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Malaysian Variables

Status
Not open for further replies.

xwb

Programmer
Jul 11, 2002
6,828
GB
This is not a technical query: just a search for the source of a strange practice.

How often have you seen code where every variable and function begins with my (Internet 2 letter abbreviation for Malaysia hence Malaysian variables)? Something like
Code:
function myCard()
{
   return myFingerPrint + myId + myPicture;
}
I find it annoying, especially when every single variable begins with my. Why can't they use
Code:
function Card()
{
   return fingerprint + id + picture;
}
Why do they have to be so possessive? I first saw it in C++ in 1997. One of the coders used it throughout his C++ and his VBA code. All the variables were Malaysian. I'm very sure it began before that. Many of the VB, VBScript, Java and JavaScript books I've seen have it too but I didn't look in the front to find out the published date.

Does anyone know the origin of this possessive practice? Alternatively, what is your earliest recollection of this possessive practice?
 
I've seen it for at least 10 years, if not more.

I find it to be almost as annoying as people that use 'foo' and 'bar' for all their examples. [thumbsdown]

I think people use it because they can't think for themselves. They see someone use myThis, myThat... all over the place and they think that's how they should be naming things.

I actually had a friend who got a co-op job where he was asked to write a simple function to do something (I forget what), and he actually named the function foo()! :p
 
Prefixing variables with "my" makes sense when writing code examples, just as using foo and bar makes sense in those situations. If you find them in real code, obviously that is probably just bad programming, but I've never seen it in anything other than examples (maybe in some poorly coded homework assignments, too).

The reason that "my" is used in examples is to indicate that the actual purpose of the variable is not relevant to the example. Normally you would name variables to indicate their purpose, but if that purpose is not important, then making one up can cause more confusion by adding unnecessary complexity.

For example, if someone asks how to make a string variable's contents empty, one could provide this example:
Code:
std::string myString = "Hello World!";
// myString.length() is 12.
myString.clear();
// myString.length() is 0.
It would distract from the point of the example to make up extra context necessary for a different variable name.

The same is true for foo and bar. A function, class or variable name is normally based on the purpose or use of that thing, but if the purpose is irrelevant, that can be indicated with a generic term like foo and bar.

Since foo, bar, my and some other terms (like "Hello World!" ) are commonly used for these situations, they work best because the reader is more likely to know why they are being used.
 
I think foobar comes from fubar i.e. f***ed up beyond all recognition. There might be a variant for the oo. My earliest recollection of that in documentation is in the Data General Fortran manual dated 1979. It is just another one of those US Army terms like snafu.
 
foo and bar are metasyntax variables

I use them all the time when posting examples (sorry cpjust).

The general idea is that people will take the examples and replace 'foo' with whatever is meaningful in the context which they want to use it.

Of course, some people even fail on this, and come back with the very obvious "foo is undeclared" error message.

--
 
As far as the origin of "my" goes, I would be surprised if there was a specific beginning to it that can be traced. It is similar to prefixing "a", "the" or "some" to variable names in that it makes some sense on its own, unlike foo and bar which come out of nowhere.
 
I remember calling variables that way in the mid 80's

I still use them when teaching OO programming to state the difference between class and object, but I think some people don't realize when the training ends and the real life begins.

To make you feel better this also happens in other languages like Spanish, people starts variables with "mi"

Cheers,
Dian
 
Never had the problem. Just call all my variables x or y...
Or occasionally i.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top