275

Looking at the Android tutorials such as the Notepad tutorial, I noticed that almost all variables are named starting with the letter 'm'. What convention is this, and where does it originate from?

Martin G
  • 17,357
  • 9
  • 82
  • 98
Kallja
  • 5,362
  • 3
  • 23
  • 33
  • http://stackoverflow.com/a/7072899/342095 – SMUsamaShah Aug 24 '15 at 04:49
  • 4
    Hi! This is no longer used, it's bad variable naming! It's called Hungarian Notation. This tarted life in the platform inside of AOSP so it adhered to the AOSP style. Android Studio and IntelliJ IDEA visually distinguish field names based on membership (instance or static). IDEs will enforce correct membership, visibility, and types by default so a naming convention isn’t going to add anything here. Cheers! – mavesonzini Nov 20 '17 at 10:32

8 Answers8

335

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

Michael Myers
  • 188,989
  • 46
  • 291
  • 292
Matthew Flaschen
  • 278,309
  • 50
  • 514
  • 539
  • 143
    I always read the 'm' as 'my'. Good to know it's not that stupid, lol – Falmarri Nov 21 '10 at 21:17
  • 24
    I never understood this convention either. Why add an odd 'm' when you can use `this`? The whole point of that keyword is to indicate you're dealing with a class member variable/function. – W.K.S Jun 12 '13 at 10:26
  • 57
    OK, "m" is very much misunderstood. I don't think it matters whether or not you use an IDE, all variables should follow this convention. By using this convention one can quickly look at the code immediately in front of them and readily understand the scope of the variables, I find this extremely important with Android Activities. I don't have to break my chain of thought by always tracing the variables back through the IDE, it's MUCH better for concentration purposes. – AutoM8R Jun 22 '13 at 19:47
  • 20
    @AutoM8R In my opinion, the fact that it is so misunderstood is what makes it unhelpful. How can you know for sure the the person who wrote the code used the convention "correctly"? – twiz Dec 15 '13 at 19:50
  • 5
    @W.K.S Yes, `this` does indicate that what follows is a member but I wouldn't clutter my code with that either. If you right short methods, whether a variable is local or a member shouldn't be confusing. Only prefix with `this.` when it's needed to disambiguate. – spaaarky21 Sep 29 '14 at 16:27
  • 4
    @AutoM8R its not misunderstood. we understand it, and dislike it. If I am using an IDE the text will be in a color that immediately tells me what it is. I am never not using an IDE, so this is just ugly syntax for me. Many other people feel this way, which is what brings its value into question. Also with proper naming and well written code, it should be obvious without being labelled what things are members and what are not. – kingfrito_5005 Jul 28 '15 at 18:01
  • I personally think that this suffix makes android code much more readable http://source.android.com/source/code-style.html#follow-field-naming-conventions – An-droid Mar 18 '16 at 10:40
  • In my opinion usage of 'm' to identify the variable scope, indicates in a way that the methods are very long and probably doing too much. – AbhishekAsh Apr 02 '17 at 15:29
  • This answer is incorrect. See @Danilo's answer. – LarsH Apr 17 '17 at 15:54
  • Also agree that knowing if something is a field/member is not a good argument. Go ahead and color-code your members if that's so important to you, humans visualize colors much better than words anyway. There is a post actually by the guy who introduced it to Android and the (somewhat flawed) reasons behind it: http://beust.com/weblog/2017/07/17/i-am-the-reason-for-hungarian-notation-in-android/ – milosmns Feb 25 '18 at 14:32
122

See Code Style Guidelines for Contributors: Follow Field Naming Conventions. The use of the "m" prefix is more specific that simply denoting a "member" variable: It's for "non-public, non-static field names."

LarsH
  • 27,481
  • 8
  • 94
  • 152
Warren Chu
  • 1,463
  • 1
  • 9
  • 7
106

According to Android source code documentation:

  • 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 this is for writing Android source code. For creating Android apps, the Google Java Style Guide may be more helpful.

Danilo Dughetti
  • 1,360
  • 1
  • 11
  • 18
  • 2
    Can you please provide an URL for this Google documentation? Thanks! – Tamás Barta Oct 22 '15 at 09:27
  • 2
    http://source.android.com/source/code-style.html#follow-field-naming-conventions – Danilo Dughetti Feb 23 '16 at 14:28
  • 7
    Quote from the page: *Note: These rules are intended for the Android platform and are not required of Android app developers. App developers may follow the standard of their choosing, such as the [Google Java Style Guide](https://google.github.io/styleguide/javaguide.html).* – Taylan Jul 11 '17 at 20:18
  • From experience, it's better to use "_" instead of "m", since any variable name can start with that letter. That way autocomplete will show only field (or whatever we set it to) variables, plus it's visually more noticeable. – cmak Sep 28 '22 at 20:24
47

The m is here to indicate a member variable.

It has 2 huge advantages:

  • If you see it, you instantly recognize it as a member variable.
  • Press m and you get all members via the auto completer. (This one is not in the other answers)
Klaus
  • 1,171
  • 1
  • 11
  • 16
17

'm' means member of the class. So, if you don't use IDE to highlight your members, then you will understand that it is a member by its name.

Pang
  • 9,564
  • 146
  • 81
  • 122
Vladimir Ivanov
  • 42,730
  • 18
  • 77
  • 103
7

As already answered this prefix indcates that a variable is member.

Somtimes people use other prefixes if you discover some variables starting with 'i' or 's' it could also be a variant of the Hungarian Notation

stacker
  • 68,052
  • 28
  • 140
  • 210
2

'm' means the variable is a member variable of the class...

swcai
  • 675
  • 5
  • 12
0

not only in java, I've seen similar convention in cocos2d+box2d samples where some of the variables start with m_, but others don't, very confusing.


b2World* world;
GLESDebugDraw *m_debugDraw;

I guess to differentiate C++ box2d variables from Obj-C variables.

chunkyguy
  • 3,509
  • 1
  • 29
  • 34