Linux Kernel Coding Style (with MCU / RTOS Comparison)¶
Overview¶
This note summarizes practical coding conventions used in Linux kernel development, with comparison to common MCU / RTOS coding styles.
Focus areas:
- Naming conventions
- Structure design
- Function design
- Concurrency patterns
- Observability
- Real-world trade-offs
1. Philosophy Difference¶
Linux Kernel Style¶
- Emphasizes readability and semantics
- Code should read like natural language
- Avoid redundant information (type already exists)
- Designed for large-scale, multi-developer systems
MCU / RTOS Style¶
- Often emphasizes type clarity in naming
- Heavily influenced by hardware / register-level work
- Naming may encode:
- type
- scope
- usage
- More flexible / team-specific
Key Difference¶
2. Variable Naming¶
2.1 Boolean Variables¶
MCU / RTOS style¶
Linux style¶
Why?¶
- Type is already known (
bool) - Name should describe meaning, not type
- Cleaner in conditions:
2.2 Type Prefix (Hungarian-like)¶
MCU style¶
Linux style¶
Reason¶
- Type is already declared
- Prefix duplicates information
- Reduces readability in large codebases
2.3 Good vs Bad Naming¶
Bad (type-oriented)¶
Good (semantic)¶
2.4 Abbreviation Rules¶
Acceptable¶
Avoid excessive shortening¶
3. Structure Design¶
Linux Style¶
Group by functionality:
/* Device */
struct i2c_client *client;
/* Cached data */
bool cache_valid;
long temp_mc_cached;
/* Policy */
unsigned int min_update_interval_ms;
/* Statistics */
unsigned int irq_event_count;
/* Control */
bool polling_enable;
MCU Style¶
Often grouped by type or usage:
Why Linux groups by function¶
- Reflects system architecture
- Easier to debug
- Easier to extend
4. Function Naming¶
Linux style¶
myi2c_sensor_update_by_trigger_locked()
myi2c_sensor_should_update_locked()
myi2c_sensor_poll_work_callback()
Characteristics¶
- Module prefix
- Verb-based
- Context included (
_locked,_callback)
MCU style¶
Difference¶
Linux prefers:
5. Boolean Function Naming¶
Linux style¶
Avoid¶
6. Function Structure¶
Linux style¶
Use early return¶
Avoid deep nesting¶
MCU style (common)¶
7. Locking Design¶
Linux pattern¶
Rule¶
- Do NOT hold lock during scheduling
- Keep critical section minimal
MCU / RTOS¶
Often:
or
Difference¶
Linux separates:
- state protection
- execution control
8. Workqueue vs RTOS Task¶
Linux¶
- IRQ must be short
- workqueue handles heavy logic
RTOS¶
Similar concept, different API.
9. Error Handling¶
Linux¶
MCU¶
Linux 특징¶
- Use standard error codes
- Allow "no-op" return (0)
10. sysfs Interface Style¶
Linux¶
snake_case + descriptive
show/store pattern¶
11. Observability (Very Important)¶
Linux style¶
Explicit metrics:
MCU style¶
Often missing or minimal logging
Why Linux emphasizes this¶
- Complex systems
- Need runtime introspection
- Debugging without JTAG
12. Policy Separation¶
Linux¶
Separate:
Bad (mixed logic)¶
13. Readability vs Cleverness¶
Avoid¶
Prefer¶
14. Units in Naming¶
Linux strongly encourages:
MCU style¶
Sometimes missing units:
15. When MCU Style is Still Useful¶
Linux style is not always better.
MCU advantages¶
- Register-level programming
- Packed structures
- Protocol handling
Example:
16. Practical Guidelines (For This Project)¶
Follow Linux style for:¶
- driver code
- sysfs
- workqueue
- policy logic
Keep MCU habits for:¶
- hardware registers
- protocol fields
- fixed-width data
Key Takeaways¶
- Naming should express meaning, not type
- Structure reflects system architecture
- Separate policy from execution
- Design for observability
- Prefer simple and readable code