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

93,293

Solution 1

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

Solution 2

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."

Solution 3

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.

Solution 4

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)

Solution 5

'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 it's name

Share:
93,293

Related videos on Youtube

Kallja
Author by

Kallja

I've studied Computer Science and Information Systems Science at a Finnish university. Working as a software developer.

Updated on September 11, 2020

Comments

  • Kallja
    Kallja over 3 years

    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?

    • SMUsamaShah
      SMUsamaShah over 8 years
    • mavesonzini
      mavesonzini over 6 years
      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!
  • Falmarri
    Falmarri over 13 years
    I always read the 'm' as 'my'. Good to know it's not that stupid, lol
  • Иван Бишевац
    Иван Бишевац over 12 years
    Great link, not just for prefixes.
  • David
    David almost 12 years
    Is this link refers to writing application? or just to Open android project?
  • Cosmin
    Cosmin about 11 years
    I get an "Insufficient permissions" error when trying to open that page. Maybe someone can post here some of its contents?
  • W.K.S
    W.K.S almost 11 years
    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.
  • AutoM8R
    AutoM8R almost 11 years
    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.
  • twiz
    twiz over 10 years
    @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"?
  • spaaarky21
    spaaarky21 over 9 years
    @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.
  • Fodder
    Fodder about 9 years
    @Cosmin It's too long to post in a comment, but the relevant part related to the question is: "Non-public, non-static field names start with m."
  • kingfrito_5005
    kingfrito_5005 almost 9 years
    @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.
  • noɥʇʎԀʎzɐɹƆ
    noɥʇʎԀʎzɐɹƆ over 8 years
    @AutoM8R I come from python and balk at the idea of it all and would rather just use this.foo instead of mFoo.
  • Tamás Barta
    Tamás Barta over 8 years
    Can you please provide an URL for this Google documentation? Thanks!
  • Danilo Dughetti
    Danilo Dughetti about 8 years
  • An-droid
    An-droid about 8 years
    I personally think that this suffix makes android code much more readable source.android.com/source/…
  • An0nC0d3r
    An0nC0d3r almost 8 years
    Plus 1 for the auto complete suggestion!
  • likejudo
    likejudo over 7 years
    Please add your comment to this petition to remove the rule code.google.com/p/android/issues/detail?id=226814
  • Kuba Beránek
    Kuba Beránek about 7 years
    There's already a thing for that built into Java - it's called this ;-)
  • AbhishekAsh
    AbhishekAsh about 7 years
    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.
  • LarsH
    LarsH about 7 years
    This answer is incorrect. See @Danilo's answer.
  • Taylan
    Taylan almost 7 years
    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.
  • milosmns
    milosmns about 6 years
    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: beust.com/weblog/2017/07/17/…
  • Rodrigo Recio
    Rodrigo Recio about 5 years
    it looks like premature optimization