2

I think official code style guidelines from Google are very helpful, but they do not cover naming conventions for view elements.

Let's say we simple Activity which contains one ImageView, one TextView and Button. Code would look like this:

class SimpleActivity extends Activity {

     private ImageView mImageView;
     private TextView mTextView;
     private Button mButton;
}

Of course we won't give such names for widgets, cause they aren't descriptive. We should know the functions of this widgets. So let's imagine that ImageView represents user profile image, TextView represents username and Button represents continue button.

I would divide possible naming conventions in three categories:

1.

class SimpleActivity extends Activity {

     private ImageView mUserProfileImageView;
     private TextView mUsernameTextView;
     private Button mContinueButton;
}

Big advantage of first conventions is that while using members in code, we know that this is TextView and we can use "setText()" method for example. Unfortunatelly name is very long, because lazy programmer is good programmer, this is it's drawback.

2.

class SimpleActivity extends Activity {

     private ImageView mUserProfileImage;
     private TextView mUsernameText;
     private Button mContinueButton;
}

Here we have mixed convention, we still know what's kind of widgets it is while using somewhere in code, but maybe when we have more complicated functions of widgets these names would be still too long?

3.

class SimpleActivity extends Activity {

     private ImageView mUserProfile;
     private TextView mUsername;
     private Button mContinue;
}

The laziest category.

Question

Which naming conventions do you use in code and what's your experience? Maybe there are better conventions which I didn't mention? What kind of convention Google is using?

Nominalista
  • 3,669
  • 7
  • 32
  • 80
  • I use a "naming convention" which I migrated from my VB and VB.NET times. I use the prefix `txt` for TextViews, `img` for ImageViews, `btn` for Buttons, ... And I use `FRG_`, `ACT_`, `CLS_` for the Activities, Fragments and Classes names (since they must start with upper case letters), respectively. I don't use to prefix variables, normally. But I could use `bln`, `str`, `int`, ... in case I wanted to. – Phantômaxx Oct 23 '16 at 11:42

1 Answers1

4

First of all, the 'm' prefix is used in the AOSP source code. It's not intended to be used as a convetion in Android apps, therefore I suggest you to remove it (you can read a bit more here).

Your third option is the first to eliminate; for instance, mContinue can easily be a boolean variable that represents whether to continue doing something, so it's definitely not very readable.

Between the first option and the second, I would still go for the first as it's significantly more readable, and as a general rule in OOP, readability is more important than short names. Furthermore, if you move the views' customization and data binding methods to other classes, you're gonna have minimum amount of references in the activity code.

Community
  • 1
  • 1
Neria Nachum
  • 1,469
  • 1
  • 17
  • 31
  • I know that "m" prefix depends on developer preferences, this is what I use. I think your explanation is quite convincing. – Nominalista Oct 23 '16 at 11:36