|
||||||
|
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 ProgrammingThere are may good reasons for using modular programming:
Of course, the technique can best be understood by looking at a simple example. An Example of Code that Doesn't Use Modular ProgrammingA 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 ProgrammingThe 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 ReadingUsing 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.
|
||||||
|
|
||||||
|
|
||||||