Chapter 3: Modularization

18
CHAPTER 3: MODULARIZATION

description

Chapter 3: Modularization. Objectives. Understanding Documentation Learn about documentation Learn about the advantages of modularization Learn how to modularize a program Declare local and global variables and constants Create hierarchy charts. Documentation. Documentation - PowerPoint PPT Presentation

Transcript of Chapter 3: Modularization

Page 1: Chapter  3: Modularization

CHAPTER 3:MODULARIZATION

Page 2: Chapter  3: Modularization

Objectives Understanding Documentation Learn about documentation Learn about the advantages of

modularization Learn how to modularize a program Declare local and global variables and

constants Create hierarchy charts

Page 3: Chapter  3: Modularization

Documentation

Documentation All supporting material that goes with a program Two major categories: for users and for

programmers User Documentation: End users (people

who use computer programs) Program Documentation

Internal program documentation: comments within code

External program documentation: supporting paperwork written before programming begins

Page 4: Chapter  3: Modularization

Output Documentation

Figure 3-2 Inventory records displayed in a GUI environment

Figure 3-3 Inventory records displayed in a running program

Page 5: Chapter  3: Modularization

Input Documentation

Describes what input is available to produce the output

File description: Describes data stored in a file Indicates fields, data types, and lengths

Figure 3-6 Inventory file description

Page 6: Chapter  3: Modularization

Completing Documentation

• Program documentation may contain:– Output design– Input description– Flowcharts– Pseudocode– Program code listing

• User documentation may contain:– Manuals– Instructional material– Operating instructions

Page 7: Chapter  3: Modularization

Modularization

Modularization: breaking a large program into modules

Advantages of modularization: Provides abstraction Allows multiple programmers to work

simultaneously Allows code reuse Makes identifying structures easier

Page 8: Chapter  3: Modularization

Modularization

Module: Unit of code that performs one small task Called a subroutine, procedure, function, or

method Invoke (call) a method is to execute it Calling method invokes the called

method Program contains unlimited number of

methods Each method can be called unlimited number

of times

Page 9: Chapter  3: Modularization

Modularization Provides Abstraction

Focuses on important properties while ignoring non-essential details

Avoids low-level details Makes complex tasks look simple High-level programming languages allow

English-like vocabulary One statement corresponds to dozens of

machine instructions Modules provide another way to achieve

abstraction

Page 10: Chapter  3: Modularization

Modularization Provides Abstraction (continued)

To-do list with abstraction

Do laundryCall Aunt NanStart term paper

To-do list without abstraction

Pick up laundry basketPut laundry basket in car

Drive to laundromatGet out of car with basket

Walk into laundromatSet basket down. . .

Page 11: Chapter  3: Modularization

Modularization Allows Multiple Programmers to Work on a Problem

Commercial programs rarely written by a single programmer

Development time is significantly reduced

Large programming projects can be divided into modules

Modules can be written by different programmers or programming teams

Page 12: Chapter  3: Modularization

Modularization Allows You to Reuse Your Work

Subroutines that are useful should be used more than once in a program Example: routine that checks the current date

Instructions placed in their own module are easy to port to other applications

Reusability: the ability to use modules in a variety of applications

Reliability: assurance that a module has been tested and proven to function correctly

Reliable software saves times and money

Page 13: Chapter  3: Modularization

Modularizing a Program

Most programs contain a main program Contains the mainline logic Accesses other modules or subroutines

Rules for naming modules different for every programming language For this text:

Must be one word Should be meaningful Followed by a set of parentheses Corresponds to module naming in Java, C++, C#

Page 14: Chapter  3: Modularization

Modularizing a Program (continued)

Table 3-1 Suggested identifiers for a module that calculates an employee’s gross pay

Page 15: Chapter  3: Modularization

Modularizing a Program (continued)

Calling program (or calling module): one that uses another module

Flowchart symbol for calling a module: rectangle with bar across the top

Flowchart for the module Start symbol: contains module name Stop symbol: contains exit or return

When a module is called, logic transfers to the model

When module ends, logic transfers back to the caller

Page 16: Chapter  3: Modularization

Figure 3-8 Sample logic Figure 3-9 Logic from Figure 3-8 using a method

Page 17: Chapter  3: Modularization

Figure 3-10 Logic from Figure 3-9 using two methods

Page 18: Chapter  3: Modularization

Modularizing a Program (continued)

Method is encapsulated in another method if it is contained in another method

Knowing when to break a module into its own subroutines or submodules is an art

Best practice: place together statements that contribute to one specific task

Functional cohesion: extent to which the statements contribute to the same task