SOME TIPS FOR WRITING CLEAN CODE FOR ANDROID DEVELOPERS

Ozan Şan
4 min readApr 26, 2022

--

The concept of clean code has started to come up very often lately, software developers have started to care about it. Clean code actually means easy to understand and changeable code. You write your code in such a way that another programmer can easily understand and grasp this code without any additional explanation or knowledge. In clean coding, everyone can focus on some points, even if it is a little different for himself. I would like to dwell on a few points that I care about and share them with you.

  • Make your code understandable without the need for explanation with additional comments. With the right method and variable naming, your code can be simple and organized without the need for comment lines.

As we have seen in a simple factorial example, it is possible to understand what the code is doing without the need for comment lines.

  • Do not give up on creating separate classes and methods for each operation you want to do. If you don’t, you may have to rewrite the code that does the same function over and over, which is undesirable.
  • If you want to do the same operation in more than one class, instead of writing the same method to each class separately, create a static method in a base class and access and use this method over the class.
We had to define a method inside each class to play the audio file.

Instead, we can call the helper from a class by defining the static method once, as follows.

  • When you want to use similar error or successful messages, you should extract them from the strings.xml file and use them, not as a new string each time. In fact, not only for these messages, but also all string values ​​that will be included in the application should be created in this file and used by reference from there. If you proceed through the strings.xml file, it will be very easy to make changes in the future. In addition, it would be more logical to define very long texts in the strings.xml file and call it from there instead of writing it in classes.
Bad example

It is most efficient to call strings from the Strings.xml file.

  • Avoid creating objects from scratch as much as possible, this affects the efficiency and speed of the program.
  • The sooner your code gets out of the loop, the more successful it is. As soon as you get the result of the operation you want to do, you should be done with it. Sometimes using extra variables is also unnecessary for the code. In fact, I tried to briefly talk about the general logic of the code in this article, I suggest you set up your logic accordingly. A wide variety of examples can be given, I will mention a few.

--

--