📜 ⬆️ ⬇️

Android naming tips

Android allows you to keep all text constants in xml files. The same applies to many other things, for example, identifiers. And if your application is a bit more complicated than HelloWorld, then incorrect naming can greatly affect your productivity and the quality of the final product.

While I was programming for Android, I made for myself a couple of rules that slightly improve my life when working with resources (the very magical R):


')

The name of the resource should reflect its essence (the most obvious rule)


For example,
< string name ="set_as_wallpaper" > As wallpaper </ string >

* This source code was highlighted with Source Code Highlighter .

Do not be lazy to rename the automatically generated identifiers, for they only litter the remaining identifiers with almost meaningless names.

A resource name must begin with its main logical domain.



If a string or identifier is strictly related to some context (for example, Activity), then the name of this context should follow the logical name.


For example,

Names must begin with the name of the file in which they are located.


For the most part, this refers to the identifiers in the layout. This should be done due to the fact that the framework dumps all identifiers in one pile and if this is not done, it becomes difficult to figure out where the identifier is “physically”.

The name should go through the levels


Those. main logic level, file name, element, string description


Total


The main benefit of this approach is to quickly search through auto-complete in Eclipse. If, for example, you define a menu in the code, then to find a specific title for an item, just type R.string.menu_ and you will immediately see all the possible names. The same goes for searching the view inside the Activity, just type R.id.my_activity_name and everything is already in full view. Also with this approach, it will be much easier to find where the resource is declared.

If you have any string constants in the code, do not forget that you can always simply transfer Alt + Shift + A quickly into an xml file.

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


All Articles