ip696
ip696

Reputation: 7084

why Google calls variables with the prefix "m"?

why Google calls variables with the prefix "m" for example:

    private int mSectionResourceId;
    private int mTextResourceId;

I see it in all examples. But i not understand why they do it?

And now i have some example where it practic very good. If a called variabels without prefix i need write

public SimpleSectionedRecyclerViewAdapter(Context context, int sectionResourceId, int textResourceId,
                                              RecyclerView.Adapter baseAdapter) {
        this.sectionResourceId = sectionResourceId;
        this.textResourceId = textResourceId;

but if i use prefix i can write

public SimpleSectionedRecyclerViewAdapter(Context context, int sectionResourceId, int textResourceId,
                                              RecyclerView.Adapter baseAdapter) {

        mSectionResourceId = sectionResourceId;
        mTextResourceId = textResourceId;

I think it more readable. Who can explain to me the pros and cons of a prefix?

Upvotes: 4

Views: 2001

Answers (4)

Tomasz Dzieniak
Tomasz Dzieniak

Reputation: 2915

It is a prefix for class member variables. It's just a naming convention.

Mostly sure, taken from Hungarian Notation where similar prefix: m_ stands for exactly the same).

Referring to pros & cons:

  1. Pros:

    • it allows to type fewer chars during programming,
    • programmers that are used to use Hungarian Notation may found it easier to follow the code.
  2. Cons:

    • as the code changes very often, it is easy to forget about changing prefixes every time, when variable changes it's purpose (especially during prototyping),
    • it makes the code starts to smell bad,

Generally, it is some kind of reinventing the wheel. Java has this keyword that should be more than enough for accessing proper variable. If it's not, the code requires refactoring, maybe because of naming glitches or using too wide variable scopes.

Personally, I do not recommend to use Hungarian Notation (even the part of Android Code Style). We have great IDEs that increases the readability of the code.

There is an exception. The code, where Hungarian Notation (or more general, specific code style) was already been used. It is a matter of consistency.

Upvotes: 1

B. Kemmer
B. Kemmer

Reputation: 1537

The m just stands for 'Member'. It is simply declared that your Variable is a Class-Member.
It is more readable Code, because you know where Class Members got declared, so you can find it pretty fast. You don't need to write this, even if you don't prefix your Variables with an m.

In your Example, this only makes it more readable when there is no prefix-m. Another developer knows that it is a instance variable (member variable) and so declared on top or bottom of the class.

Upvotes: 1

Spencer Williams
Spencer Williams

Reputation: 86

The m is just a member variable. A class member if you will. Useable with constructors like WebView M WebView then later on you would use something like mWebView.loadurl("example.com"); it's just a placeholder for the variable you created. You don't have to add the member class variable as an m but it's more organized if you do

Upvotes: 0

Marko
Marko

Reputation: 20513

The variables starting with m are telling you they are variables in the scope of your class. Member of the class.

Link to Android Code Style Guide

Upvotes: 6

Related Questions