3

This question already has an answer here:

I see Hungarian notation in almost all Android code I see. It is used in the source code, in the samples, in code from other programmers I get to see, in code that presenters show at Android conferences. They use mContext rather than context, mTextView rather than just textView. Hungarian notation is useless in Java, especially when you use an IDE that uses color and typeface to indicate local and member fields. Joel Spolski wrote an excellent article supporting this view. Hungarian notation makes your code look messy.

So my question is: despite the fact there's no benefit in Hungarian notation in Java, why do several Android programmers use it?

edit: Android developer documentation used to advise you to do so. Thus my question has two parts: why would Google advise us to use the m notation, and if they do, why do people follow that advice even when no-one explains why you should?

marked as duplicate by gnat, JB King, Jarrod Roberson Nov 14 '15 at 2:52

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 11
    Cause that's what the tutorials use and the poor programmers don't know any better. – ratchet freak Nov 13 '15 at 14:06
  • 9
    You are completely misunderstanding both Hungarian notation and Joel's article. The lowercase m is there to identify members of self and to distinguish them from local variables, parameters etc. that would have the same name. That's not Hungarian notation at all. – gnasher729 Nov 13 '15 at 15:24
  • 2
    And if I see Objective-C code that uses member variables without a leading underscore, I know not to trust that code. – gnasher729 Nov 13 '15 at 15:26
  • 1
    Cross site duplicate: stackoverflow.com/q/23214014/99456 – yannis Nov 13 '15 at 15:26
  • 2
    The real purpose of hungarian notation is to differentiate between two identical types with different meanings. For examples integers for both screen coordinates and client coordinates. And you need to tell them apart, despite both being integer types. No IDE or coloring will ever be able to do this. Now, if you look at what people without an understanding made of it, then you are right, that "hungarian notation" is indeed irrelevant. – nvoigt Nov 13 '15 at 15:51
11

The Android codebase uses a highly modified notation that isn't strictly "Hungarian".

This notation is highly influenced by the entire Google codebase, in all languages, almost all of which contains domain-specific Hungarian-style notation. Code reviews at Google pretty much require this1, and there is a ripple effect to the public codebases.

why would Google advise us to use the m notation

Because this is their coding standard. When you have large codebases across many APIs shared by literally thousands of coders, you need standards. Google have decided that marking up their code in this manner leads to better and easier to maintain code.

Not everyone agrees, but every shop has its own rules you have to follow. As someone commented earlier, you often don't see this at Java shops. Though, you certainly do see it in Java at Google.

As for why to annotate your code at all in this manner, there are lots of links in the comments. But, at the end of the day, it is about code maintainability (and readability is a major part of that) and an effort to keep bugs from creeping in as the code is changed.

why do people follow that advice even when no-one explains why you should?

Well, open source projects are uneven in how they introduce new people to their processes and workflows. If you were working at Google, you would get a mentor who would shadow you and check all your code prior to acceptance, and this process would introduce you to all the things you would be expected to do as a coder. This includes following coding standards. If you had to ask why, you could ask your co-workers.

Open source public facing libraries and APIs are managed differently, and also have a different way of introducing you to their specific coding practices. In this case, my guess is that pushing code that doesn't follow a standard required by the "community" would cause the push to be annotated and flipped back to you for further grooming. Thus, you would learn.

1 Citation: me, and the chats I've had with colleagues that work for Google

Not the answer you're looking for? Browse other questions tagged or ask your own question.