Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I saw lot of code (for example some Android source code) where fields name start with a "m" while static fields start with "s"

Example (taken from Android View class source):

private SparseArray<Object> mKeyedTags;

private static int sNextAccessibilityViewId;

I was wondering what "m" and "s" stand for... maybe is "m" mutable and "s" static?

Since it seems that is a largely adopted pattern do you know if there some literature about this kind of naming convention?

share|improve this question
2  
Android dudes did really wrong in some aspects of their API, too much non standard java and outdated practices ... –  Kemoda Nov 26 '12 at 12:38

2 Answers 2

up vote 12 down vote accepted

m is typically for a public member (see this answer for common C code conventions http://stackoverflow.com/a/1229360/117507).

I've never seen s before, but based on that answer:

  • m for members
  • c for constants/readonlys
  • p for pointer (and pp for pointer to pointer)
  • v for volatile
  • s for static
  • i for indexes and iterators
  • e for events

Have you read any published standards for the project you've seen that code in?

One of the most famous prefix notation systems is Hungarian Notation.

There is a excellent blog post by Joel Spolsky on prefixes: Making Wrong Code Look Wrong

share|improve this answer
2  
s prefix for static is really common (I use it!). First hit on web search took me to developer.mozilla.org/en-US/docs/Developer_Guide/Coding_Style –  ckhan Nov 26 '12 at 10:29
6  
You are mixing two kinds of things in that list. The scopes – m, c and s – and the Hungarian-style kind prefixes p, v, i and e. They are different things and if both are used, each identifier may have both. –  Jan Hudec Nov 26 '12 at 10:32
3  
Prefixing identifiers dilutes the value of diffs when something changes. For example, if something in a class starts life a constant and you've decided to make it a configurable-from-the-outside member, you have to change every reference to it to a new name. Someone reviewing the change will not only see the change in declaration and any new accessors, but also a substance-free change in every line that uses it. Adding a prefix is like declaring the variable repeatedly. Keep it to a single place and use your development tools to find out what it is. –  Blrfl Nov 26 '12 at 12:18
2  
@Blrfl, constants are normally ALL_CAPS anyway, so you'd get the many line changes. That's no too much of a hardship to review. Also, if something changes from a private const to a member, there's enough of a change to how that's used to warrant a rename anyway. –  StuperUser Nov 26 '12 at 13:25
2  
nounI verbDisagree punctuationPeriod –  Blrfl Nov 26 '12 at 15:27

It's called hungarian notation, and it sucks. Do some research on it, it's actually a misunderstood concept.

It originated with a good intent, indicate variables that may cause harm, such as input directly from the user in a HTML form (be sure to strip out HTML before storing to DB for instance).

var unsafeComment = $('.input').val();
save(unsafeComment);//looks wrong
//strip out html then save 

The m is for member and s for static.

share|improve this answer
2  
No, this isn't Hungarian notation. Or rather it's one kind of it and not the one usually meant by that term. –  Jan Hudec Nov 26 '12 at 10:33
3  
Have a read of joelonsoftware.com/articles/Wrong.html, Hungarian notation is easily open to abuse, but used judiciously it can be very useful. –  StuperUser Nov 26 '12 at 10:33
3  
@StuperUser: Indeed; there are good uses of Hungarian notation and evil uses of it. The one pushed by Microsoft rates as very evil, because it "effectively prevents abstraction from creeping into the code", but this one rates as somewhat useful and in dynamic languages it's often the sanest way to avoid getting lost in what is what. –  Jan Hudec Nov 26 '12 at 10:41
1  
@JanHudec - this is (sort of) the original use of Hungarian. It was for pixel coordinates relative to the window vs relative to the screen - so you can spot which way round an offset should be applied. –  Martin Beckett Nov 26 '12 at 16:35
    
@MartinBeckett: These m and s prefixes indicate (lexical) scope, which is vastly different from indicating semantics as in the original paper (in C++ type is much better for that; see e.g. Boost.Units) –  Jan Hudec Nov 27 '12 at 6:49

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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