lesson 4 mcmanus cop1006 1. pointers modules & functions cohesion & coupling local &...

50
Lesson 4 McManus COP1006 1

Upload: moses-farmer

Post on 22-Jan-2016

224 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Lesson 4 McManus COP1006 1.  Pointers  Modules & Functions  Cohesion & Coupling  Local & Global Variables  Parameters  Variable Names & Data Dictionaries

Lesson 4

McManusCOP1006 1

Page 2: Lesson 4 McManus COP1006 1.  Pointers  Modules & Functions  Cohesion & Coupling  Local & Global Variables  Parameters  Variable Names & Data Dictionaries

Pointers Modules & Functions Cohesion & Coupling Local & Global Variables Parameters Variable Names & Data Dictionaries Three Logic Structures

McManusCOP1006 2

Page 3: Lesson 4 McManus COP1006 1.  Pointers  Modules & Functions  Cohesion & Coupling  Local & Global Variables  Parameters  Variable Names & Data Dictionaries

Use Modules◦ Each part should have a particular function

Use the three logic structures◦ Sequential, Decision & Iteration

Don’t reinvent the wheel◦ Avoid rewriting identical processes

Use techniques to improve readability

McManusCOP1006 3

Page 4: Lesson 4 McManus COP1006 1.  Pointers  Modules & Functions  Cohesion & Coupling  Local & Global Variables  Parameters  Variable Names & Data Dictionaries

Making Modules Better!

McManusCOP1006 4

Page 5: Lesson 4 McManus COP1006 1.  Pointers  Modules & Functions  Cohesion & Coupling  Local & Global Variables  Parameters  Variable Names & Data Dictionaries

Each module should◦ Be functionally independent◦ Perform one problem-related task

Calculating IRS Withholding is one problem-related task, but may have multiple operations within the module

◦ When connected, use the smallest interface possible.

McManusCOP1006 5

Page 6: Lesson 4 McManus COP1006 1.  Pointers  Modules & Functions  Cohesion & Coupling  Local & Global Variables  Parameters  Variable Names & Data Dictionaries

The degree of interaction within a module.◦ Each module should perform one functionally-

related task…not necessarily one assignment statement.

◦ Concentration is on what goes on within the module.

McManusCOP1006 6

Term was coined by Larry Constantine in mid-1960’s

Page 7: Lesson 4 McManus COP1006 1.  Pointers  Modules & Functions  Cohesion & Coupling  Local & Global Variables  Parameters  Variable Names & Data Dictionaries

Stevens, Myers, Constantine, and Yourdon developed the Scale of Cohesion as a measure of the “black boxiness” of a module, and as a result, the maintainability of a module.

McManusCOP1006 7

Type Measure Black BoxFunctional Best Black BoxInformational ** BestSequential CommunicationalProcedural Gray BoxTemporalLogicalCoincidental Worst Transparent

or White Box**Originally not part of Scale

Page 8: Lesson 4 McManus COP1006 1.  Pointers  Modules & Functions  Cohesion & Coupling  Local & Global Variables  Parameters  Variable Names & Data Dictionaries

The degree of interaction between two modules.◦Interaction is the

interface, or lack thereof, between two modules. The interface is the

parameter list.

McManusCOP1006 8

Best (Lowest Interaction)

Worst (Highest Interaction)

Normal Data Stamp Control

Common

Content

Page 9: Lesson 4 McManus COP1006 1.  Pointers  Modules & Functions  Cohesion & Coupling  Local & Global Variables  Parameters  Variable Names & Data Dictionaries

Which pieces affect Cohesion and which affect Coupling?

McManusCOP1006 9

Private Sub Minimum(min As Long, y As Long, z As Long)

If y < min Then

min = y

End If

If z < min Then

min = z

End If

lblSmallest.Caption = "Smallest value is " & min

End Sub

Page 10: Lesson 4 McManus COP1006 1.  Pointers  Modules & Functions  Cohesion & Coupling  Local & Global Variables  Parameters  Variable Names & Data Dictionaries

What are the parameters?

McManusCOP1006 10

Private Sub Minimum(Scully As Long, Mulder As Long)

Dim Temp As Long

If Scully < Mulder Then

temp = Scully

Scully = Mulder

Mulder = temp

End If

End Sub

Page 11: Lesson 4 McManus COP1006 1.  Pointers  Modules & Functions  Cohesion & Coupling  Local & Global Variables  Parameters  Variable Names & Data Dictionaries

Cohesion’s Goal◦ To create a procedure that performs one

functionally-related task. Coupling’s Goal

◦ To protect global data and local data from being used within a procedure without declaring it on the procedure’s header

McManusCOP1006 11

Page 12: Lesson 4 McManus COP1006 1.  Pointers  Modules & Functions  Cohesion & Coupling  Local & Global Variables  Parameters  Variable Names & Data Dictionaries

High Cohesion ◦ Functional or Information

Low Coupling◦ Data, Stamp, Control

McManusCOP1006 12

Page 13: Lesson 4 McManus COP1006 1.  Pointers  Modules & Functions  Cohesion & Coupling  Local & Global Variables  Parameters  Variable Names & Data Dictionaries

The subparts to a Program

McManusCOP1006 13

Page 14: Lesson 4 McManus COP1006 1.  Pointers  Modules & Functions  Cohesion & Coupling  Local & Global Variables  Parameters  Variable Names & Data Dictionaries

“A module is a lexically contiguous sequence of program statements, bounded by boundary elements, having an aggregate identifier.” Yourdon & Constantine (1979)◦ A part of a larger system◦ Written and tested separately◦ Combined with other modules to form a

complete system◦ Used in top-down programming◦ Procedures & Functions

McManusCOP1006 14

Page 15: Lesson 4 McManus COP1006 1.  Pointers  Modules & Functions  Cohesion & Coupling  Local & Global Variables  Parameters  Variable Names & Data Dictionaries

A smaller part of the main program. 2 Advantages

◦ Eliminates the need to program the same thing more than once.

◦ Larger programs are easier to read when broken into procedures (and functions).

McManusCOP1006 15

Page 16: Lesson 4 McManus COP1006 1.  Pointers  Modules & Functions  Cohesion & Coupling  Local & Global Variables  Parameters  Variable Names & Data Dictionaries

Functions◦ A subprogram that acts like a mathematical

function: given a particular set of argument values, the

function returns a unique result. Use Return values that are associated with the name

of the function

McManusCOP1006 16

Page 17: Lesson 4 McManus COP1006 1.  Pointers  Modules & Functions  Cohesion & Coupling  Local & Global Variables  Parameters  Variable Names & Data Dictionaries

PascalFUNCTION doublenum(b : Integer) : Integer; BEGIN doublenum := 2 * b END;

Visual BasicPrivate Function Doublenum(b As Integer) As Integer Doublenum = 2 * bEnd Function

C++Int doublenum ( int b){ return 2 * b;}

McManusCOP1006 17

Page 18: Lesson 4 McManus COP1006 1.  Pointers  Modules & Functions  Cohesion & Coupling  Local & Global Variables  Parameters  Variable Names & Data Dictionaries

Each module ◦ is an entity by itself◦ has a single purpose◦ should be easily read, modified and maintained◦ Length is governed by function and number of

instructions contained within◦ Controls the order of processing

McManusCOP1006 18

Page 19: Lesson 4 McManus COP1006 1.  Pointers  Modules & Functions  Cohesion & Coupling  Local & Global Variables  Parameters  Variable Names & Data Dictionaries

McManusCOP1006 19

Page 20: Lesson 4 McManus COP1006 1.  Pointers  Modules & Functions  Cohesion & Coupling  Local & Global Variables  Parameters  Variable Names & Data Dictionaries

ControlControl ◦Demonstrates

overall flow of data Initialization & Initialization &

Wrap-UpWrap-Up◦Processes

instructions to be performed once (either at beginning or at the end of the program)

◦Used typically in batch processing

McManusCOP1006 20

Process DataProcess Data◦Calculation◦Print◦Read and

Validation EventEvent

◦Used in OO and some event driven programming languages

◦More about these later

Page 21: Lesson 4 McManus COP1006 1.  Pointers  Modules & Functions  Cohesion & Coupling  Local & Global Variables  Parameters  Variable Names & Data Dictionaries

Most often called “Main” All other modules, procedures and functions

are subordinate to the control module

Sub MainCall ProcedureA(X, Y)Call ProcedureB(A, B)

End Main

McManusCOP1006 21

Page 22: Lesson 4 McManus COP1006 1.  Pointers  Modules & Functions  Cohesion & Coupling  Local & Global Variables  Parameters  Variable Names & Data Dictionaries

Used in some languages to initialize variables or processes◦ Examples

Opening files Initializing variables Printing report headings

Procedure BeginDim X, Y, Z As IntegersOpen Payroll file

End Procedure ‘Begin

McManusCOP1006 22

Page 23: Lesson 4 McManus COP1006 1.  Pointers  Modules & Functions  Cohesion & Coupling  Local & Global Variables  Parameters  Variable Names & Data Dictionaries

Calculation◦ Performs

Arithmetic operations Accumulations Sorting or Searching

Private Sub Double (X, Y)Dim Temp as IntegerTemp = X * Y

End Sub

McManusCOP1006 23

Read and Data Validation◦ Reads and validates

input data Usually separate

modules

Private Sub Verify(X)If X < 0 Or X > 10 Then

lblMessage.Text = “Data Error”End If

End Sub

Page 24: Lesson 4 McManus COP1006 1.  Pointers  Modules & Functions  Cohesion & Coupling  Local & Global Variables  Parameters  Variable Names & Data Dictionaries

Used to close out processes◦ Examples

Closing files Printing reports Returning updated data to databases

Procedure EndClose Employee fileClose Payroll file

End Procedure ‘End

McManusCOP1006 24

Page 25: Lesson 4 McManus COP1006 1.  Pointers  Modules & Functions  Cohesion & Coupling  Local & Global Variables  Parameters  Variable Names & Data Dictionaries

Scope!

McManusCOP1006 25

Page 26: Lesson 4 McManus COP1006 1.  Pointers  Modules & Functions  Cohesion & Coupling  Local & Global Variables  Parameters  Variable Names & Data Dictionaries

The area of a program where an identifier (variable) is visible

When an identifier has multiple declarations in different modules, the most local declaration is used each time that identifier is referenced. (overloading)

Global or “non-local” variables subject to side effects.

McManusCOP1006 26

Page 27: Lesson 4 McManus COP1006 1.  Pointers  Modules & Functions  Cohesion & Coupling  Local & Global Variables  Parameters  Variable Names & Data Dictionaries

Caused when the value of a global variable is changed within a procedure or function◦ Any effect of one module on another module that

is not a part of the explicitly defined interface between them

Also caused when a variable name is used in globally and locally (causes overloading)

A nasty effect that should be avoided!A nasty effect that should be avoided!

McManusCOP1006 27

Page 28: Lesson 4 McManus COP1006 1.  Pointers  Modules & Functions  Cohesion & Coupling  Local & Global Variables  Parameters  Variable Names & Data Dictionaries

Declared within the main program Can be referenced anywhere in the program

◦ Is visible and accessible everywhere

McManusCOP1006 28

X, Y, Z

A

CB

X, Y & Z are Global to modules

A, B & C

Page 29: Lesson 4 McManus COP1006 1.  Pointers  Modules & Functions  Cohesion & Coupling  Local & Global Variables  Parameters  Variable Names & Data Dictionaries

Declared within a module◦ Has no effect outside the procedure or function in

which it is declared Can be referenced only within a procedure

or a function

McManusCOP1006 29

X, Y, Z

A m

C pB n

Within A, Variable m is defined, but can also see X, Y, & Z

Within B, Variable n is defined , but can also see X, Y, & Z

Within C, Variable p is defined , but can also see X, Y, & Z

Page 30: Lesson 4 McManus COP1006 1.  Pointers  Modules & Functions  Cohesion & Coupling  Local & Global Variables  Parameters  Variable Names & Data Dictionaries

Private Sub Minimum(Scully As Long, Mulder As Long)

Dim Temp As Long

If Scully < Mulder Then

temp = Scully

Scully = Mulder

Mulder = temp

End If

End Sub

McManusCOP1006 30

Scully & Mulder are what type

of variables?

What type of variable is Temp?

Page 31: Lesson 4 McManus COP1006 1.  Pointers  Modules & Functions  Cohesion & Coupling  Local & Global Variables  Parameters  Variable Names & Data Dictionaries

McManusCOP1006 31

Scope of X, Y, Z, Procedure1

Scope of M, N, Me, X, You

program ShowScope; var X, Y, Z : Real; procedure Procedure1

(var M, N, Me : Real); var X, You : Real; begin {Procedure1} ....... end; {Procedure 1}

begin {ShowScope} Procedure1(X, Y, Z)end. {ShowScope}

Page 32: Lesson 4 McManus COP1006 1.  Pointers  Modules & Functions  Cohesion & Coupling  Local & Global Variables  Parameters  Variable Names & Data Dictionaries

How we avoid side effects!

McManusCOP1006 32

Page 33: Lesson 4 McManus COP1006 1.  Pointers  Modules & Functions  Cohesion & Coupling  Local & Global Variables  Parameters  Variable Names & Data Dictionaries

Are the variables that are passed into and out of modules

Use global parameters ◦ (to the procedure or function)

Pass values through the use of variables Actual and Formal parameters Call-by-reference & Call-by-value

McManusCOP1006 33

Page 34: Lesson 4 McManus COP1006 1.  Pointers  Modules & Functions  Cohesion & Coupling  Local & Global Variables  Parameters  Variable Names & Data Dictionaries

A measure of the quantity of data passing through a module’s interface.

Is also a measure of the module’s coupling. The goal is to strive for a minimal amount of

information being passed.

McManusCOP1006 34

Page 35: Lesson 4 McManus COP1006 1.  Pointers  Modules & Functions  Cohesion & Coupling  Local & Global Variables  Parameters  Variable Names & Data Dictionaries

Input Parameter ◦ Information passed into a procedure, but not

returned or passed out of the procedure. Output Parameter

◦ Information returned to the calling program from a procedure.

Input/Output Parameter ◦ Information passed into a procedure, perhaps

modified, and a new value returned.

McManusCOP1006 35

Page 36: Lesson 4 McManus COP1006 1.  Pointers  Modules & Functions  Cohesion & Coupling  Local & Global Variables  Parameters  Variable Names & Data Dictionaries

Provide the communication links between the main program and its modules.

Make procedures and functions more versatile.◦ Different data can be manipulated each time

the module is called. Come in two types:

◦ Actual◦ Formal

McManusCOP1006 36

Page 37: Lesson 4 McManus COP1006 1.  Pointers  Modules & Functions  Cohesion & Coupling  Local & Global Variables  Parameters  Variable Names & Data Dictionaries

Actual Parameters◦ Are substituted for the formal parameter at the

time the procedure is called.◦ Parameters used in the call statement

Statements that transfer control to a procedure.

◦ Data types must be assignment compatible with its corresponding formal parameter

◦ Can be a variable, constant or an expression◦ Can be call-by-value or call-by-reference

McManusCOP1006 37

Page 38: Lesson 4 McManus COP1006 1.  Pointers  Modules & Functions  Cohesion & Coupling  Local & Global Variables  Parameters  Variable Names & Data Dictionaries

Formal Parameters◦ Parameters declared in the procedure header◦ Is a list of “place marker” names used in the

procedure’s declaration. ◦ Can include the data type of the valued

parameters. ◦ Must be a variable◦ Can be call-by-value or call-by-reference

McManusCOP1006 38

Page 39: Lesson 4 McManus COP1006 1.  Pointers  Modules & Functions  Cohesion & Coupling  Local & Global Variables  Parameters  Variable Names & Data Dictionaries

Determined by position in respective parameter lists

Lists must be the same size, although the names may differ

Data Types of corresponding actual and formal parameters must be identical

McManusCOP1006 39

Page 40: Lesson 4 McManus COP1006 1.  Pointers  Modules & Functions  Cohesion & Coupling  Local & Global Variables  Parameters  Variable Names & Data Dictionaries

McManusCOP1006 40

8.0

10.0

8.0

10.0

? ?

Formal Parameters

Local Variables

Sum Average

Num1

Num2

Actual Parameters Var1

Var2

Main program data area Procedure data area

Page 41: Lesson 4 McManus COP1006 1.  Pointers  Modules & Functions  Cohesion & Coupling  Local & Global Variables  Parameters  Variable Names & Data Dictionaries

By Reference (Call-by-Reference)◦ Passing a variable to a procedure is called passing

an argument by reference, because a variable can be modified by a procedure and returned to the calling module.

By Value (Call-by-Value)◦ Passing a literal value (such as a string in

quotation marks) to a procedure is called passing an argument by value, because a value cannot be modified by a procedure.

McManusCOP1006 41

Page 42: Lesson 4 McManus COP1006 1.  Pointers  Modules & Functions  Cohesion & Coupling  Local & Global Variables  Parameters  Variable Names & Data Dictionaries

The default for parameter passing Gives access to the contents of the storage

area where values are stored Giving the called procedure the ability to

directly access the caller’s data Allowing changes in the data

McManusCOP1006 42

Page 43: Lesson 4 McManus COP1006 1.  Pointers  Modules & Functions  Cohesion & Coupling  Local & Global Variables  Parameters  Variable Names & Data Dictionaries

Protects the data being passed Accomplished by creating a copy of the

value◦ without affecting the original value of the variable

Thus…◦ Called procedure is unable to change the values

stored in the variable’s storage area Helps avoid Side Effects!

McManusCOP1006 43

Page 44: Lesson 4 McManus COP1006 1.  Pointers  Modules & Functions  Cohesion & Coupling  Local & Global Variables  Parameters  Variable Names & Data Dictionaries

Call Parameters

Actual Parameters Valued Variable

McManusCOP1006 44

Procedure Header Parameters

Formal ParametersValuedVariable

Parameter Interface using Global Variables

Page 45: Lesson 4 McManus COP1006 1.  Pointers  Modules & Functions  Cohesion & Coupling  Local & Global Variables  Parameters  Variable Names & Data Dictionaries

Names & the Data Dictionary

McManusCOP1006 45

Page 46: Lesson 4 McManus COP1006 1.  Pointers  Modules & Functions  Cohesion & Coupling  Local & Global Variables  Parameters  Variable Names & Data Dictionaries

Use mnemonic terms◦ Use a variable name that relates the name of the

variable to its usage Contributes to self-documenting code

◦ Which reduces the amount of commenting required◦ Z = X * Y What is it doing (besides

multiplication?)◦ SalesTax = SalesTaxRate * Cost (this you know)

Examples◦ SalesTax, SalesRate, PayRate, Temp

McManusCOP1006 46

Page 47: Lesson 4 McManus COP1006 1.  Pointers  Modules & Functions  Cohesion & Coupling  Local & Global Variables  Parameters  Variable Names & Data Dictionaries

Defines all of the variables used within a program

Lists:◦ Names ◦ Data type◦ Location defined & accessed◦ Test Data (or error checking)◦ Domain (range of possible values)

McManusCOP1006 47

Page 48: Lesson 4 McManus COP1006 1.  Pointers  Modules & Functions  Cohesion & Coupling  Local & Global Variables  Parameters  Variable Names & Data Dictionaries

Item Name(no

spaces)

Data Type

Modules Domain(Range)

Scope

Hours worked

Hours Numeric-real

GetHours

CalcGrossPay

0 n 168

Global

Global

Gross Pay

GrossPay Numeric-real

CalcGrossPay

CalcDeductions

CalcNetPay

PrintPayChecks

0 n 1 million

Global

Global

Global

Global

Net Pay

NetPay Numeric-real

CalcNetPay

PrintPayChecks

0 n 1 million

Local

Global

McManus COP1006 48

Page 49: Lesson 4 McManus COP1006 1.  Pointers  Modules & Functions  Cohesion & Coupling  Local & Global Variables  Parameters  Variable Names & Data Dictionaries

SequentialSequential◦ One statement follows another

Selection Selection (Decision)◦ Allows choices based on the data◦ IfThenElse, Nested If’s, Case, Switch

IterationIteration (Looping or Repetition)◦ Allows statements to be repeated a specified

number of times◦ While, Do, For, Do Until, Repeat

McManusCOP1006 49

Page 50: Lesson 4 McManus COP1006 1.  Pointers  Modules & Functions  Cohesion & Coupling  Local & Global Variables  Parameters  Variable Names & Data Dictionaries

McManusCOP1006 50