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 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 Output Documentation Figure 3-2 Inventory records displayed in a GUI environment Figure 3-3 Inventory records displayed in a running program 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 Completing Documentation • • Program documentation may contain: – Output design – Input description – Flowcharts – Pseudocode – Program code listing User documentation may contain: – Manuals – Instructional material – Operating instructions 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 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 Modularization Provides Abstraction Focuses on important properties while ignoring nonessential 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 Modularization Provides Abstraction (continued) To-do list with abstraction Do laundry Call Aunt Nan Start term paper To-do list without abstraction Pick up laundry basket Put laundry basket in car Drive to laundromat Get out of car with basket Walk into laundromat Set basket down . . . 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 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 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# Modularizing a Program (continued) Table 3-1 Suggested identifiers for a module that calculates an employee’s gross pay 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 Figure 3-8 Sample logic Figure 3-9 Logic from Figure 3-8 using a method Figure 3-10 Logic from Figure 3-9 using two methods 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