Progress Log: Protocol Function Naming Convention (v1.8.6)¶
Task Description¶
Implement a consistent naming pattern for all protocol functions in the firmware to improve code readability, maintainability, and developer velocity when implementing new protocols. The naming convention follows the pattern [protocol]_[stage]() where:
[protocol]: The protocol identifier (binaryortext)[stage]: The processing pipeline stage (receive, queue, queued, dequeue, validate, execute, response_ok, response_error)
The standardization addressed inconsistencies in the codebase where function names were unclear about protocol type and processing stage.
Outcome¶
Successfully implemented and documented the [protocol]_[stage]() naming convention across all protocol functions:
Binary Protocol (binary_*)¶
binary_receive()- Read binary command from serial, validate structure, queue itbinary_queue()- Add binary command to queuebinary_queued()- Check if binary commands pendingbinary_dequeue()- Remove next binary command from queuebinary_execute()- Dequeue and execute pending binary commandbinary_validate()- Validate binary command payload and execute if validbinary_response_ok(channel, data1, data2)- Send success response (JSONL format)binary_response_error(error_msg, error_code)- Send error response (JSONL format)
Text Protocol (text_*)¶
text_receive()- Read text command line from serial, parse it, queue ittext_queue()- Add text command to queuetext_queued()- Check if text commands pendingtext_dequeue()- Remove next text command from queue and executetext_execute()- Dequeue and execute pending text commandtext_parse()- Parse text command line into structured commandtext_dispatch()- Validate and dispatch parsed command to handlertext_response_ok(message)- Send success response (JSONL format)text_response_error(command, error_msg, error_code)- Send error response (JSONL format)
Shared Utilities¶
serial_init(baudRate)- Initialize serial communication with specified baud rateserial_discard()- Discard remaining bytes from serial input buffer (safe with size limit)
Improvements Applied¶
- ✅ Removed unused wrapper functions to reduce API surface
- ✅ Renamed
serial_flush()→serial_discard()for clarity (input buffer cleanup vs output flush) - ✅ Moved text protocol queue management to
text_protocol.cppfor architectural symmetry - ✅ All functions now use
[protocol]_[stage]()pattern consistently - ✅ Shared utilities clearly prefixed with
serial_(not protocol-specific)
Learnings¶
Benefits Realized¶
- Self-Documenting Code: Function names immediately communicate what protocol is being used and where in the pipeline the function operates
- Discoverability: Developers can find related functions by searching for common prefixes (
binary_*,text_*,*_queue,*_response_*) - Consistency Across Protocols: New protocol implementations automatically follow the same pattern, reducing cognitive load and training time
- Pipeline Clarity: Reading code from top to bottom shows the logical flow of command processing
Historical Context¶
Before v1.8.6, the codebase had inconsistent function names:
serial_read_command()vsserial_read_text_command()process_binary_commands()vsprocess_text_commands()send_binary_dac_response()vssend_ok_response()serial_available()(unused wrapper)serial_send_response()(unused wrapper)serial_send_text_response()(unused wrapper)serial_flush()(confusing name, unclear purpose vsSerial.flush())
These inconsistencies made it difficult to:
- Understand which protocol a function handled
- Find related functions
- Follow the processing pipeline
- Distinguish between Arduino API (
Serial.flush()) and local utilities
Next Steps¶
- Monitor adoption of the naming convention in new protocol implementations
- Document additional protocols using the same
[protocol]_[stage]()pattern - Consider adding linting rules to enforce naming convention in CI/CD pipeline
- Update code review guidelines to reference the protocol naming convention document