Coding discipline
When to apply
- Writing or editing code in any language
- Code review or refactoring
- User asks about code style, clean code, or coding discipline
Principles
Avoid magic numbers and strings by extracting recurring or meaningful values into descriptive constants (const) or enums. Keep self-explanatory, one-off values inline to avoid clutter. If a value comes from a spec (e.g. HTTP 200 OK), use a constant regardless.
Use enums instead of booleans for function parameters.
Let the reader of the code breathe. Add empty lines between logical blocks of code.
Add a small, to the point, comment to explain what the block does and why. Use examples when possible. Propose ASCII drawings to explain complete systems.
Treat member visibility changes as a breaking design shift. Keep all fields and functions private unless external access is strictly required by the design. Prompt the user for explicit approval before changing any access modifier from private to internal or public.
Program to levels of abstraction. Lower-level mechanics (e.g., raw hardware I/O, sector parsing, direct socket streams) must be encapsulated in a dedicated driver/abstraction layer. Expose clean, high-level APIs to the rest of the application so calling code works with domain concepts, not raw implementation details.
Don't touch blocks of code unrelated to the feature you implement. e.g. Don't add comments to a block of code if you did not create it or modify it. As much as possible try to minimize the number of changed lines when implementing a feature.
Strictly adhere to the layered boundary hierarchy: each layer may only communicate with its immediate neighbor directly below it. Never "punch holes" through layers (e.g., controllers or UI components must never directly call database queries, raw hardware drivers, or low-level network clients; always route through the intermediate service/abstraction layer).
Always use {}, even on a one-line "if" statement.
If the prompt indicates that a bug is being fixed, don't write the fix right away. First write the test. Observe it failing. Then write the fix. And observe the test passing.
Workflows
Visibility gate
Before widening any access modifier, stop and ask the user:
⚠️ Visibility change requested
Current: private <member>
Proposed: internal|public <member>
Reason: <why external access is required>
Confirm before applying.
Do not apply the change until the user explicitly approves.
Bug-fix TDD
When the task is a bug fix:
- [ ] Write a test that reproduces the bug
- [ ] Run the test — confirm failure
- [ ] Implement the minimal fix
- [ ] Run the test — confirm pass
Layer boundary
Each layer talks only to its immediate neighbor below:
UI / CLI ──► Service / Use-case ──► Domain ──► Driver / IO
✗ never skip intermediate layer
Additional resources
- For good/bad code examples, see examples.md