Modulizing Source Code
From Kietzman.org
Contents |
Introduction
Code modulization basically means that when a section of logic within source code is repeatedly used, it should then be seperated out into a module. This technique applies to both base and web code, but is implemented differently depending on the language. The overall purpose of modulization is to avoid recreating the wheel. By doing so you will help shorten the software development lifecycle and decrease the amount of possible bugs in the software.
There are three general techniques available for modulizing source code: templates, functions, objects. We will touch on the three techniques on a per language basis. Not all programming languages are capable of utilizing all three techniques.
Basic Understanding
- Template: modulize without encapsulating
- Function: modulize while encapsulating internal variables
- Object: modulize while encapsulating internal variables and functions
C/C++
In C or C++ you have the ability to modulize using templates and functions. In C++ you also have the ability to modulize using objects.
Template
template.h
#define VERSION "1.0" #define mVER_USAGE(A,B) cout << endl << A << " Version: " << B << endl << endl
Function
functions.h
int function add(const int, const int); float function divide(const int, const int);
functions.c
int add(const int nNum1, const int nNum2)
{
return nNum1 + nNum2;
}
float divide(const float fNumerator, const float fDenominator)
{
return fNumerator / fDenominator;
}
Object
object.h
#ifndef _OBJECT_
#define _OBJECT_
extern "C++"
{
class CObject
{
public:
CObject();
~CObject();
int add(const int, const int);
float divide(const float, const float);
};
}
#endif
object.cpp
#include "object.h"
extern "C++"
{
CObject::CObject()
{
}
CObject::~CObject()
{
}
int CObject::add(const int nNum1, const int nNum2)
{
return nNum1 + nNum2;
}
float CObject::divide(const float fNumerator, const float fDenominator)
{
return fNumerator / fDenominator;
}
}
Cascading Style Sheets (CSS)
In CSS you have the ability to modulize using templates.
Template
template.css
a.std_link {}
a.std_link:visited {}
a.std_link:hover {}
a.std_bold_link {font-weight: bold;}
a.std_bold_link:visited {font-weight: bold;}
a.std_bold_link:hover {font-weight: bold; color: orange; text-decoration: none;}
Java
In Java you have the ability to modulize using objects only.
Object
object.java
public class object
{
public int add(int nNum1, int nNum2)
{
return nNum1 + nNum2;
}
}
Javascript
In Javascript you have the ability to modulize using functions.
Function
functions.js
function add(nNum1, nNum2)
{
return nNum1 + nNum2;
}
function divide(nNumerator, nDenominator)
{
return nNumerator / nDenominator;
}
Perl
In Perl you have the ability to modulize using functions (sub).
Function
functions.pl
sub add
{
return $nNum1 + $nNum2;
}
sub divide
{
return $nNumerator / $nDenominator;
}
PHP
In PHP you have the ability to modulize using templates, functions and objects.
Template
template.php
<?php $gstrTitle = 'Page Title'; $gstrServer = 'server name'; ?>
Function
function.php
<?php
function add($nNum1, $nNum2)
{
return $nNum1 + $nNum2;
}
function divide($nNumerator, $nDenominator)
{
return $nNumerator / $nDenominator;
}
?>
Object
object.php
<?php
class object
{
public function __construct()
{
}
public function __destruct()
{
}
public function add($nNum1, $nNum2)
{
return $nNum1 + $nNum2;
}
public function divide($nNumerator, $nDenominator)
{
return $nNumerator / $nDenominator;
}
}
?>
Shell
In Shell you have the ability to modulize using functions.
Function
functions.sh
add()
{
echo `expr ${1} + ${2}`
}
divide()
{
echo `expr ${1} / ${2}`
}
