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

AndroidCoding Style

Android Problem Overview


I know about camel case rules, but I'm confused with this m rule. What does it stand for? I'm a PHP developer. "We" use first letters of variables as indication of type, like 'b' for boolean, 'i' for integer and so on.

Is 'm' a Java thing? Does it stand for mobile? mixed?

Android Solutions


Solution 1 - Android

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.

Solution 2 - Android

A lot of coding guide lines use m for 'members' of a class. So when you're programming you can see the difference between local and member variables.

Solution 3 - Android

What is m prefix?

m stands for member variable or data member. Use m prefix for non-public and non-static fields.

When to Use?

private String mCityName;
private float mTemperature;

When not to Use?

public static int mFirstNumber;
public static final String mDATABASE_NAME;

What I do?

Personally, I don't use it. It makes the code more complicated and chaos the readability. If you are still using Notepad for coding I have no words, but modern IDEs are capable of highlighting and coloring member and local variables or anything else.

Conclusion

Use? "Yes" or "No" is your personal choice.

Solution 4 - Android

If it's member variables in classes, the 'm' means 'member'. Many Java programmers do that, although with modern IDEs it's not needed since you have highlighting, mouse over tooltips, etc.

Solution 5 - Android

According to Clean Code book, it's not a clean code.

> You don't need to prefix member variables with m. Besides, people quickly learn to ignore the prefix or suffix to see the meaningful part of the name.

Solution 6 - Android

If you have problems like

> your IDE to generate setters/getters and you end up with getmName() > and setmName()

Don't forget to do next (Settings/Editor/Code Style/Java/Code Generation):

enter image description here

Update: we don't use something like this in Kotlin (so it's better to switch to it and don't use prefixes anymore)

Solution 7 - Android

I think it is very individual which code conventions is used. I prefer to name my variables with the following prefixes:

  • m - Method variables
  • c - Class variables
  • p - Parameter variables

But I guess that each programmer has their own style.

Solution 8 - Android

To prove that you definitely shouldn't treat this convention for naming variables in your code, I pass a screenshot from a parent Android Studio hereunder.

Find that variables inside an object a specially sorted to put m-variables lower than your native variables. So by naming them in your code with "m" prefix you hide them in a heap from yourself.

enter image description here

Solution 9 - Android

As a matter of readability the convention of m for member variables and s for static fields should not be used anymore if you are using a modern IDE like Android Studio. Android Studio can differentiate between those without adding m or s.

Solution 10 - Android

The one benefit I found of this code style, is when during an auto-complete of some reference to a variable, I know that I can type "m" to see just the member variables.

Solution 11 - Android

As was mentioned earlier, it is styled for different variable. But also it is very useful for code-generation. If you press "Alt + Insert" you will get windows for most common code generations properties. If you want to generate "get" method for your variable you will get.

public class Foo{
   private int bar;

   public int getBar(){
       return this.bar;
   }

   public void setBar(int bar){
       this.bar = bar; 
   }

}

But if you declare "m, s" you will get:

public class Foo{
private int mBar;

public int getBar(){
   return mBar;
}

public void setBar(int bar){
   mBar = bar;
}
}

It will be automatically generated and "m" or "s" deleted from your constructor, get, set methods name. After this "get"' and "set" for the field will be generated without "m". Andoroid Fle->Setting->Code Style-> Java->Code Genenretion. And make as on a picture. Maybe it will help. Sorry for my eng. [Configure android] 1

Solution 12 - Android

It seems to have been a personal preference of some early Android/Google engineers to start member variables with 'm' and so they recommended it.

Now this rule is being forced down the throats of developers in companies who are neither AOSP contributors, simply because that page is considered Android Code Style rules. There is little if any benefit in that rule. Google should consider removing it. Otherwise please specify that for Android Apps which of the Code Style Rules are optional.

Please add your comment of support to this petition to remove the rule https://code.google.com/p/android/issues/detail?id=226814

Solution 13 - Android

It can also be stated that it stand for "mine", as in the Class/Instance is saying "This variable is mine and no one else can get to it." Different to static which, while it might be available to only the Class it is shared by all instances of that class. Like if you were drawing circles you'd need to know how big the radius of each circle is

    private double mRadius;

but at the same time you want a counter to keep track of all circles, inside of the circle class you could have

    private static int sCircleCount;

and then just have static members to increase and decrease the count of the circles you currently have.

Solution 14 - Android

The following are the 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.

Example:

public class MyClass {
    public static final int SOME_CONSTANT = 42;
    public int publicField;
    private static MyClass sSingleton;
    int mPackagePrivate;
    private int mPrivate;
    protected int mProtected;
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionpambukView Question on Stackoverflow
Solution 1 - Androidxiaobing.zhaoView Answer on Stackoverflow
Solution 2 - AndroidKolkyView Answer on Stackoverflow
Solution 3 - AndroidMadan SapkotaView Answer on Stackoverflow
Solution 4 - AndroidahansView Answer on Stackoverflow
Solution 5 - AndroidHamedzView Answer on Stackoverflow
Solution 6 - Androiduser924View Answer on Stackoverflow
Solution 7 - AndroidSteffen JørgensenView Answer on Stackoverflow
Solution 8 - AndroidZonView Answer on Stackoverflow
Solution 9 - AndroidaselimsView Answer on Stackoverflow
Solution 10 - AndroidJanac MeenaView Answer on Stackoverflow
Solution 11 - AndroidIHAFURRView Answer on Stackoverflow
Solution 12 - AndroidlikejudoView Answer on Stackoverflow
Solution 13 - AndroidjimistephenView Answer on Stackoverflow
Solution 14 - AndroidLopezDevelopView Answer on Stackoverflow