7 Ways to Make Your Code More Reusable

Swarnendu De June 10, 2015

milan blog pic

“Code Reusability” is a common term for developers. Still, when the time comes to apply the rules, they face several difficulties. Here are some simple ideas that help write reusable code and save you time.

1. Proper planning before a project starts:

Before you jump to coding, go back a step, gather your team members, plan the sections that your application needs and divide it into modules.

You can divide the development process in two ways. For some applications you may need to use same kind of views & functionalities throughout; simply divide the app in different modules depending on the functionalities in the view & controller. Otherwise you can divide the app depending on separate segments of each function.

project planning

For example – Consider an app which requires data-model, database, and some vital components Controller and view. All of these components should be developed in such a way that any change in one layer doesn’t reflect on another. Unless we migrate to a different database, the rest of the layers should remain unaffected.
So, plan before you code. A proper plan can reduce the development effort significantly.

2. Utility Classes:

You can make an UtilityProjectIndependent Class which can be directly used in other projects. For example:

    1. All date string format methods,
    2. Html error code detection and error message,
    3. Email validation,
    4. Date formating,
    5. Image compression,
    6. Image conversion (png-jpg or vice-versa),
    7. Image download, etc

These can be added to UtilityProjectIndependent class. You can make project specific Utility class with very useful methods such as sorting an array of object based on specific field etc. There are lots of other useful methods which developers need every now and then – discover and add them all to your Utility class.

3. Problems of Copy & Paste Function Snippets:

When I started developing iOS apps, I used to copy function code snippets from one model to another model without making a new separate common class for it. This is quite a common issue among the beginner level developers where they find it difficult to plan before writing it. And the obvious results are multi-fold like:

    1. If you need to change any logic or you want to add new code, you need to update it at all the other places
    2. It will be very difficult to maintain same method throughout the app.

Here is the solution. If any group of code is required at least twice in any project, you should make a method of it. Bunch of similar functions or same purpose functions should be put in a new class. Bunch of similar classes without any dependence makes a package or reusable library or framework.

4. Functions should focus on specific task:

Here is the most important thing to keep in mind when you define a method: be specific to task of that method. Give a proper name to the method so that anyone can understand. Don’t write extra code out of specific task. Therefore, a method of addition should ideally take care of an additional functionality, nothing else. There shouldn’t be any code of parsing or string to number conversion etc. Each method will be assigned for specific work. The idea is to make them reusable for other projects.

5. Divide a Complex Task (read ‘function’) into several Simple Tasks:

 Always be specific to the task of the method and divide it under several methods to make it simple. For example, consider that you need to parse a json file, pick up one date of birth and calculate the age. This task is so small that people will want to write the whole task in one method, most of the time. You should use reusable model class. You should parse the json and make a model object out of it. Then pass it to another method which will bring out only the DOB followed by a utility method which will give you the difference between two dates of the years. Now it is the time to display the age only.
You must be thinking that it is similar to “shooting flies with a cannon”. But in actuality you need to start practicing at basic levels to make this a habit.

6. Be lazy:

be lazy

When I was discussing the said topic with my senior Raju Bera, he advised me to be a lazy programmer by never writing similar code twice in a lifetime. So, write code / methods in such a way which are useful across projects.

7. Maintain Language file:

Finally, maintain language file from the first day supporting at least one language. We usually find that localization of app is required on second version of the app. To avoid the nightmare, it is better to maintain a language file.

Most of the developers are more concerned about the new functionality of an app and the complicacies of that. But, if we are patient and ready to implement the suggestions mentioned above, we could reduce development time significantly. To keep code clean I would recommend you to Robert C. Martin’s (Uncle Bob) book as your next best step.