Designing Modular Programs
Purpose
A modular approach to programming separates the code into manageable chunks. Functions are one way to achieve this. A function should be designed to do a single task. Objects are introduced, enabling a reduction in the interaction of the program, reducing some of the possibilities of bugs.
Why modules?
- Each module can be coded independently. Small blocks of code are easier to write, test and maintain.
- Proper documentation of what each code block does is encouraged because modules have to communicate effectively.
- Changing one module should not affect other modules. Removal of bugs and maintenance is confined to individual modules.
- Design and coding can be shared by a team.
Before any design can be commenced it is necessary to know what the input and output data is and what the relationship is between them. A further factor is a need to know how the operator is to interact with the system.
A modular solution to a problem should allow for the details of the implementation of each sub task to be modified without affecting the remaining sub tasks. The way in which different parts of the design communicate has to be carefully specified. This communication must remain unchanged if and when implementation changes are made.
Designing and testing of the processing code can be developed separately from the user interface.
Objects
Combining data items with functions produces a data type structure known as objects. These can be regarded as active. Such active objects respond to requests to do something. The functions incorporated into the objects with the data items are called methods. Data tables will show the data items and their methods. These will make up a class definition.
| Type | Identifier | Description |
| BinType | Bins[6][10] | Array of storage bins |
| String | Store (InCode, InQuantity) | Stores InQuantity of product InCode into Bins. Returns string giving location of bin used or error message if no bin available. |
| String | Supply (OutCode, OutQuantity) | Supplies OutQuantity of product OutCode from Bins. Returns string giving location(s) of bin(s) used and/or error message if order partly supplied |
A variable of class WarehouseType defined above will be an object of that type.
Comments, suggestions, ideas to
Stuart Banner
