This document outlines code quality standards and development practices for the Patience project.
This project uses automated quality gates to prevent regression of improvements made during TDD development cycles.
The CI/CD pipeline enforces these quality standards:
- Maximum function length: 120 lines
- Maximum cyclomatic complexity: 15
- Rationale: Prevents regression of the 216-line
Executor.Runmethod that was refactored for maintainability
- Required: All numeric constants must be defined in
pkg/executor/constants.go - Detection: Automated checks ensure constants file exists and is used
- Rationale: Prevents hardcoded timeout values and improves maintainability
- Required: Proper error propagation patterns throughout the codebase
- Minimum: 20+
if err != nilpatterns maintained - Rationale: Ensures robust error handling and prevents silent failures
- Required: Proper mutex usage for shared state
- Minimum: 5+ mutex instances maintained across the codebase
- Race detection: All tests run with
-raceflag - Rationale: Prevents data races and ensures thread safety
- Maximum: 10
interface{}usages in non-test code - Preferred: Use concrete types and generics where possible
- Rationale: Improves type safety and reduces runtime errors
- Required: Proper slice preallocation where capacity is known
- Prohibited: Manual
runtime.GC()calls - Rationale: Optimizes memory usage and prevents performance degradation
- Required: All exported functions must have comments
- Format: Follow Go documentation conventions
- Rationale: Maintains code readability and API documentation
# Install pre-commit hooks (recommended)
pip install pre-commit
pre-commit install
# Install golangci-lint
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latestRun these commands before committing:
# Format code
go fmt ./...
# Run linting (catches all regression patterns)
golangci-lint run --config=.golangci.yml
# Run tests with race detection
go test -race ./...
# Run benchmarks (smoke test)
go test -bench=. -benchtime=1s -run=^$ ./...Pre-commit hooks check for:
- Code formatting and imports
- Function complexity limits
- Magic number detection
- Concurrency safety patterns
- Type safety compliance
- General code quality issues
All pull requests must pass the CI/CD pipeline:
- Linting: golangci-lint with comprehensive rules
- Testing: Unit tests with race detection on Go 1.21 and 1.22
- Building: Cross-platform builds (Linux, macOS, Windows)
- Security: Gosec security scanning
- Coverage: Code coverage analysis
- Quality Gate: All checks must pass before merge
- Keep functions under 120 lines
- Limit cyclomatic complexity to 15
- Use descriptive names
- Single responsibility principle
// Good: Proper error propagation
result, err := someOperation()
if err != nil {
return fmt.Errorf("operation failed: %w", err)
}
// Bad: Ignoring errors
result, _ := someOperation()// Good: Proper mutex usage
type SafeCounter struct {
mu sync.Mutex
count int
}
func (c *SafeCounter) Increment() {
c.mu.Lock()
defer c.mu.Unlock()
c.count++
}
// Bad: Unprotected shared state
type UnsafeCounter struct {
count int // Race condition risk
}// Good: Concrete types
func ProcessCommand(cmd Command) error {
// Implementation
}
// Acceptable: Constrained generics
func ProcessItems[T Processable](items []T) error {
// Implementation
}
// Avoid: Excessive interface{} usage
func ProcessAnything(data interface{}) error {
// Avoid this pattern
}- Maintain high test coverage (aim for >80%)
- Include unit tests for all new functionality
- Add integration tests for complex workflows
- All tests must pass with
-raceflag - Test concurrent scenarios explicitly
- Use proper synchronization primitives
- Include benchmarks for performance-critical code
- Ensure benchmarks pass in CI (smoke test)
- Document performance expectations
## Summary
Brief description of changes and motivation.
## Changes Made
- List specific changes
- Reference any issues fixed
## Testing
- [ ] Unit tests added/updated
- [ ] Integration tests pass
- [ ] Benchmarks pass
- [ ] Race detection clean
## Quality Checklist
- [ ] Function complexity under limits
- [ ] No magic numbers introduced
- [ ] Proper error handling
- [ ] Concurrency safety maintained
- [ ] Type safety preserved
- [ ] Documentation updated- Automated quality checks must pass
- Code review by maintainer
- Manual testing if needed
- Merge after approval
Current quality metrics (maintained by automation):
- Function Complexity: ✅ All functions under 120 lines
- Magic Numbers: ✅ All constants properly extracted
- Error Handling: ✅ Comprehensive error propagation
- Concurrency Safety: ✅ Race-free with proper synchronization
- Type Safety: ✅ Minimal interface{} usage
- Test Coverage: ✅ High coverage with race detection
- Security: ✅ No known vulnerabilities
- Issues: Report bugs and feature requests via GitHub Issues
- Discussions: Use GitHub Discussions for questions
- Documentation: Check the README and inline documentation
- Code Examples: See the
examples/directory
Automated checks prevent regression of these fixes:
- TDD Cycle 1: Function complexity and magic number elimination
- TDD Cycle 2: Error handling improvements
- TDD Cycle 3: Concurrency safety enhancements
- TDD Cycle 4: Type safety and memory optimization
Any changes that would reintroduce these issues will be automatically rejected by our CI/CD pipeline.
These guidelines keep the codebase reliable and maintainable.