I often see m_
prefix used for variables (m_World
,m_Sprites
,...) in tutorials, examples and other code mainly related to game development.
Why do people add prefix m_
to variables?
I often see m_
prefix used for variables (m_World
,m_Sprites
,...) in tutorials, examples and other code mainly related to game development.
Why do people add prefix m_
to variables?
It is common practice in C++. This is because in C++ you can't have same name for the member function and member variable, and getter functions are often named without "get" prefix.
"m_" states for the "member". Prefix "_" is also common.
You shouldn't use it in programming languages that solve this problem by using different conventions/grammar.
To complete the current answers and as the question is not language specific, some C-project use the prefix
m_
to define global variables that are specific to a file - andg_
for global variables that have a scoped larger than the file they are defined.In this case global variables defined with prefix
m_
should be defined asstatic
.See EDK2 (a UEFI Open-Source implementation) coding convention for an example of project using this convention.
As stated in the other answers,
m_
prefix is used to indicate that a variable is a class member. This is different from Hungarian notation because it doesn't indicate the type of the variable but its context.I use
m_
in C++ but not in some other languages where 'this' or 'self' is compulsory. I don't like to see 'this->' used with C++ because it clutters the code.Another answer says
m_dsc
is "bad practice" and 'description;' is "good practice" but this is a red herring because the problem there is the abbreviation.Another answer says typing
this
pops up IntelliSense but any good IDE will have a hotkey to pop up IntelliSense for the current class members.In Clean Code: A Handbook of Agile Software Craftsmanship there is an explicit recommendation about the usage of this prefix:
There is also an example (C# code) of this:
Bad practice:
Good practice:
We count with language constructs to refer to member variables in the case of explicitly ambiguity (i.e.,
description
member anddescription
parameter):this
.As stated in many other responses, m_ is a prefix that denotes member variables. It is/was commonly used in the C++ world and propagated to other languages too, including Java.
In a modern IDE it is completely redundant as the syntax highlighting makes it evident which variables are local and which ones are members. However, by the time syntax highlighting appeared in the late 90s, the convention had been around for many years and was firmly set (at least in the C++ world).
I do not know which tutorials you are referring to, but I will guess that they are using the convention due to one of two factors:
One argument that I haven't seen yet is that a prefix such as
m_
can be used to prevent name clashing with#define
'd macro's.Regex search for
#define [a-z][A-Za-z0-9_]*[^(]
in/usr/include/term.h
from curses/ncurses.