Jump to content
  • Advertisement
Sign in to follow this  
Kryptus

Quick Question About Variables

This topic is 4431 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've seen MS DX9 sample code where they put an "m_" before the variables. Why is it? Also, why is there sometimes a "p" as well? I understand that if it was an "i" it would be and integer or if it was a "b" it would be a boolean, but what does "p" mean? this is simple enough: int iMouseX, iMouseY; but would I ever do this? int m_iMouseX, m_iMouseY Of course, I know there are no rules on variables, but I'm just asking.

Share this post


Link to post
Share on other sites
Advertisement
It's a form of Hungarian Notation. Don't do it. Jesus will weep so much kittens will drown (yes, I combine my memes).

"i" stands for integer, p for pointer.

The prefix "m_" is used for private variables in classes, usually. Using that is not necessarily a bad thing; As opposed to using Hungarian Notation to denote the type the variable is, you're using it to denote it's exposure. This can be a very good thing, but the major benefit, I've found (I remove the underscore to just have something like mWidth), is that you don't duplicate your variable names with something else. And since most of your members (variables) should be protected/private anyway, you never have the this->x = x problem, because mX is known to be local to the class.

I believe the different is called... Application Hungarian Notation, and Systems Hungarian Notation? I believe snk_kid knows the history very well.

Share this post


Link to post
Share on other sites
p means pointer

its called "hungarian notation"

and me and my closest programmer friends consider it extremely evil.

It is also prohibitted in the .NET naming guidelines as well (even though it used to be the MS recommended way to program in the old Win32 / MFC days).

Its main problem was simple - it was intended to convey MEANING to the user of the program, not just compiler type information, but the second group which adopted it perverted it and ruined it for all time (because compiler type is an easier concept to map, and can be verified by all easily - but since that's true, it is totally unneccesary).

Also this was usefull for code that was very LOW LEVEL and the fact that a thing was signed or unsigned, 16 or 32 bits was what mattered (such as when writing a portable OS API, or a device driver). And it still is usefull for those specific situations (usually written in strait C).

It is not usefull however in an object oriented, high level world. In fact it is the antithesis of OO design when used the way most people use it.

Share this post


Link to post
Share on other sites
'm_' usually denotes a class member.


class Example
{
public:
void Print() {
m_iVar = 10;
std::cout << "Example::Print() -- " << m_iVar << std::endl;
}

private:
int m_iVar;
}



And it can be used for any variable, whether it be another class, an integer, a string, a 'char', etc.

And the 'p' is usually used to denote a pointer to an object.


class Example
{
public:
void Print() {
std::cout << "Example::Print() -- " << m_pObject->Name() << std::endl;
}

private:
MyObject *m_pObject;
}



=)

Share this post


Link to post
Share on other sites
Quote:
Original post by _goat
It's a form of Hungarian Notation. Don't do it. Jesus will weep so much kittens will drown (yes, I combine my memes).

"i" stands for integer, p for pointer.

The prefix "m_" is used for private variables in classes, usually. Using that is not necessarily a bad thing; As opposed to using Hungarian Notation to denote the type the variable is, you're using it to denote it's exposure. This can be a very good thing, but the major benefit, I've found (I remove the underscore to just have something like mWidth), is that you don't duplicate your variable names with something else. And since most of your members (variables) should be protected/private anyway, you never have the this->x = x problem, because mX is known to be local to the class.

I believe the different is called... Application Hungarian Notation, and Systems Hungarian Notation? I believe snk_kid knows the history very well.


The dreaded type prefix is called system hungarian notation (because it was used b y the Windows dev team) and the application hungarian notation ("application" because it was used by the Office team in MS) is a totally different beast - it uses a prefix to specify the content of the variable. Consider a float that will contain a distance, the SHN will name thid variable dDistance, while the AHN will name it mmDistance (mm being the distance unit).

These informations comes from Joel Spolsky's blog (Joel on Software, this specific ticket).

Regards,

Share this post


Link to post
Share on other sites
Is it true? MS finally cleaned up their act? :D. It was bad enough they'd use ALLCAPS for all their types, it made it completely impossible to read when it looked like LPSZCRAPCRAPALLCAPS :).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!