Code Documentation for Traceability
Purpose
Provide conventions for code comments that support traceability to requirements, risks, and tests, while maintaining clarity and brevity.
When to Apply
- Any safety-related code, interfaces, or tests.
- When implementing or modifying requirements or risk controls.
Requirements (testable)
- Traceability Tags: Use stable IDs (e.g.,
REQ-###, RISK-CTRL-###, TEST-###) in comments near implementing code. Rationale: link code to artifacts.
- Minimal Necessary Comments: Explain intent, constraints, and safety context; avoid restating obvious code. Rationale: clarity without noise.
- API Documentation: Public interfaces documented with purpose, parameters, ranges, error codes, and side effects. Rationale: correct usage.
- Change History: Avoid inline history; rely on VCS; only note rationale when non-obvious decisions impact safety. Rationale: reduce clutter.
- Generated Docs: Use Doxygen/Javadoc-style only where toolchains consume it; keep tags consistent. Rationale: consistency.
Recommended Practices
- Co-locate traceability tags with logic they cover.
- Note assumptions and safety limits (ranges, timing).
- Keep comments in ASCII; avoid ambiguous abbreviations.
- Remove stale comments during refactors.
Patterns
Traceability on requirement and risk control:
// REQ-221: stop pump within 50 ms on door open; RISK-CTRL-19; TEST-410
bool pump_stop_on_door_open(void) {
...
}
API doc example:
/**
* @brief Set infusion rate.
* @param ml_per_hr range [0, MAX_FLOW]; clamps and alarms if exceeded.
* @return 0 on success, -ERANGE if clamped.
* REQ-300, RISK-CTRL-27
*/
int set_flow(float ml_per_hr);
Anti-Patterns (risks)
- No traceability tags -> risk: lost linkage in audits/impact analysis.
- Commenting the obvious or mismatched comments -> risk: clutter/confusion.
- Inline change logs instead of VCS -> risk: divergence.
- Missing parameter ranges -> risk: misuse of APIs.
Verification Checklist
Traceability
- Tags
REQ-###, RISK-CTRL-###, TEST-### aligned with external artifacts; code review enforces presence on safety-relevant sections.
References
- IEC 62304 traceability expectations.
- ISO 14971 linkage of controls to implementation.
Changelog
- 1.0.0 (2026-01-04): Initial code comment/traceability guidance.
Audit History
- 2026-01-04: Audit performed. Verified:
- IEC 62304 traceability expectations correctly referenced
- ISO 14971 linkage requirements accurate
1---2name: code-comments3description: Code Documentation for Traceability4---56# Code Documentation for Traceability78## Purpose9Provide conventions for code comments that support traceability to requirements, risks, and tests, while maintaining clarity and brevity.1011## When to Apply12- Any safety-related code, interfaces, or tests.13- When implementing or modifying requirements or risk controls.1415## Requirements (testable)161. Traceability Tags: Use stable IDs (e.g., `REQ-###`, `RISK-CTRL-###`, `TEST-###`) in comments near implementing code. Rationale: link code to artifacts.172. Minimal Necessary Comments: Explain intent, constraints, and safety context; avoid restating obvious code. Rationale: clarity without noise.183. API Documentation: Public interfaces documented with purpose, parameters, ranges, error codes, and side effects. Rationale: correct usage.194. Change History: Avoid inline history; rely on VCS; only note rationale when non-obvious decisions impact safety. Rationale: reduce clutter.205. Generated Docs: Use Doxygen/Javadoc-style only where toolchains consume it; keep tags consistent. Rationale: consistency.2122## Recommended Practices23- Co-locate traceability tags with logic they cover.24- Note assumptions and safety limits (ranges, timing).25- Keep comments in ASCII; avoid ambiguous abbreviations.26- Remove stale comments during refactors.2728## Patterns29Traceability on requirement and risk control:30```c31// REQ-221: stop pump within 50 ms on door open; RISK-CTRL-19; TEST-41032bool pump_stop_on_door_open(void) {33 ...34}35```3637API doc example:38```c39/**40 * @brief Set infusion rate.41 * @param ml_per_hr range [0, MAX_FLOW]; clamps and alarms if exceeded.42 * @return 0 on success, -ERANGE if clamped.43 * REQ-300, RISK-CTRL-2744 */45int set_flow(float ml_per_hr);46```4748## Anti-Patterns (risks)49- No traceability tags -> risk: lost linkage in audits/impact analysis.50- Commenting the obvious or mismatched comments -> risk: clutter/confusion.51- Inline change logs instead of VCS -> risk: divergence.52- Missing parameter ranges -> risk: misuse of APIs.5354## Verification Checklist55- [ ] Requirements/risk/test IDs present where applicable.56- [ ] Comments capture intent/constraints, not restating code.57- [ ] Public APIs documented with params, ranges, errors.58- [ ] Stale/misleading comments removed.59- [ ] Documentation style consistent (Doxygen/Javadoc if used).6061## Traceability62- Tags `REQ-###`, `RISK-CTRL-###`, `TEST-###` aligned with external artifacts; code review enforces presence on safety-relevant sections.6364## References65- IEC 62304 traceability expectations.66- ISO 14971 linkage of controls to implementation.6768## Changelog69- 1.0.0 (2026-01-04): Initial code comment/traceability guidance.7071## Audit History72- **2026-01-04**: Audit performed. Verified:73 - IEC 62304 traceability expectations correctly referenced74 - ISO 14971 linkage requirements accurate