How to make code modular?

user175498 picture user175498 · Sep 18, 2009 · Viewed 38.3k times · Source

I have some Java programs, now I want to find out whether it is modular or not, if it is modular then up to what extent, because modularity can never be binary term i.e. 0 or 1. How do I decide that particular code is modular upto this much extent. I want to know how to make code much more modular?

Answer

OrangeRind picture OrangeRind · Sep 18, 2009

Some Benchmarks for modularity:

  1. How many times are you rewriting similar code for doing a particular task?
  2. How much do you have to refactor your code when you change some part of your program?
  3. Are the files small and easy to navigate through?
  4. Are the application modules performing adequately and independently as and when required?
  5. Is your code minimally disastrous? Does all hell break lose when you delete just one function or variable? Do you get 20-odd errors upon re-naming a class? (To examine this, you can implement a stacking mechanism to keep trace of all the hops in your application)
  6. How near is the code to natural language usage? (i.e. modules and their subcomponents represent more real world objects without giving much concern to net source file size).

For more ideas check out this blurb about modularity and this one on software quality

As for your concern on making your code more modular first you should ask yourself the above questions, obtain specific answers for them and then have a look at this.

The basic philosophy is to break down your application into as small of code fragments as possible, arranged neatly across a multitude of easily understandable and accessible directory layouts.

Each method in your application must do no more than the minimum quanta of processing needed. Combining these methods into more and more macro level methods should lead you back to your application.