Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Lesson 4 McManus COP1006 1.  Pointers  Modules & Functions  Cohesion & Coupling  Local & Global Variables  Parameters  Variable Names & Data Dictionaries."— Presentation transcript:

1 Lesson 4 McManus COP1006 1

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

3  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 McManusCOP10063

4 Making Modules Better! McManus COP1006 4

5  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. McManusCOP10065

6  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. McManusCOP10066 Term was coined by Larry Constantine in mid-1960’s

7  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. McManusCOP10067 Type Measure Black Box Functional BestBlack Box Informational ** Best Sequential Communicational ProceduralGray Box Temporal Logical Coincidental WorstTransparent or White Box **Originally not part of Scale

8  The degree of interaction between two modules. ◦ Interaction is the interface, or lack thereof, between two modules.  The interface is the parameter list. McManusCOP10068 Best (Lowest Interaction) Worst (Highest Interaction) Normal Data Stamp Control Common Content

9  Which pieces affect Cohesion and which affect Coupling? McManusCOP10069 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

10  What are the parameters? McManusCOP100610 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

11  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 McManusCOP100611

12  High Cohesion ◦ Functional or Information  Low Coupling ◦ Data, Stamp, Control McManusCOP100612

13 The subparts to a Program McManus COP1006 13

14  “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 McManusCOP100614

15  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). McManusCOP100615

16  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 McManusCOP100616

17  Pascal FUNCTION doublenum(b : Integer) : Integer; BEGIN doublenum := 2 * b END;  Visual Basic Private Function Doublenum(b As Integer) As Integer Doublenum = 2 * b End Function  C++ Int doublenum ( int b) { return 2 * b; } McManusCOP100617

18  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 McManusCOP100618

19 McManusCOP100619

20  Control ◦ Demonstrates overall flow of data  Initialization & Wrap-Up ◦ Processes instructions to be performed once (either at beginning or at the end of the program) ◦ Used typically in batch processing McManusCOP100620  Process Data ◦ Calculation ◦ Print ◦ Read and Validation  Event ◦ Used in OO and some event driven programming languages ◦ More about these later

21  Most often called “Main”  All other modules, procedures and functions are subordinate to the control module Sub Main Call ProcedureA(X, Y) Call ProcedureB(A, B) End Main McManusCOP100621

22  Used in some languages to initialize variables or processes ◦ Examples  Opening files  Initializing variables  Printing report headings Procedure Begin Dim X, Y, Z As Integers Open Payroll file End Procedure ‘Begin McManusCOP100622

23  Calculation ◦ Performs  Arithmetic operations  Accumulations  Sorting or Searching Private Sub Double (X, Y) Dim Temp as Integer Temp = X * Y End Sub McManusCOP100623  Read and Data Validation ◦ Reads and validates input data  Usually separate modules Private Sub Verify(X) If X 10 Then lblMessage.Text = “Data Error” End If End Sub

24  Used to close out processes ◦ Examples  Closing files  Printing reports  Returning updated data to databases Procedure End Close Employee file Close Payroll file End Procedure ‘End McManusCOP100624

25 Scope! McManus COP1006 25

26  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. McManusCOP100626

27  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! McManusCOP100627

28  Declared within the main program  Can be referenced anywhere in the program ◦ Is visible and accessible everywhere McManusCOP100628 X, Y, Z A C B X, Y & Z are Global to modules A, B & C

29  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 McManusCOP100629 X, Y, Z A m C p B 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

30 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 McManusCOP100630 Scully & Mulder are what type of variables? What type of variable is Temp?

31 McManusCOP100631 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}

32 How we avoid side effects! McManus COP1006 32

33  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 McManusCOP100633

34  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. McManusCOP100634

35  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. McManusCOP100635

36  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 McManusCOP100636

37  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 McManusCOP100637

38  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 McManusCOP100638

39  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 McManusCOP100639

40 McManusCOP100640 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

41  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. McManusCOP100641

42  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 McManusCOP100642

43  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! McManusCOP100643

44 Call Parameters Actual Parameters  Valued  Variable McManusCOP100644 Procedure Header Parameters Formal Parameters Valued Variable Parameter Interface using Global Variables

45 Names & the Data Dictionary McManus COP1006 45

46  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 McManusCOP100646

47  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) McManusCOP100647

48 ItemName (no spaces) Data Type ModulesDomain (Range) Scope Hours worked HoursNumeric- real GetHours CalcGrossPay 0  n  168 Global Gross Pay Numeric- real CalcGrossPay CalcDeductions CalcNetPay PrintPayChecks 0  n  1 million Global Net Pay Numeric- real CalcNetPay PrintPayChecks 0  n  1 million Local Global McManusCOP100648

49  Sequential ◦ One statement follows another  Selection  Selection (Decision) ◦ Allows choices based on the data ◦ IfThenElse, Nested If’s, Case, Switch  Iteration  Iteration (Looping or Repetition) ◦ Allows statements to be repeated a specified number of times ◦ While, Do, For, Do Until, Repeat McManusCOP100649

50 McManusCOP100650 Next?


Download ppt "Lesson 4 McManus COP1006 1.  Pointers  Modules & Functions  Cohesion & Coupling  Local & Global Variables  Parameters  Variable Names & Data Dictionaries."

Similar presentations


Ads by Google