Modularity Lecture 4 Course Name: High Level Programming Language Year : 2010
3 Learning Outcomes At the end of this lecture, students are capable of: Understanding why modularity concept in programming is important.
4 Outline Material Modularity I –Interface versus Implementation –Advantage using Interface –Decomposition of File –Abstract Data –Abstract Data and Module Modularity II –Top-Down Programming and Bottom-Up Programming –Cohesion –Coupling
5 Module: a unit from the whole organization of a software system. groups together some functions, data, types, etc. Example: string functions from standard library C- string hides detail of implementation from user’s point of view (information hiding). Ex.: local variable in C function, can not be accessed by the caller function or main() Modularity I
6 Users and the module’s creator have a different perspective about the module itself. Interface: is an access point for users into a module. it explains to users about how to use it. it makes a module easier to be used and understood. it explains what services that the module provides, not how it provides the services. Interface versus Implementation
7 Users can create their own program, before the module is finished. Users programs and module program can be compiled separately. Implementation of the module itself can be changed, e.g. for fixing bugs, data structure, or for a better algorithm; without changing or re-compiling the users’ programs. Advantage of Using Interface
8 Interface: header file (.h) contains declaration of function, constant, variable, or users-defined types that have public scope data behavior. It also includes “comments” for users. Implementation: source file (.c) that contains definition of functions, along with local constants, variable, and users-defined type for private scope data behavior. It also includes “comments” for the creator of that particular module. Decomposition of File
9 Users include header file, for functions that they use, by using. Pay attention, that this is enough to compile the users programs. Modules also include their header file, so that the c compiler can check the consistency of the module program, before it reaches the linking stage. In the next slide, an example of interface is used for a function service of linked-list.c Dekomposisi File
10 Header File for linklist2.c /* Sorted linked lists of chars */ /* Linked list nodes are of type node_t */ typedef struct element { char contents; struct element *next; } node_t; /* Inserts new node with contents `key', keeping sorted. `ptr_to_head’: address of pointer to head of list */ int insertNode(node_t **ptr_to_head, char key); /*... */ int deleteNode(node_t **ptr_to_head, char); /*... */ void traverse(node_t *head); /*... */ void deleteAllNodes(node_t **); Decomposition of File
11 Abstract Data Type (ADT): A group of data and a group of instruction that operates on that particular data, in which the operation is independent from the module implementation. Example: stacks and lists. Description of a Stack ADT: Stack records data through a push and pop mechanisms. And it has a LIFO (Last In First Out) behavior, which means that input data will be in order, but output data will be in reverse order. Data Abstraction
12 There is only one stack ADT, but there is another possibility to implemented it. An particular implementation uses a particular data structure and also has functions that manipulate that particular data. In C language, a person can implement stack ADT in link-list or in array. The operations that are inside the stack ADT, are functions that are declared inside the interface file (header file.h). However, details of how the operation works, does not necessarily need to be known from the Users’ point of view. Abstraksi Data dan Modul
13 Top-Down Programming and Bottom- Up Programming Cohesion Coupling Modularity II
14 Top-Down Programming and Bottom-Up Programming Important Rule #1 Desain with Top-Down, but create with Bottom-Up Code and test the easiest component first. Test every component, before using it to create a more complex one.
15 Coupling Relation and dependency between functions: Coupling Control: –Happens when a function sends a signal control to another function. –Example: function call, return Coupling Data: –Sending data between functions –Example: function parameters, return values
16 Example 1 Structure Diagram with label that shows data coupling undanganKePesta ( nama, tanggal, jam ) { telepon ( nama ) ajakanKePesta (tanggal, jam ) bye ( nama ) } AjakanKePestabyetelepon inviteToParty nama tanggal jam nama tanggal jam Control Coupling Data Coupling
17 searchAddrBook telepon ( nama ) { set nomerTelp sebagai hasil dari cariBukuAlmt (nama ) angkat telepon tekan nomerTelp katakan “Hi nama, Ini Budi” } Example 2 Structure chart with labels showing data coupling ringUp dll... nama nomerTelp nama Return value or data that has been altered using pointer
18 Important Points about Coupling Objective: To maximize independency between module = to minimize coupling Use of Global Variables is not recommended by data coupling’s concept! –It is recommended not to use global variable in your program!
19 Important Facts about Coupling Where do you use control coupling in coding? –Function call: when a function is called, then the control is given to that function –Function return: when a coding inside a function has been completed or done, then the control is returned to the caller’s coding When do you use data coupling in coding? –Data is sent to one function via parameter –Data is returned from another function via “return” –Data is modified using pointer
20 Cohesion Cohesion: refers to how close the dependencies between lines of coding in one particular function. Logical Cohesion Logic (weak) –Ex.: Function Input / Output Function Cohesion (Strong) –Function that results with one output or activity. Purposes: function Cohesion
21 Example of Cohesive kontak ( perusahaan, pesan, mode ) { if mode = fax { kirimFax ( perusahaan, pesan) } else if mode = { kirim ( perusahaan, pesan) } Cohesive
22 Example of not Cohesive Not Cohesive kontak ( perusahaan, pesan, mode ) { if mode = fax { kirimFax ( perusahaan, pesan) } else if mode = { kirim ( perusahaan, pesan) } cetakBukuAlamat ( ) }
23 Cohesion Modules inside structural programming is tightly cohesive, but having a loosely coupling The amount of data coupling shows in general the position of a module in a programming hierarchy. Important Rule #2
24 Conclusion Modularity is an important concept in creating a complex software Design top-down, but create it bottom- up A good structure programming is one that has behaviors as follow; very cohesive with loosely coupling between modules.
25 Topic For Next Week Concurrent Versioning System –What is CVS – Concurrent Versioning System? –“Working Space” and “Repository Space” –Check-in and Check-out –Branch and Tag Assignment: –CVS has been around from the beginning of software development. For beginners, please read the following article, called “Tutorial-cvs.pdf”.
26 –Once you have read it, then install winCVS, and try to implement from what you have read into the winCVS. The only difference is that in the article it uses cvs command line interface, while winCVS is an IDE CVS for windows’ application. –Make a report of your experiment with winCVS and it will be discussed and presented in the class. Topic For Next Week