Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming Logic and Design Fourth Edition, Comprehensive

Similar presentations


Presentation on theme: "Programming Logic and Design Fourth Edition, Comprehensive"— Presentation transcript:

1 Programming Logic and Design Fourth Edition, Comprehensive
Chapter 12 Advanced Modularization Techniques

2 Objectives Understand local and global variables and encapsulation
Pass a single value to a module Pass multiple values to a module Return a value from a module Use prewritten, built-in modules Programming Logic and Design, Fourth Edition, Comprehensive

3 Objectives (continued)
Create an IPO chart Understand the advantages of encapsulation Reduce coupling and increase cohesion in your modules Programming Logic and Design, Fourth Edition, Comprehensive

4 Understanding Local and Global Variables and Encapsulation
Procedural program: consists of a series of steps or procedures that are executed one after another Modularization: breaking down programs into reasonable units called modules, subroutines, functions, or methods Benefits of modularization: Abstraction: easier to see the “big picture” Multiple programmers can work on a program Modules are reusable Structures can be identified more easily Programming Logic and Design, Fourth Edition, Comprehensive

5 Understanding Local and Global Variables and Encapsulation (continued)
Global variable: available to every module Local variable: Name and value are known only to its own module Declared within a module Ceases to exist when the module ends In scope: a variable during the period that it is existing and usable Out of scope: a variable that no longer exists Programming Logic and Design, Fourth Edition, Comprehensive

6 Understanding Local and Global Variables and Encapsulation (continued)
Benefits of using local variables: Other programmers do not need to know the names of the variables Each module is a complete unit, containing all needed variables Allows modules and variable names to be reused Programming Logic and Design, Fourth Edition, Comprehensive

7 Understanding Local and Global Variables and Encapsulation (continued)
Programming Logic and Design, Fourth Edition, Comprehensive

8 Understanding Local and Global Variables and Encapsulation (continued)
Programming Logic and Design, Fourth Edition, Comprehensive

9 Understanding Local and Global Variables and Encapsulation (continued)
Programming Logic and Design, Fourth Edition, Comprehensive

10 Understanding Local and Global Variables and Encapsulation (continued)
Programming Logic and Design, Fourth Edition, Comprehensive

11 Understanding Local and Global Variables and Encapsulation (continued)
Encapsulation: all program components are bundled together Information hiding (or data hiding): data or variables are contained within and accessible only to the module in which they are declared Advantages of encapsulation: Modules are self contained and reusable Multiple programmers can work on the project Variable names can be reused in other modules Programming Logic and Design, Fourth Edition, Comprehensive

12 Understanding Local and Global Variables and Encapsulation (continued)
Programming Logic and Design, Fourth Edition, Comprehensive

13 Passing a Single Value to a Module
Sometimes necessary for one module to provide a value to another module Developing the application: askQuestion() module currently produces output based on presenting five questions to the user Programming Logic and Design, Fourth Edition, Comprehensive

14 Passing a Single Value to a Module (continued)
Programming Logic and Design, Fourth Edition, Comprehensive

15 Passing a Single Value to a Module (continued)
Programming Logic and Design, Fourth Edition, Comprehensive

16 Passing a Single Value to a Module (continued)
Adding capabilities to the application: Print count of correct answers Print percentage of correct answers Print a statement about user’s performance Must pass the count of correct answers from the askQuestion() module to the new finalStatistics() module Pass a value: send a copy of the data in one module to another module Programming Logic and Design, Fourth Edition, Comprehensive

17 Passing a Single Value to a Module (continued)
Programming Logic and Design, Fourth Edition, Comprehensive

18 Passing a Single Value to a Module (continued)
Programming Logic and Design, Fourth Edition, Comprehensive

19 Passing a Single Value to a Module (continued)
Programming Logic and Design, Fourth Edition, Comprehensive

20 Passing a Single Value to a Module (continued)
Module header: introductory title statement Must declare a special variable within the module header to receive a copy of the correctCount value Parameter: variable declared in a module header to receive data passed in from another module askQuestion() module passes a copy of correctCount value to the numRight parameter in finalStatistics() Programming Logic and Design, Fourth Edition, Comprehensive

21 Passing a Single Value to a Module (continued)
Programming Logic and Design, Fourth Edition, Comprehensive

22 Passing Multiple Values to a Module
Developing the application: finalStatistics() module currently assumes there are exactly five questions Need another parameter to represent the number of questions Parameters are separated by commas in the header Each parameter: Has a data type and an identifier Follows standard naming rules Order in which the parameters appear is important: Module that passes values must do so in correct order Programming Logic and Design, Fourth Edition, Comprehensive

23 Passing Multiple Values to a Module (continued)
Programming Logic and Design, Fourth Edition, Comprehensive

24 Passing Multiple Values to a Module (continued)
Programming Logic and Design, Fourth Edition, Comprehensive

25 Passing Multiple Values to a Module (continued)
Can call a module from other modules in various ways: Use variables for parameter values Use constant for parameter values Programming Logic and Design, Fourth Edition, Comprehensive

26 Passing Multiple Values to a Module (continued)
Programming Logic and Design, Fourth Edition, Comprehensive

27 Returning a Value from a Module
Return a value: pass back a value from the called module to the caller Developing the application: finalStatistics() module passes back a value to the giveQuiz() module Programming Logic and Design, Fourth Edition, Comprehensive

28 Returning a Value from a Module (continued)
Programming Logic and Design, Fourth Edition, Comprehensive

29 Returning a Value from a Module (continued)
Developing the application: giveQuiz() module calls finalStatistics() module, passing in userRight and numQuestions values finalStatistics() module passes back pctCorrect value to giveQuiz() module Module header must include type of value to return Return type: data type of the value to be returned Programming Logic and Design, Fourth Edition, Comprehensive

30 Returning a Value from a Module (continued)
Programming Logic and Design, Fourth Edition, Comprehensive

31 Returning a Value from a Module (continued)
Programming Logic and Design, Fourth Edition, Comprehensive

32 Using Prewritten, Built-in Modules
Built-in methods (or built-in functions): prewritten modules that perform frequently needed tasks Commonly available built-in methods include mathematical functions Use language documentation to determine what built-in methods are available To use a built-in method, you need: Name of the method Arguments that must be passed to the method Type of return value produced (if any) Programming Logic and Design, Fourth Edition, Comprehensive

33 Using Prewritten, Built-in Modules (continued)
Black box method: method you can use without understanding its internal processes Built-in methods function as black box methods There may be multiple versions of a built-in method: Version to be used depends on arguments passed in Programmer neither knows nor cares which one is used Absolute value: the positive value of a number Programming Logic and Design, Fourth Edition, Comprehensive

34 Using Prewritten, Built-in Modules (continued)
Programming Logic and Design, Fourth Edition, Comprehensive

35 Using an IPO Chart IPO chart:
Identifies and categorizes each item within a module as pertaining to input, processing, or output Helps plan program logic Programming Logic and Design, Fourth Edition, Comprehensive

36 Understanding the Advantages of Encapsulation
If all variables are global: All programmers on the project must understand how each variable is used Variable names cannot be reused Reusing the module elsewhere will require that the variables be defined correctly in the new program Using local variables: Procedures become miniprograms that are relatively autonomous Details are hidden and contained (encapsulated) Modules are easily reusable Programming Logic and Design, Fourth Edition, Comprehensive

37 Understanding the Advantages of Encapsulation (continued)
Reliability: a feature of programs or modules that have been tested and proven to work correctly Software that is reusable: Saves time and money Is more reliable Ability to pass values to modules: Allows programmers to create variable names locally Makes programming more flexible Programming Logic and Design, Fourth Edition, Comprehensive

38 Understanding the Advantages of Encapsulation (continued)
Limitations: Name of module cannot be used elsewhere in the program Must know what type of data to pass to a module Programming Logic and Design, Fourth Edition, Comprehensive

39 Reducing Coupling and Increasing Cohesion
Deciding how to break up a program into modules can be difficult Placing too many or too few instructions in a single module: Decreases code readability Reduces reliability Two rules for organizing program into modules Reduce coupling Increase cohesion Programming Logic and Design, Fourth Edition, Comprehensive

40 Reducing Coupling Coupling: Tight coupling:
Measure of the strength of the connection between two program modules Expresses the extent to which information is exchanged by subroutines Tight coupling: When modules excessively depend on each other Makes programs more prone to errors Many data paths to keep track of Many chances for bad data to be passed Many chances that one module alters data needed by another Programming Logic and Design, Fourth Edition, Comprehensive

41 Reducing Coupling (continued)
Loose coupling: Occurs when modules do not depend on other modules Reduce coupling as much as possible to improve: Ease of writing the program Program maintainability Module reuse Evaluating the amount of coupling: Look at the intimacy between modules and the number of parameters passed between them Programming Logic and Design, Fourth Edition, Comprehensive

42 Reducing Coupling (continued)
Tight coupling: Least intimate situation: modules have access to the same globally defined variables Loose coupling: Most intimate way to share data is to pass a copy of variables between modules Loosest (best) subroutines pass single arguments only Programming Logic and Design, Fourth Edition, Comprehensive

43 Reducing Coupling (continued)
Data coupling: Loosest type of coupling Occurs when modules share a data item by passing arguments Also called simple data coupling or normal coupling Data-structured coupling: Entire record is passed between modules Programming Logic and Design, Fourth Edition, Comprehensive

44 Reducing Coupling (continued)
Passing an entire record between modules: Programming Logic and Design, Fourth Edition, Comprehensive

45 Reducing Coupling (continued)
Control coupling: Occurs when the argument passed to a module controls the module’s actions Can be appropriate, but may be tightly coupled External coupling: Occurs when two or more modules access the same global variable or record Pathological coupling: Occurs when two or more modules change one another’s data Avoid this at all costs! Programming Logic and Design, Fourth Edition, Comprehensive

46 Reducing Coupling (continued)
Control coupling example: Any module calling selectMethod() must know how it works If changes are made to selectMethod(), changes will be needed to modules that call it Programming Logic and Design, Fourth Edition, Comprehensive

47 Increasing Cohesion Cohesion:
Measure of how much a module’s statements serve to accomplish the module’s purpose Highly cohesive modules are usually more reliable Functional cohesion: Occurs when all operations in a module contribute to the performance of a single task Highest level of cohesion Programming Logic and Design, Fourth Edition, Comprehensive

48 Increasing Cohesion (continued)
A module with functional cohesion: Programming Logic and Design, Fourth Edition, Comprehensive

49 Increasing Cohesion (continued)
Other types of cohesion are considered inferior to functional cohesion Sequential cohesion: When a module’s operations must be carried out in a specific order, using the same data Communicational cohesion: Modules that perform tasks that share data Only the data items are related; tasks are not Programming Logic and Design, Fourth Edition, Comprehensive

50 Increasing Cohesion (continued)
Temporal cohesion: Module tasks are related by when they take place Procedural cohesion: When module tasks must take place in sequence, but do not share data Main procedure modules are often procedurally cohesive Programming Logic and Design, Fourth Edition, Comprehensive

51 Increasing Cohesion (continued)
Dispatcher module: Another name for a mainline logic module Should only call other modules to perform tasks Logical cohesion: When a member module performs tasks based on a logical decision Coincidental cohesion: Based on coincidence Operations just happen to be placed together Indicates poor design! Programming Logic and Design, Fourth Edition, Comprehensive

52 Summary Procedural program: a series of steps or procedures that take place sequentially Modularizing a program provides abstraction, allows multiple programmers, allows reuse, and allows structures to be identified more easily Pass one or more variables to modules to share data between modules Modules can return data to the caller Built-in modules are prewritten modules for common tasks Programming Logic and Design, Fourth Edition, Comprehensive

53 Summary (continued) IPO chart: identifies and categorizes each item pertaining to input, processing, or output Passing variables to modules allows the use of local variables Local variables facilitate encapsulation Strive to achieve loose coupling and high cohesion Programming Logic and Design, Fourth Edition, Comprehensive


Download ppt "Programming Logic and Design Fourth Edition, Comprehensive"

Similar presentations


Ads by Google