How to Use Modular Programming

Program Software more Efficiently by Reusing Code

© Mark Alexander Bain

Aug 14, 2009
How to Use Modular Programming, Mark Alexander Bain
Modular programming is a simple yet effective way of writing software. The amount of code can be minimised and reused throughout an application.

Modular programming is a simple programming technique that can be applied to any programming language. It's as useful in C++ programming as it is in Java programming or programming in C. In fact some programming languages such as AJAX programming rely on it completely. Without this simple idea today's computer programmers would have an all but impossible job to do.

What is Modular Programming?

The idea behind modular programming is little more that common sense. All that the programmer has to do is to split their code into relatively small sections of code rather than leaving it in one massive chunk. Each section of code should devoted to one particular job and it should also be encapsulated into a procedure, for example a subroutine or a function.

The Advantages of Using Modular Programming

There are may good reasons for using modular programming:

  • code is easier to understand. Instead of pages and pages there are small, discreet blocks of code
  • errors are easier to find. An error will always be localised to a subroutine or function rather than buried somewhere in a mass of code
  • scoping of variables can be controlled more easily
  • duplication of code can be eradicated - rather than retyping the same code again and again, the programmer can create a single procedure
  • reuse of code - as well as being reused within a single application, modular programming allows code to be used in multiple applications
  • much less code needs to be written
  • programs are easier to design - the designer just needs to think about high level functions
  • collaborative programming is possible - modular programming enables more than one programmer to work a single application at the same time
  • code can be stored across multiple files

Of course, the technique can best be understood by looking at a simple example.

An Example of Code that Doesn't Use Modular Programming

A simple example of repeated code can be seen in:

PI = 3.14159265358979
R = 1
C = 2 * PI * R
A = PI * R * R
wscript.echo "Radius " & R & " Circumference " & C & " Area " & A
R = 2
C = 2 * PI * R
A = PI * R * R
wscript.echo "Radius " & R & " Circumference " & C & " Area " & A
R = 3
C = 2 * PI * R
A = PI * R * R
wscript.echo "Radius " & R & " Circumference " & C & " Area " & A
R = 4
C = 2 * PI * R
A = PI * R * R
wscript.echo "Radius " & R & " Circumference " & C & " Area " & A

Here the same functionality is repeated but does works (as can be seen in figure 1). However, the programmer can work more efficiently by using modular programming.

A Simple Example of Modular Programming

The aim of the programmer is now to ensure that code is reused as much as possible. They do this by identifying the code that can be place separate functions and subroutines:

PI = 3.14159265358979
function circumference (R)
circumference = 2 * PI * R
end Function
function area (R)
area = PI * R * R
end function
sub print_details (R)
C = circumference (R)
A = area (R)
wscript.echo "Radius " & R & " Circumference " & C & " Area " & A
end sub
print_details 1
print_details 2
print_details 3
print_details 4

The output is the same as before (as can be seen in figure 2), but this time the chance of the operation of the script being altered due to a typing error is greatly reduced, and if there is an error then the task of correcting it is made much simpler. The programmer can also use the functionality throughout their application without worrying about having to rewrite the code, thereby improving the efficiency of both the code and their time.

Futher Reading

Using Perl Subroutines, Packages and Modules

How to Create a VBScript Library


The copyright of the article How to Use Modular Programming in Computer Programming is owned by Mark Alexander Bain. Permission to republish How to Use Modular Programming in print or online must be granted by the author in writing.


How to Use Modular Programming, Mark Alexander Bain
Figure 1: Repeating the Same Code, Mark Alexander Bain
Figure 2: Modular Programming, Mark Alexander Bain
   


Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo