Why do variable names often start with the letter 'm'? [duplicate]
Asked Answered
R

8

277

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?

Rajewski answered 21/11, 2010 at 10:54 Comment(2)
https://mcmap.net/q/79968/-why-do-most-fields-class-members-in-android-tutorial-start-with-mSecondary
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!Zendejas
C
336

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

Chanda answered 21/11, 2010 at 10:56 Comment(10)
I always read the 'm' as 'my'. Good to know it's not that stupid, lolSlapdash
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.Tormentil
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.Rufe
@Rufe 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"?Cerebrospinal
@Tormentil 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.Seldan
@Rufe 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.Amnion
I personally think that this suffix makes android code much more readable source.android.com/source/…Bicuspid
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.Healey
This answer is incorrect. See @Danilo's answer.Synchronic
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/…Derwood
J
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."

Jenicejeniece answered 14/6, 2011 at 9:36 Comment(2)
Great link, not just for prefixes.Plausive
Is this link refers to writing application? or just to Open android project?Justifier
F
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.

Fustanella answered 15/9, 2015 at 13:51 Comment(4)
Can you please provide an URL for this Google documentation? Thanks!Headward
source.android.com/source/…Fustanella
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.Teneshatenesmus
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.Solorio
J
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)
Julianajuliane answered 16/7, 2015 at 12:54 Comment(2)
Plus 1 for the auto complete suggestion!Belicia
There's already a thing for that built into Java - it's called this ;-)Adrianeadrianna
P
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.

Paraphrastic answered 21/11, 2010 at 10:56 Comment(1)
it looks like premature optimizationLibnah
U
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

Undulatory answered 21/11, 2010 at 11:4 Comment(0)
H
2

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

Harvison answered 21/11, 2010 at 10:56 Comment(0)
M
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.

Martainn answered 12/2, 2011 at 22:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.