Engineering Modular Programming: Reliable Module Boundaries

Engineering modular programming organizes a program into parts that you can understand, test, and change with confidence. In an engineering tool, reading measurements, calculating a physical quantity, and presenting results involve different decisions. Separating these responsibilities helps you check the numerical model without also navigating file formats and display settings. The important design choice is what each part promises to do and what information it exchanges.

Give each module a focused responsibility

A module is a program unit that groups a related responsibility behind a defined interface. Depending on the language and project, it may be implemented as a file, package, or collection of related routines. A module can contain several functions; modular programming concerns the boundaries between responsibilities rather than the syntax of individual function calls.


For a measurement-processing tool, useful responsibilities include:

  • An input adapter decodes recorded values, converts units, and rejects invalid records.
  • A calculation module evaluates the engineering model using agreed input units.
  • A reporting module formats calculated results for people or other software.

A small driver coordinates these modules and passes results between them.


A driver coordinates input adaptation, calculation, and reporting through explicit data handoffs.


Keep closely related operations together. Unit conversion and interpretation of a recorder's fields belong naturally in its adapter. By contrast, changing a report's decimal places should not require editing the physical calculation. Splitting code into many files does little good if every file still depends on the others' internal details.



Make interfaces carry meaning

An interface contract states what a module accepts, what it returns, and how it behaves when its requirements are not met. For engineering calculations, variable names and data types are insufficient: units, valid ranges, assumptions, and failure behaviour also matter.


Consider electrical energy over an interval with constant voltage and current:


E=VIΔtE = VI\Delta t


Here, VV is voltage in volts, II is current in amperes, Δt\Delta t is elapsed time in seconds, and EE is energy in joules. A calculation module using this model should document these units and the constant-value assumption. Its contract should also state whether it rejects negative durations and how it reports invalid inputs.


The caller should not need to know how the module stores intermediate values. Pass required data explicitly and return results explicitly where practical. Hidden dependence on shared mutable variables makes a calculation harder to reproduce because its behaviour can depend on earlier operations.



Test modules and their connections

A module test checks one responsibility against its contract. For the energy calculation, choose inputs with an independently calculated expected result. Also test relevant boundaries, such as zero elapsed time, and the documented response to invalid data.


An integration test checks that connected modules agree. A calculation can pass its own tests while the complete program fails because the adapter supplies milliseconds where seconds are expected. Check the quantities at the handoff, including their units, rather than checking only that the program completes.


Proposed change

Contract consequence

Checks to prioritize

Recorder changes its storage format

Adapter can preserve its existing output contract

Adapter and integration tests

Report uses more decimal places

Calculation contract can remain unchanged

Reporting tests

Calculation starts returning kilojoules instead of joules

Output contract changes

Every affected caller and integration test


Preserving an interface allows you to replace an implementation without requiring callers to understand the replacement. Preservation includes numerical meaning and failure behaviour, not just matching names and argument counts. Tests provide evidence for the cases they cover; passing a few examples does not establish correctness for every permitted input.

What to remember

  • Group related work into modules with focused responsibilities.
  • Document units, assumptions, valid inputs, outputs, and failures at each interface.
  • Keep required data explicit so calculations are reproducible.
  • Test individual modules and the handoffs between them.
  • Treat changes to numerical meaning as interface changes.

Example: A Modular Discharge-Test Tool

A laboratory uses engineering modular programming to process a constant-voltage, constant-current battery discharge test. The recorder supplies 24.0 V24.0\ \text{V}, 1800 mA1800\ \text{mA}, and an elapsed time of 75000 ms75000\ \text{ms}. Use the model E=VIΔtE=VI\Delta t.


The tool has an input adapter, an energy-calculation module, and a reporting module. It accepts finite values with voltage greater than zero, current greater than or equal to zero, and elapsed time greater than or equal to zero. Invalid records must produce an error and stop processing. The final report displays energy in kilojoules to three decimal places.


A recorder supplies values to an input adapter, an energy module, and a reporting module in sequence.


a) Specify the input and output contract for each module, including where unit conversion and invalid-record handling belong.


Make each handoff explicit so that downstream modules do not need to understand the recorder's storage format. The adapter converts external measurements into the SI quantities required by the calculation.

  • Input adapter: Accept recorder fields in V, mA, and ms. Validate the measurements and return values in V, A, and s, or an error if the record is invalid.
  • Energy calculation: Accept finite V>0V>0, I≥0I\geq0, and Δt≥0\Delta t\geq0 in V, A, and s. Return E=VIΔtE=VI\Delta t in J under the constant-value model.
  • Reporting: Accept a successful energy result in J. Convert it to kJ and display it rounded to three decimal places.

The driver stops if the adapter reports an invalid record. It does not pass an error marker to the calculation as though it were a measurement. The calculation module documents its input requirements and does not perform recorder-specific conversions. Reporting handles the final J-to-kJ conversion and display precision.


b) Trace the supplied record through the modules and calculate the displayed energy.


The adapter preserves the voltage and converts the current and elapsed time to the units promised at its output:


V=24.0 V,I=1800 mA(1 A1000 mA)=1.800 A,Δt=75000 ms(1 s1000 ms)=75.000 s.\begin{aligned} V&=24.0\ \text{V},\\ I&=1800\ \text{mA}\left(\frac{1\ \text{A}}{1000\ \text{mA}}\right)=1.800\ \text{A},\\ \Delta t&=75000\ \text{ms}\left(\frac{1\ \text{s}}{1000\ \text{ms}}\right)=75.000\ \text{s}. \end{aligned}


The calculation module receives these SI values. First identify the constant electrical power, then multiply by the elapsed time:


P=VI=(24.0 V)(1.800 A)=43.2 W,E=PΔt=(43.2 W)(75.000 s)=3240 J.\begin{aligned} P&=VI=(24.0\ \text{V})(1.800\ \text{A})=43.2\ \text{W},\\ E&=P\Delta t=(43.2\ \text{W})(75.000\ \text{s})=3240\ \text{J}. \end{aligned}


Reporting converts the returned energy and applies the requested display precision:


E=3240 J(1 kJ1000 J)=3.240 kJ.E=3240\ \text{J}\left(\frac{1\ \text{kJ}}{1000\ \text{J}}\right)=3.240\ \text{kJ}.


The displayed result is 3.240 kJ3.240\ \text{kJ}. The three decimal places are a display requirement; they do not establish additional measurement accuracy. Keep the intermediate values unrounded until reporting.


c) A replacement recorder supplies the same physical measurements directly in volts, amperes, and seconds. Explain which module implementation needs updating and identify regression tests that protect the existing behaviour.


Update the recorder adapter to read the replacement format. Its output contract remains ________, so the energy and reporting modules can retain their implementations. The driver selects the replacement adapter while continuing to pass the same kinds of quantities downstream.


For the equivalent new record, the adapter should return 24.0 V24.0\ \text{V}, 1.800 A1.800\ \text{A}, and 75.000 s75.000\ \text{s} without dividing the current or time by ________ again. Test this handoff explicitly.


Retain a direct calculation test expecting 3240 J3240\ \text{J} and a reporting test expecting 3.240 kJ3.240\ \text{kJ}. Add an integration test requiring the replacement record to produce that same display. These checks detect accidental double conversion as well as changes downstream.


Also test a zero-duration record, which should produce 0 J0\ \text{J} and display 0.000 kJ0.000\ \text{kJ}. A negative-duration record should produce the specified error and stop before calculation or reporting. Preserving the interface includes preserving this failure behaviour.

An engineering laboratory program reads temperature records in degrees Celsius, calculates their arithmetic mean, and writes a formatted summary. The recorder format and summary layout change independently. Which module arrangement best supports focused responsibilities and independent testing?

Choose an answer

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

An engineering team is documenting a modular program that analyses laboratory measurements. Match each design term to its meaning when describing how the program's modules are organized and connected.

Match each term
A.

The degree to which responsibilities within one module belong together

B.

The agreed inputs, outputs, and usage rules at a module boundary

C.

Keeping implementation details behind a controlled boundary

D.

The degree of dependence between separate modules

Encapsulation

Interface

Coupling

Cohesion

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

A power-calculation module accepts voltage in volts and current in amperes, then returns P=VIP=VI in watts. Other callers already supply amperes correctly. A recorder provides a voltage of 12.0 V12.0\ \text{V} and a current of 650 mA650\ \text{mA}.


Which proposal correctly connects this recorder to the existing module while preserving its documented behaviour for all callers?

Choose an answer

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

Extra Practice