There is a common misconception that modularity is inherently a good thing. In fact modular systems are not optimized for performance and are often harder to maintain depending on the extent of the modularization. It is similar problem as OOP which as a drastic effect on the program as a whole. Once a modularization technique is chosen it usually affects the entire program permanently crippling it is not properly implemented. Modularity similar to OOP is a useful tool in the appropriate context. One should not start writing a modular program. However one should anticipate the areas of the program which will benefit from being modular.
Lets us look at a simple example 1;
Code:
start program
print("-- Add 2 numbers --");
print("Enter first Number:");
firstnumber = readkeyboard();
print("Enter second Number:");
secondnumber = readkeyboard();
result = firstnumber + secondnumber;
print("Result:" + result);
end program
As you can see the Example 1 is pretty simple and efficient. What I'm going to do now is redesign the above program in deferent stages of modularity. What you will notice is that as the program becomes more modular it becomes more complicated and at the same time less efficient.
[bu]Lets us look at example 2;[/bu]
Code:
function acceptnumber( screenmessage )
print( screenmessage );
return readkeyboard();
end function
start program
print("-- Add 2 numbers --");
firstnumber = acceptnumber("Enter first Number:");
secondnumber = acceptnumber("Enter second Number:");
result = firstnumber + secondnumber;
print("Result:" + result);
end program
In example 2 we have introduced a function called acceptnumber() which allows us to reduce the amount of lines in the main program by moving the repeat tasks of printing a message on the screen and reading the keyboard from the user.
[bu]Lets us look at example 3;[/bu]
Code:
function acceptnumber( screenmessage )
print( screenmessage );
return readkeyboard();
end function
function do_addition( num1, num2 )
return num1 + num2;
end function
start program
print("-- Add 2 numbers --");
firstnumber = acceptnumber("Enter first Number:");
secondnumber = acceptnumber("Enter second Number:");
print("Result:" + do_addition( firstnumber, secondnumber ) );
end program
In example 3 I've moved the code that does the add calculation into its own function called do_addition. do_addition accepts the 2 numbers that need to be added and returns the result. I've also go ahead and gotten rid of the result variable since its not really used for anything important.
[bu]Lets us look at example 4;[/bu]
Code:
constants = array ("-- Add 2 numbers --", "Enter first Number:", "Enter second Number:", "Result:", "addition" );
function acceptnumber( screenmessage )
print( screenmessage );
return readkeyboard();
end function
function do_addition( num1, num2 )
return num1 + num2;
end function
function get_data_input(settings)
print(settings[0]);
input1 = acceptnumber(settings[1]);
input2 = acceptnumber(settings[2]);
return array( input1, input2 );
end function
function get_calculation_output(settings, data_array)
if( settings[3] == "addition" ) {
return settings[3] + do_addition( data_array[0], data_array[1] );
}
return "no work or invalid settings";
end function
start program
data_array = get_data_input(settings);
output_text = get_calculation_output(settings, data_array);
print(output_text);
end program
In the 4th and final example I when all the way and separated the input of data from the output and calculation. You maybe able to see various advantages and disadvantages of this method but it was done to demonstrate my final conclusion.
[bu]Conclusion[/bu]
As you can hopefully see, all four example do exactly the same thing, except each one becoming increasingly modular, less efficient and more complicated. I could have done a 5th step and implemented it in OOP but by now you should be able to seem my point. A program can be simple without being modular and on the other hand be modular but VERY complicated. A program being modular does not mean that it is simple. And modular program is not necessarily more readable that a simple program.
original article / permanent link