This document outlines a systematic plan to eliminate anti-patterns and inefficiencies in the Listeningway codebase. The analysis identified 8 major categories of anti-patterns that impact code maintainability, readability, and performance.
Problem: Excessive "DEFAULT_LISTENINGWAY_" prefixes throughout constants
- 42 constants with unnecessarily verbose names
- Adds no semantic value, reduces readability
- Increases maintenance burden
Examples:
// Current (verbose)
constexpr size_t DEFAULT_LISTENINGWAY_NUM_BANDS = 32;
constexpr float DEFAULT_LISTENINGWAY_FLUX_ALPHA = 0.1f;
// Proposed (clean)
constexpr size_t DEFAULT_NUM_BANDS = 32;
constexpr float DEFAULT_FLUX_ALPHA = 0.1f;Problem: Repetitive patterns in settings initialization and management
- Macro-based INI read/write patterns
- Repetitive equalizer band handling
- Duplicated audio provider enumeration logic
Examples:
- Settings initialization: 40+ repetitive lines in LoadAllTunables()
- Equalizer bands: Manual switch statements for 5 bands
- Audio format detection: Hardcoded switch statements
Problem: overlay.cpp has too many responsibilities (771 lines)
- Single file handles: UI rendering, settings management, provider switching, formatting
- Multiple helper functions that could be grouped into classes
- Poor separation of concerns
Functions to extract:
DrawFrequencyBands()-> FrequencyBandRenderer classDrawVolumeSpatializationBeat()-> VolumeRenderer classDrawBeatDetectionAlgorithm()-> BeatDetectionUI class
Problem: Various hardcoded values without clear documentation
- Audio format constants (1, 2, 6, 8) without enum
- UI spacing values (4.0f, 6.0f) scattered throughout
- Color constants without named definitions
Problem: Pattern matching that could be abstracted
- Equalizer band selection (5 identical cases)
- Audio format detection (5+ cases)
- Provider type mapping
Problem: Manual COM interface management in multiple providers
- Repetitive RAII patterns across audio providers
- Inconsistent error handling
- Manual reference counting
Problem: Multiple mutex patterns that could be consolidated
- Settings mutex vs data mutex
- Inconsistent locking strategies
- Potential deadlock scenarios
Problem: Inconsistent error handling patterns across audio providers
- Mix of exceptions, return codes, and logging
- Inconsistent error recovery strategies
- Missing error propagation in some paths
| Anti-Pattern | Impact | Effort | Priority | Estimated Time |
|---|---|---|---|---|
| Verbose Naming | High | Low | 1 | 2 hours |
| Magic Numbers | Low | Low | 2 | 1 hour |
| Thread Safety | High | Medium | 3 | 4 hours |
| Code Duplication | Medium | Medium | 4 | 6 hours |
| Switch Statements | Medium | Medium | 5 | 3 hours |
| Error Handling | Medium | Medium | 6 | 5 hours |
| Resource Management | Medium | Medium | 7 | 4 hours |
| Large Functions | Medium | High | 8 | 8 hours |
Total Estimated Time: 33 hours
Files to modify:
src/core/constants.h- Remove "DEFAULT_LISTENINGWAY_" prefixsrc/core/settings.h- Update referencessrc/core/settings.cpp- Update references
Benefits:
- Immediate readability improvement
- Reduces visual noise
- Sets precedent for clean naming
New files to create:
src/core/audio_formats.h- Audio format enumsrc/core/ui_constants.h- UI spacing constants
Benefits:
- Type safety
- Self-documenting code
- Easier maintenance
Files to modify:
src/core/settings.cpp- Unified locking strategysrc/audio/audio_analysis.cpp- Consistent mutex usage
Approach:
- Create SettingsManager class with internal synchronization
- Remove global mutexes
- Use RAII lock guards consistently
New abstractions to create:
ConfigurationManagerclass for INI operationsEqualizerBandArrayclass for band management- Template-based settings serialization
New patterns to implement:
- Audio format registry pattern
- Equalizer band strategy pattern
- Provider factory pattern
New components:
Result<T, Error>type for consistent error handling- Centralized error logging strategy
- Error recovery protocols
New patterns:
- RAII wrappers for COM interfaces
- Smart pointer usage for audio resources
- Automatic cleanup patterns
New classes to extract:
OverlayRenderer- Main overlay coordinationFrequencyBandRenderer- Frequency visualizationVolumeRenderer- Volume/spatialization displaySettingsUI- Settings management UIBeatDetectionUI- Beat detection controls
git checkout -b refactor/eliminate-anti-patterns- Start with verbose naming fixes
- Add magic number enums
- Test after each change
- One anti-pattern at a time
- Maintain backward compatibility
- Add unit tests for new components
- Break down into smaller sub-branches
- Extensive testing required
- Performance benchmarking
- Lines of code reduction: Target 15-20%
- Cyclomatic complexity reduction: Target 30%
- Code duplication elimination: Target 80%
- Reduced build times
- Easier onboarding for new developers
- Fewer bug reports related to anti-patterns
- No performance regression
- Improved memory usage patterns
- Better thread contention characteristics
- Verbose naming changes
- Magic number enumeration
- Adding new abstractions alongside existing code
- Thread safety consolidation
- Large function breakdown
- Error handling standardization
- Resource management changes
- Core audio processing modifications
- Create tests for new abstractions
- Regression tests for modified components
- Performance tests for critical paths
- Audio provider switching
- Settings persistence
- UI functionality
- Real-world audio scenarios
- Different audio formats
- Provider switching under load
- Each phase implemented in separate commits
- Feature flags for new implementations
- Ability to revert to previous patterns if issues arise
- Comprehensive backup of current working state
- Approval - Review and approve this refactoring plan
- Environment Setup - Create feature branch and testing environment
- Phase 1 Implementation - Start with verbose naming fixes
- Iterative Development - Implement one anti-pattern fix at a time
- Testing and Validation - Comprehensive testing after each phase
- Documentation - Update code documentation and architecture diagrams
- Team Training - Share new patterns and best practices
Document Version: 1.0
Last Updated: June 4, 2025
Next Review: After Phase 1 completion