
Modular Programming, Reusable Logic, Error Handling & Logging
Learning objectives
- Structure code into reusable modules.
- Write logic that is reusable across rooms.
- Implement robust error handling.
- Add logging for serviceability.
Vocabulary
- Modular programming
- Splitting code into independent, reusable modules.
- Reusable logic
- Logic written once and reused across rooms/devices.
- Error handling
- Code that detects and responds to failures gracefully.
- Logging
- Recording events and errors for service and debugging.
Lesson content
As rooms get complex, code must be maintainable. Modular, reusable, well-handled, logged code serves the next technician — and you, six months later.
Structure
- Put device drivers in their own modules.
- Put room logic (start, source, volume, shutdown) in a room module.
- Keep UI binding separate from logic.
- Write generic helpers (timers, retry) once and reuse them.
Reusable logic
// generic, reusable
function withRetry(send, expect, attempts, timeout) {
for (i in 1..attempts) {
send()
if (waitFor(expect, timeout)) return OK
}
return TIMEOUT
}Error handling
- 1Catch failures at each device interaction.
- 2Surface a clear user-facing state (error message + Retry).
- 3Never let one device failure hang the whole room.
- 4Fail safe: mute audio rather than blast it on an unknown state.
Logging
- Log commands sent, responses, timeouts, and state transitions.
- Keep logs time-stamped and retained for service.
- Use logs to diagnose field issues without reproducing them.
The person who services this room in a year may be you. Logs and clear module boundaries turn a three-hour mystery into a ten-minute read.
Worked example
A display fails to power on. The driver retries once, logs 'display PON timeout after 2 attempts,' sets displayPower = ERROR, and the panel shows 'Display not responding — Retry / Call support.' The rest of the room stays usable; the log shows exactly what happened.
Implementation checklist
- ✓Drivers and room logic in separate modules.
- ✓Generic helpers reused, not duplicated.
- ✓Errors caught and surfaced; no device hangs the room.
- ✓Time-stamped logs of commands, responses, timeouts.
Downloadable study guide
01. Modular: drivers, room logic, UI binding separate; reusable helpers. 02. Error handling: catch per device, surface user-facing state, fail safe. 03. Logging: commands, responses, timeouts, state transitions, time-stamped. 04. Logs turn mysteries into ten-minute reads.