Android Android naming convention

For example, I have a view that has id:

<ImageButton android:id="@+id/imageButtonStart" android:layout_width="100dp" android:layout_height="100dp" /> 

I need to make a personal field with the same name, for example:

 private ImageButton imageButtonStart; 
+4
source share
2 answers

Although it is good practice to maintain consistency and use the same name for your variable, you do not need to call the class variable in the same way as the identifier in your XML, because you manually connect them to your code for example:

 ImageButton imageButtonNotStart = (ImageButton) this.findViewById(R.id.imageButtonStart); 

My personal preference is to use the componentTypeDescriptiveName format, such as imageButtonStart in XML, but inside the code, I prefer a more English approach like startButton .

I usually finish

 <ImageButton android:id="@+id/imageButtonStart" android:layout_width="100dp" android:layout_height="100dp" /> ImageButton startButton = (ImageButton) this.findViewById(R.id.imageButtonStart); 
+3
source

Do I need a personal field with the same name?

No, android:id in the layout XML file does not require a variable with the same name in your code.

But you need to use the exact name if you are looking for id

 View randomName = findViewById(R.id.imageButtonStart); 

Naming is a lot of personal preference. You must choose names that clearly define what a named thing is. And you must stay in accordance with your naming scheme.

How long this name should be debatable, some people chose startBtn over imageButtonStart because it is shorter and less annoying to your code. If the fact that the ImageButton button ImageButton not related to your code, you do not need to call it ImageButton , just a button will be enough to make it understandable.

Some people like to use hierarchical names, with the most important distinguishing feature in the first place. Like the Java and Android package names com.google.market instead of market.google.com .

Instead of startButton and endButton they selected buttonStart and buttonEnd . The reason for this is that you can use autocomplete more efficiently. If you cannot remember whether you specified your endButton or stopButton end button, you need to look at the entire list of offers. Using the buttonXYZ scheme, you can start typing button , since you know that it is a button and you only get a few suggestions.

The above is also a good example of why you should stay the same. If you call one button buttonStart and the other clickablethingStop , the whole scheme is useless. Inconsistent naming takes more time to find the right thing and can lead to errors if you choose the wrong thing.

Another thing is the language you use for names. It is perfectly normal to use your own language if you write code only for yourself or people who speak that language. But as soon as you want to share it with others, stay with English. Even a wrong English name is easier to read and understand than names in a language that you don’t understand at all.

There are also many other naming conventions: WP: Naming Conventions

For example, Android syle coding guide :

  • Non-public names of non-static fields begin with m.
  • Static field names begin with s.
  • Other fields begin with a lowercase letter.
  • Open static end fields (constants) - ALL_CAPS_WITH_UNDERSCORES.

This will be a private ImageButton mButtonStart; or private static sSingletonThing;

This agreement, by the way, is required only for use, if you want to contribute to the Android source code, you can write your own applications in any style.

The only naming convention you should follow is the general Java schema / method / variable: (on top of the wikipedia link)

  • Class Names: UpperCamelCase
  • Method Names: lowerCamelCase
  • Variable Names: lowerCamelCase
  • Constants: ALL_CAPS

It starts to get very confused for other people if they read your code, and you have lowercase names, etc.

+5
source

Source: https://habr.com/ru/post/1447773/


All Articles