“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.
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.
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.
You can make an UtilityProjectIndependent Class which can be directly used in other projects. For example:
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.
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:
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.
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.
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.
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.
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.