3

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?

Christine
  • 165
  • 7
  • 12
    Cause that's what the tutorials use and the poor programmers don't know any better. – ratchet freak Nov 13 '15 at 14:06
  • 10
    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: http://stackoverflow.com/q/23214014/99456 – yannis Nov 13 '15 at 15:26
  • 3
    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
  • 1
    @gnasher729 I have 20+ years of working Objective C code - when did member variables start getting a _ ? Not that long ago – mmmmmm Nov 13 '15 at 15:54
  • @Yannis given "You would have to ask the original developers who set the standard for the project. :) This is purely speculation on my part..." as the only answer there, isn't this question purely speculation on *our* part too then? Is it possible for us to come up with an answer that *isn't* a guess? –  Nov 13 '15 at 15:55
  • As a clarification, as a freelance android programmer for 7 years now, I've seen the `m` in every single codebase I've seen (except my own). I've been a Java programmer for even longer, and I don't remember having seen the same naming style in Java projects. Still, it's more or less the same language. Hence I was wondering what causes this difference. One thing is that Android documentation advises you to do so, as was already said here. – Christine Nov 13 '15 at 16:36
  • @Christine so the answer to "Why do Android programmers use Hungarian notation?" is "[the] Android documentation advises you to do so"? –  Nov 13 '15 at 16:45
  • If you say it like that, it has two parts. Why does android documentation advise you to do so, and why do programmers follow that advice. Should I rephrase the question? It is something I've been thinking about since I started doing Android programming. – Christine Nov 13 '15 at 17:00
  • The first, as suggested in Yannis's link "You would have to ask the original developers who set the standard for the project." - we can only guess. Why should programmers follow that advice (as opposed to using other standards?) is something that will lead to debates on single vs multiple coding standards for a project. –  Nov 13 '15 at 17:08
  • @nvoigt Those clearly aren't integer types. Those are ScreenCoordinate and ClientCoordinate types, which are mutually incompatible. Any worthwhile IDE or compiler for Java will be able to tell those apart. – 8bittree Nov 13 '15 at 17:10
  • I must also point out that you seem to have failed to grasp a key part of Joel's article. He presents a *defense* of Hungarian notation (the paragraph beginning with "So now I’ll walk you through a little example" starts it out. Joel is a proponent of Apps Hungarian rather than Systems Hungarian. –  Nov 13 '15 at 17:39
  • Joel defends Hungarian notation for very specific cases. – Christine Nov 13 '15 at 17:58
  • MichaelT, "is it ok to make coding style changes to an open source project" applies to the android source code. It does not apply to code in projects that use Android but are a separate code base. – Christine Nov 13 '15 at 18:00
  • 1
    Java already has a way to specify the difference between instance scope and local scope it is called `this.` there is no need for the `m` prefix tautology. Outside of dynamically type languages like JavaScript or Python ( which does not use it ) these silly archaic prefixes are tautologies. –  Nov 14 '15 at 02:54
  • At any rate, thank you all for commenting and discussing (and answering). – Christine Nov 14 '15 at 18:11

1 Answers1

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