Why Do Variable Names Often Start with the Letter 'M'

Why do variable names often start with the letter 'm'?

It stands for member. I personally find this convention unhelpful, but it's subjective.

What does the m in mVariableName mean?

It's commonly used to mean that variable is a member of class. For example it's useful in situations like this:

someclass::somefunc()
{
...
.
.
m_myvar = 1;
lvar = 2;
.
.
.
}

You can tell at a glance that m_myvar is a member of someclass but lvar is not.

What does `m_` variable prefix mean?

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.

Why do most fields (class members) in Android tutorial start with `m`?

This notation comes from AOSP (Android Open Source Project) Code Style Guidelines for Contributors:

Follow Field Naming Conventions

  • Non-public, non-static field names
    start with m.
  • Static field names start with s.
  • Other fields start with a lower case letter.
  • Public static final fields (constants) are ALL_CAPS_WITH_UNDERSCORES.

Note that the linked style guide is for code to be contributed to the Android Open Source Project.

It is not a style guide for the code of individual Android apps.

What does the m stand for in java code

Letter m as prefix means that it is member of class.
Letters lv means that it is local variable.
Letters pm means that it is parameter.

example:

class Example
{
Integer mMemberOfClass;

public void someMethod(Object pmSomeParameter)
{
Integer lvSomeLocalVariable;

}

}

Why do we use _ in variable names?

It doesn't mean anything. It is rather a common naming convention for private member variables to keep them separated from methods and public properties. For example:

class Foo
{
private int _counter;

public int GetCounter()
{
return _counter;
}

public int SetCounter(int counter)
{
_counter = counter;
}
}

Which field names get prefix 'm'?

In your second example, mMethodFieldOne and mMethodFieldTwo are not fields, just variables local to someMethod, so the naming convention does not apply.

Using m prefix for variables in Kotlin

A good reference from Android

https://developer.android.com/kotlin/style-guide#naming_2

Special prefixes or suffixes, like those seen in the examples name_,
mName, s_name, and kName, are not used except in the case of backing
properties (see “Backing properties”).

What does variable names beginning with _ mean?

There's no language-defined meaning - it's just a convention some people use to distinguish instance variables from local variables. Other variations include m_foo (and s_foo or g_foo or static variables) or mFoo; alternatively some people like to prefix the local variables (and parameters) instead of the instance variables.

Personally I don't use prefixes like this, but it's a style choice. So long as everyone working on the same project is consistent, it's usually not much of an issue. I've seen some horribly inconsistent code though...

An interesting detail about variable name

The m will be to signify that the object is a member variable of the class in question. It's a common use of Hungarian Notation to prefix the name with clues to the variable's purpose or type.



Related Topics



Leave a reply



Submit