modularity lecture 4 course name: high level programming language year : 2010

26

Upload: roderick-hines

Post on 01-Jan-2016

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Modularity Lecture 4 Course Name: High Level Programming Language Year : 2010
Page 2: Modularity Lecture 4 Course Name: High Level Programming Language Year : 2010

Modularity Lecture 4

Course Name: High Level Programming LanguageYear : 2010

Page 3: 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.

Page 4: Modularity Lecture 4 Course Name: High Level Programming Language Year : 2010

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

Page 5: Modularity Lecture 4 Course Name: High Level Programming Language Year : 2010

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

Page 6: Modularity Lecture 4 Course Name: High Level Programming Language Year : 2010

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

Page 7: Modularity Lecture 4 Course Name: High Level Programming Language Year : 2010

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

Page 8: Modularity Lecture 4 Course Name: High Level Programming Language Year : 2010

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

Page 9: Modularity Lecture 4 Course Name: High Level Programming Language Year : 2010

9

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

Dekomposisi File

Page 10: Modularity Lecture 4 Course Name: High Level Programming Language Year : 2010

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

Page 11: Modularity Lecture 4 Course Name: High Level Programming Language Year : 2010

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

Page 12: Modularity Lecture 4 Course Name: High Level Programming Language Year : 2010

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

Page 13: Modularity Lecture 4 Course Name: High Level Programming Language Year : 2010

13

•Top-Down Programming and Bottom-Up Programming•Cohesion•Coupling

Modularity II

Page 14: Modularity Lecture 4 Course Name: High Level Programming Language Year : 2010

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.

Page 15: Modularity Lecture 4 Course Name: High Level Programming Language Year : 2010

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

Page 16: Modularity Lecture 4 Course Name: High Level Programming Language Year : 2010

16

Example 1

Structure Diagram with label that shows data coupling

undanganKePesta ( nama , tanggal , jam ){ telepon ( nama ) ajakanKePesta (tanggal , jam ) bye ( nama )}

AjakanKePesta byetelepon

inviteToParty

na

ma

tan

gg

al

jam

namanama

tan

gg

al

jam

Control Coupling

Data Coupling

Page 17: Modularity Lecture 4 Course Name: High Level Programming Language Year : 2010

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

no

me

rTe

lp

na

ma

Return value or datathat has been altered

using pointer

Page 18: Modularity Lecture 4 Course Name: High Level Programming Language Year : 2010

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!

Page 19: Modularity Lecture 4 Course Name: High Level Programming Language Year : 2010

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

Page 20: Modularity Lecture 4 Course Name: High Level Programming Language Year : 2010

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

Page 21: Modularity Lecture 4 Course Name: High Level Programming Language Year : 2010

21

Example of Cohesive

kontak ( perusahaan , pesan , mode ){ if mode = fax { kirimFax ( perusahaan , pesan) } else if mode = email { kirimEmail ( perusahaan , pesan) }}

Cohesive

Page 22: Modularity Lecture 4 Course Name: High Level Programming Language Year : 2010

22

Example of not Cohesive

Not Cohesive

kontak ( perusahaan , pesan , mode ){ if mode = fax { kirimFax ( perusahaan , pesan) } else if mode = email { kirimEmail ( perusahaan , pesan) }}

cetakBukuAlamat ( )}

Page 23: Modularity Lecture 4 Course Name: High Level Programming Language Year : 2010

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

Page 24: Modularity Lecture 4 Course Name: High Level Programming Language Year : 2010

24 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.

Page 25: Modularity Lecture 4 Course Name: High Level Programming Language Year : 2010

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”. <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.>

Page 26: Modularity Lecture 4 Course Name: High Level Programming Language Year : 2010

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