Wednesday, March 24, 2010

Modular programming in C




What is Modular programming ?

- A programming technique to break down program functions into separate modules/parts/layers.
- Module, have to accomplishes one function by containing the source codes and input/output variables needed to accomplish that function.

e.g:
Project | components | layers | tasks | functions | module(s) = interface header (.h) + implementation code (.c)

e.g: LED or UART: only read/write registers/addresses functions

Why use modular programming ?

- Code easier to understand – error easier to find and test root cause.
- Grouping the scope of variables/functions/layers/files. Control effectively.
- Program easier to design.
- Team-working sharing to develop in parallel.
- Re-using module.
Structured of modular program:
- Main idea is “divide and conquer” algorithm.

- In same layer, modules can invoke each others.
- Make library to re-use in future.
Functions:
Definition:

- List of functions and data import/export as well as declarations of them.
Implementation:
- Its dependencies of other modules.
- Importing resources from other modules to use.
- Its exporting resources for other modules use.
- Implement what it will do.

Reference input/output parameters:

Validation parameters.
Protection parameters.

Syntax & arguments:
Using variables.
Using pointer reference.

Design:

- Building blocks.
- Start with small and add more.
- Module designed toward make more independent module. (coupling or cohesion)

Circle:
- Analysize.
- Design + Usage.
- Implement.
- Test.
- Maintain.

Layer:
Separate project to layers for easier controlled (changes, updates)


HIGHEST: Each layer can only use/call modules in its layer or lower layer
MIDDLE: As a bridge (APIs) to link between highest – lowest layers.
Dump useful information, usage
LOWEST: List of components can be accessed to hardware.

Each layer can only use/call modules in its layer or lower layer.
One layer can be replaced without affecting other layers.

Rules:
A module may make simple call to other modules in same layer.

A module may call a lower-layer module only use gate (bridge APIs)
If not using gate, a module can not access directly resource in other layers.
A module may not call a higher-level routine.
A module may not modify the vector address of another level’s handler(s).
A module may be not call farther than one layer (optional).


Tags: Modular programming, Modular, programming, technique, break down, functions, modules, parts, layers, variables, interface header, implementation code, divide and conquer.

No comments:

Post a Comment

Labels