Do you code DRY!

Excellence is not enough!

Do you code DRY!

What is DRY principle, and how to achieve that in your android development journey, let’s discuss.

DRY stands for “don’t repeat yourself!“. it simply means that you have to never write any piece of code twice otherwise you are really doing it wrong. because when a logic related to that piece of code changes we have to search all over the code-base and find that logic for refactoring purposes and in a relatively large software it is really a disaster, believe me!.

So how we could make it DRY, it’s a big question here.

the key concept for DRY coding is to break your bunch of codes into smaller reusable pieces whenever possible. let’s consider you have a method that is exceeding 30 lines of code and it has some different logic then you certainly could refractor it and create some smaller methods that has general usages, for example a method that formats strings based on a given criteria or a method converts some model class to another, it’s all make your code more clean and more simpler to debug and work with.

For class design try to make a parent abstract classes that has some general basic logic in there and then use that class to add more functionality in it. for example when you create a login activity in your android code a good idea is to create a base class for login purposes and use that base class for login and also for registeration purposes as this class could contain similar methods and logic to accommodate such functionality. or for listing purposes you could use such idea.

Always gather utility methods and functions into one related class and use those methods from that parent class, for example gather all Networking methods into one class called NetworkUtils, so you have never need to create repeated unnecessary methods in your activities.

Try to gather application wide Constants into one single class so that you will refer to that class when needed.

Never hard-code any string, measurement constant and so on…, it is a stupid habit, use res folder files for that.

Always try to review your code and think about the ways you could modularize and break the bulks into smaller pieces.

Always try to keep every class as a Single Responsible unit means that you have always just one reason to change your class.

For layout and UI parts you could separate reusable parts into a different layout file and use <Layout/> attribute to place that part into the main layout, so that you could reuse that layout whenever needed.

Also consider using Custom Views when you think you need a special form of view that is not provided by default view types, then you could reuse that part easily across your code base and even in other projects as third-party libraries.

Another awesome idea you could use to achieve DRY code is the Module concept in android, you could modularize your different parts of code, put the common functionality in a library and extend that library logic for different situation then gather related logics into one piece of code, now you could have even different types of one application in a single code-base!.

That’s it, hope you enjoy.