Course Name: High Level Programming Language Year : 2010 Modularity Lecture 4 Learning Outcomes At the end of this lecture, students are capable of: • Understanding why modularity concept in programming is important. 3 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 4 Module: Modularity I • a unit from the whole organization of a software system. • groups together some functions, data, types, etc. Example: string functions from standard library Cstring • 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() 5 Interface versus Implementation 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. 6 Advantage of Using Interface • 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. 7 Decomposition of File 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. 8 Dekomposisi File Users include header file, for functions that they use, by using <#include the header files’s name>. 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 9 Decomposition of File 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 **); 10 Data Abstraction 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. 11 Abstraksi Data dan Modul 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. 12 Modularity II •Top-Down Programming and BottomUp Programming •Cohesion •Coupling 13 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. 14 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 15 values Structure Diagram with label that shows data coupling Control Coupling jam nama Example 1 Data Coupling tanggal undanganKePesta ( nama , tanggal , jam ) { telepon ( nama ) ajakanKePesta (tanggal , jam ) inviteToParty bye ( nama ) } tanggal jam telepon AjakanKePesta 16 bye Structure chart with labels showing data coupling Example 2 telepon ( nama ) { set nomerTelp sebagai hasil dari cariBukuAlmt (nama ) angkat telepon tekan nomerTelp katakan “Hi nama, Ini Budi” } ringUp nomerTelp 17 nama Return value or data that has been altered using pointer searchAddrBook dll... 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! 18 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” 19 – Data is modified using pointer 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 20 Example of Cohesive kontak ( perusahaan , pesan , mode ) { if mode = fax { kirimFax ( perusahaan , pesan) } else if mode = email { kirimEmail ( perusahaan , pesan) } } Cohesive 21 Example of not Cohesive kontak ( perusahaan , pesan , mode ) { if mode = fax { kirimFax ( perusahaan , pesan) } else if mode = email { kirimEmail ( perusahaan , pesan) } } cetakBukuAlamat ( ) Not Cohesive } 22 Cohesion Important Rule #2 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. 23 Conclusion • Modularity is an important concept in creating a complex software • Design top-down, but create it bottomup • A good structure programming is one that has behaviors as follow; very cohesive with loosely coupling between modules. 24 24 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”. <note: you can download the article from the internet, i.e.: wiki.wheder.net/images/0/0d/Tutorial-cvs.pdf or from binusmaya download session in lecture 4.> 25 Topic For Next Week –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. 26