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?
This is typical programming practice for defining variables that are member variables. So when you're using them later, you don't need to see where they're defined to know their scope. This is also great if you already know the scope and you're using something like intelliSense, you can start with m_
and a list of all your member variables are shown. Part of Hungarian notation, see the part about scope in the examples here.
this->
is nice, the problem is indeed that you can't really enforce that. I like this convention though. –
Funest m_
either. –
Rickey m_
prefixes in C++ as all members are more or less declared at the same place in a header. For this->
on the other hand, I'd say it's more likely to let a few non-prefixed instances slip in method bodies. But yeah, if people decide they don't want to follow this kind of convention, of if they just don't pay attention, you're screwed with both solutions anyway. –
Funest In Clean Code: A Handbook of Agile Software Craftsmanship there is an explicit recommendation against the usage of this prefix:
You also don't need to prefix member variables with
m_
anymore. Your classes and functions should be small enough that you don't need them.
There is also an example (C# code) of this:
Bad practice:
public class Part
{
private String m_dsc; // The textual description
void SetName(string name)
{
m_dsc = name;
}
}
Good practice:
public class Part
{
private String description;
void SetDescription(string description)
{
this.description = description;
}
}
We count with language constructs to refer to member variables in the case of explicitly ambiguity (i.e., description
member and description
parameter): this
.
_
: Use camel casing when naming private or internal fields, and prefix them with _ . See this link ("When editing C# code that follows these naming conventions in an IDE that supports statement completion, typing _ will show all of the object-scoped members.") –
Nashbar m_
prefix is a very small part of the improvements. The main takeaways from the "good practice" is that you should not abbreviate identifiers and use this
when possible. The m_
prefix could easily be added to the good practice without negatively affect it IMO. I am biased though. I really do not like the confusion of non-unique identifiers, even if the compiler might be able to sort it out. –
Uncivil 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.
class Person
{
public:
std::string name() const;
private:
std::string name; // This would lead to a compilation error.
std::string m_name; // OK.
};
main.cpp:9:19: error: duplicate member 'name' std::string name; ^ main.cpp:6:19: note: previous declaration is here std::string name() const; ^ 1 error generated.
"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.
_t
suffix for types, which is also reserved by POSIX, but people use it anyways. –
Aphesis The m_
prefix is often used for member variables - I think its main advantage is that it helps create a clear distinction between a public property and the private member variable backing it:
int m_something
public int Something => this.m_something;
It can help to have a consistent naming convention for backing variables, and the m_
prefix is one way of doing that - one that works in case-insensitive languages.
How useful this is depends on the languages and the tools that you're using. Modern IDEs with strong refactor tools and intellisense have less need for conventions like this, and it's certainly not the only way of doing this, but it's worth being aware of the practice in any case.
this.
in your language, then m_
is really useless. –
Rael m_
is to distinguish it from the property it backs - so this.Something
for the property vs this.m_something
for the backing member. It's not a convention I prefer myself, but I have mostly seen it used in case insensitive languages (like VB). –
Fitment this.Something
for the property and this.something
for the backing? Or this._something
for the backing? this.m_something
is redundant. I use _something
so that I don't accidently type it when I go to type Something
, nothing to do with membershipness or not –
Coral _
prefix on it's own would do the job, but m_
is the convention. It's not one that I'd use personally, but if you see it in code that was the intent of the author. –
Fitment 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.
m_description
vs description
. –
Howells Lockheed Martin uses a 3-prefix naming scheme which was wonderful to work with, especially when reading others' code.
Scope Reference Type(*Case-by-Case) Type
member m pointer p integer n
argument a reference r short n
local l float f
double f
boolean b
So...
int A::methodCall(float af_Argument1, int* apn_Arg2)
{
lpn_Temp = apn_Arg2;
mpf_Oops = lpn_Temp; // Here I can see I made a mistake, I should not assign an int* to a float*
}
Take it for what's it worth.
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:
Others have mentioned that it means a class member. Qt is a popular c++ Framework that uses this notation so alot of C++ GUI tutorial use m_
. You can see almost all their examples use m_
for class members. Personally, I use m_
as it is shorter than this->
and feels compact.
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 - and g_
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 as static
.
See EDK2 (a UEFI Open-Source implementation) coding convention for an example of project using this convention.
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.
© 2022 - 2024 — McMap. All rights reserved.
m_
for, yet half the responses here are a commentary on why everyone thinks their current favorite is the best. – Trude