A Funny Java Flavoured Look at the World

Friday, September 15, 2006

The benefits of writing modular code

I got a fair bit of criticism for this putting be modular as my last tip in my blod entry 10 tips on writing reusable code, in truth I couldn't really think of any more tips and I wanted 10. I have been thinking about this recently and thought I would perhaps try and explain what I meant be your code being modular.

I view modular code as code that is like a black box, you know what you what you pass into the module and you know what comes out but you don't know or care how it does it. Similar really to 3rd party pieces of code or code that Sun writes, why bother wasting your time knowing how it works but focus on writing the code that uses it.Modular code has a public interface to a class or classes. I view modular code as being almost stand-alone pieces of code.

Modular code should have few connections to other modules so it should have weak coupling. The effect of coding a module means you are writing a self contained piece modular object is that it doesn't have links to any other modules and hence the weak coupling.

I know that what I am describing is probably just good coding practise by I feel the word modularity does help clarify the overall thinking of design decisions by thinking of each module as a separate self contained piece module, keeping back the tentacles of coupling.

One of the benefits of writing modular code is you will end up with a lots of modules which you will be able to use in lots of different projects and use different modules together.

By using different modules to create your software you will also be able to control the changes needed in the future by managing future change. Modules will naturally result in changes have a smaller effect on your code because you will hopefully be able to isolate the change to just one or two of your modules leaving the rest of the code unaffected by change.

Another good use of writing modular code is when the code is using an external resource like a configuration file, database or some other kind of input file. By accessing the resource from the one module you can then change the input type or the processing of the input without any of the other code being affected because the other code only interfaces with the one module.

Modular coding also uses encapsulation and information hiding built into my definition of modular code

Well I hope I have described what I meant by modular coding and I appreciate that I basically mean using all the good OO coding standards that we are told to use but I see that as a good thing. It also not new but I am just replying to the comments on my blog rightly criticising the vague use of the word modular.

2 Comments:

  • Modular code is certainly not OO-only.

    Modular code is all well and good, but you need to understand why you want modularity.

    High coupling is undesirable in a lot of code because it makes the code hard to use elsewhere. With today's IDEs, this is less and less relevant. You can later refactor the code so that it uses interfaces instead of being bound to implementations, etc. It is still quite relevant though - until IDEs have a way of automatically 'unifying' a class/method, so that it can be used separately, high coupling will remain undesirable.

    More relevant in many OO apps is interdependent state. It's more important to make a method change nothing (make it return a result), to remove the idea of partially-constructed objects, etc., than to stop it from depending on other objects/methods/classes.

    That's what functional programming concentrates on, and I think the two paradigms can work well together.

    By Blogger Ricky Clarkson, at Thu Oct 19, 11:56:00 pm 2006  

  • Thanks for your comment Ricky you make an excellent point.

    It can be very tempting to take shortcuts in methods and change the state of other objects. This I believe is known as having an side effect of using a method. This may be quicker in the short term but will make it difficult to use the code in another project.

    I have also been reading about J2EE design patterns and a lot of the patterns is about putting layers between objects to reduce the effect of change in the code.

    I suppose reducing the effect of change on your code is really the goal of modular code.

    By Blogger The Hosk, at Fri Oct 20, 12:20:00 am 2006  

Post a Comment

<< Home