Code Review on Android Projects - Part II

Code Review on Android Projects - Part II

Continuing the previous post ...

String Resources

First of all, it is a good practice to put the text inside of a strings.xml file. We also need to be careful about how to use this powerful resource to keep easy maintenance. For example, if you need to create a new string resource to the text ``GET STARTED''. You can define the following:

Image that contains the example using get started text, in a string.xml file

It works, but imagine if you have in another place of your app to use the same string, using it in camelcase mode, something like ``Get Started''. You would need to create another item in your string file. For this reason, I like to create strings like ``Get Started'' in camelcase, because it is more useful. Using this way, you can build a text transformation using a custom style to simulate the uppercase mode.

How can we name the keys?

I think we need to consider two cases, the first one is short texts, you can use the same value to the key:

Example where I describe the string resource file that has two items, the first one is with the yes value, another has the no value.

In the second case, when the text is long, you can put a name to identify the description. Most of these cases are when we need to show alert messages, successful messages, and so on.

Annotations

Each resource in the Android World has a unique id. The id type is a ``Int'' value for default. But if you need to build a custom view component that works with the id, how do you do? For example, the following code.

No alt text provided for this image

In a code review process, we can recommend using an annotation to identify what kind of resource this method returns. What kind of ``Int'' this method returns. If we solve this issue, the code will be:

No alt text provided for this image

AndroidX package provides other kinds of annotations. You can check it here.

In the next article, we will check more points to improve your code review in an Android project ;)


要查看或添加评论,请登录

Gabriel Bronzatti Moro的更多文章

社区洞察

其他会员也浏览了