Skip to content

Code Quality Improvements: Logging, Constants, Docs & Cleanup - #20

Merged
cnicholas merged 1 commit into
mainfrom
cleanup-code-quality
Oct 15, 2025
Merged

cnicholas merged 1 commit into
mainfrom
cleanup-code-quality

Conversation

@cnicholas

Copy link
Copy Markdown
Owner

Summary

This PR addresses Issues #12, #14, #16, and #18, implementing four targeted code quality improvements that enhance maintainability, professionalism, and developer experience.

Changes

🧹 Issue #12: Remove Duplicate Utility Functions (HIGH PRIORITY)

Problem: Five utility functions were duplicated between analysis_dataset.py and objects.py, creating a maintenance burden and risk of divergence.

Solution:

  • ✅ Removed 69 lines of duplicate code from analysis_dataset.py
    • calculate_limits() - Control limit calculations
    • c4() - Bias constant for Xbar/S charts
    • b3(), b4() - S chart limit multipliers
    • detect_beyond_limits() - Point detection logic
  • ✅ Single source of truth maintained in objects.py
  • ✅ All code now uses obj.calculate_limits() pattern

Impact: Eliminates risk of bugs being fixed in one place but not the other


📝 Issue #14: Replace print() with Proper Logging (MEDIUM PRIORITY)

Problem: 33+ print() statements scattered throughout production code made debugging unprofessional and uncontrollable.

Solution:

  • ✅ Added module-level logger: logger = logging.getLogger(__name__)
  • ✅ Replaced all print() statements with appropriate logging levels:
    • logger.debug() - 25 instances for data inspection and flow tracing
    • logger.info() - 5 instances for important operations (e.g., zero-centering)
    • logger.warning() - 3 instances for limitations and edge cases

Examples:

# Before
print(f'\nIn calculate statistics XbarS...')
print(f'zero-centering data')

# After
logger.debug('In calculate statistics XbarS')
logger.info('Zero-centering data')

Benefits:

  • Professional, configurable output
  • No performance impact when logging disabled
  • Better production debugging

🔢 Issue #16: Replace Magic Numbers with Named Constants (MEDIUM PRIORITY)

Problem: Statistical constants (2.66, 3.268, 3) appeared throughout code with no explanation of their origin or purpose.

Solution:

  • ✅ Added well-documented constants in objects.py:
# Statistical constants based on SPC theory
SIGMA_MULTIPLIER = 3                # Standard 3-sigma control limits
IMR_LIMIT_MULTIPLIER = 2.66         # E2 constant (n=2 moving range)
R_UPPER_LIMIT_MULTIPLIER = 3.268    # D4 constant (n=2 range chart)
  • ✅ Updated 8 locations using magic numbers:
    • calculate_limits() - Xbar, IMR, R calculations
    • b3(), b4() - S chart calculations

Before:

lcl = mean + (-1.0 * (2.66 * mR))  # What is 2.66?
ucl = mR * 3.268                   # Where does 3.268 come from?

After:

lcl = mean + (-1.0 * (IMR_LIMIT_MULTIPLIER * mR))  # Clear meaning
ucl = mR * R_UPPER_LIMIT_MULTIPLIER                # Self-documenting

Benefits:

  • Self-documenting code
  • Easy to verify against statistical references
  • Single point of modification

📚 Issue #18: Document Sampling Design State Logic (MEDIUM PRIORITY)

Problem: Docstring claimed to handle SDS 1-6 but only SDS 1-2 were implemented, misleading users.

Solution:

  • ✅ Enhanced __calculate_sampling_design_state() docstring with:
    • Clear "Currently Implemented" section (SDS 0, 1, 2)
    • Explicit "Not Yet Implemented" section (SDS 3-6)
    • Return value documentation
    • Usage notes and statistical references

Before: Misleading - claimed SDS 1-6 support
After: Accurate - clearly states SDS 0-2 only, with roadmap for 3-6

Benefits:

  • Users understand current capabilities
  • Clear feature roadmap
  • No false expectations

Impact Summary

Metrics

  • Lines removed: 123 (duplicates, print statements)
  • Lines added: 293 (logging, constants, documentation)
  • Net change: +170 lines (better structure and docs)
  • Files changed: 2 (analysis_dataset.py, objects.py)

Quality Improvements

  • ✅ Maintainability: Single source of truth for utilities
  • ✅ Professionalism: Proper logging infrastructure
  • ✅ Readability: Self-documenting constants
  • ✅ Accuracy: Honest documentation
  • ✅ Testing: All 27 tests passing ✓

Testing

pytest tests/ -v
# ============================== 27 passed in 0.75s ==============================

All existing tests pass without modification, confirming zero breaking changes.

Checklist

  • All tests passing (27/27)
  • No breaking changes
  • Removed dead/duplicate code
  • Added professional logging
  • Replaced magic numbers with named constants
  • Improved documentation accuracy
  • Commit message follows conventions

Related Issues

Closes #12
Closes #14
Closes #16
Closes #18


🤖 Generated with Claude Code

#12, #14, #16, #18)

This commit implements four code quality improvements to enhance
maintainability, readability, and professionalism of the codebase.

## Changes Made

### Issue #12: Remove Duplicate Utility Functions (HIGH PRIORITY)
- **Removed** 69 lines of duplicate code from `analysis_dataset.py`:
  - `calculate_limits()` - already exists in `objects.py`
  - `c4()`, `b3()`, `b4()` - statistical helper functions
  - `detect_beyond_limits()` - limit detection logic
- **Result**: Single source of truth for all utility functions in `objects.py`
- **Impact**: Eliminates maintenance burden and potential divergence

### Issue #14: Replace print() with Proper Logging (MEDIUM PRIORITY)
- **Added** module-level logger: `logger = logging.getLogger(__name__)`
- **Replaced** 33+ print() statements with appropriate logging levels:
  - `logger.debug()` - 25 instances (data inspection, flow tracing)
  - `logger.info()` - 5 instances (important operations like zero-centering)
  - `logger.warning()` - 3 instances (limitations, edge cases)
- **Benefits**:
  - Professional logging output
  - Configurable verbosity levels
  - Better debugging in production
  - No performance impact when logging disabled

### Issue #16: Replace Magic Numbers with Named Constants (MEDIUM PRIORITY)
- **Added** well-documented statistical constants in `objects.py`:
  ```python
  SIGMA_MULTIPLIER = 3              # Standard 3-sigma control limits
  IMR_LIMIT_MULTIPLIER = 2.66       # E2 constant (n=2 moving range)
  R_UPPER_LIMIT_MULTIPLIER = 3.268  # D4 constant (n=2 range)
  ```
- **Updated** 8 occurrences of magic numbers:
  - `calculate_limits()`: Xbar, IMR, and R calculations
  - `b3()` and `b4()`: S chart limit calculations
- **Benefits**:
  - Self-documenting code
  - Easy to verify correctness
  - Centralized modification point
  - References to statistical theory included

### Issue #18: Document Sampling Design State Logic (MEDIUM PRIORITY)
- **Enhanced** `__calculate_sampling_design_state()` docstring with:
  - Clear explanation of SDS 0, 1, 2 (currently implemented)
  - Explicit list of SDS 3-6 (not yet implemented)
  - Return value documentation
  - Usage notes and statistical references
- **Benefits**:
  - Users understand current capabilities
  - Clear roadmap for future enhancements
  - No misleading documentation

## Impact Summary

### Code Quality Metrics
- **Lines removed**: 123 (duplicates + print statements)
- **Lines added**: 293 (logging, constants, documentation)
- **Net change**: +170 lines (more documentation, better structure)

### Benefits
- ✅ **Better maintainability**: Single source of truth for utilities
- ✅ **Professional logging**: Configurable debug output
- ✅ **Self-documenting code**: Named constants with references
- ✅ **Accurate documentation**: Clear about what's implemented
- ✅ **All tests passing**: 27/27 tests pass ✓

## Testing

```bash
pytest tests/ -v
# ============================== 27 passed in 0.75s ==============================
```

All existing tests pass without modification, confirming backward compatibility.

## Files Modified
- `analysis_dataset.py`: Logging, removed duplicates, enhanced docs
- `objects.py`: Added statistical constants, removed magic numbers

Closes #12
Closes #14
Closes #16
Closes #18

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
@cnicholas
cnicholas merged commit d08466f into main Oct 15, 2025
0 of 2 checks passed
@cnicholas
cnicholas deleted the cleanup-code-quality branch October 15, 2025 02:55
cnicholas added a commit that referenced this pull request Sep 1, 2026
#12, #14, #16, #18) (#20)

This commit implements four code quality improvements to enhance
maintainability, readability, and professionalism of the codebase.

## Changes Made

### Issue #12: Remove Duplicate Utility Functions (HIGH PRIORITY)
- **Removed** 69 lines of duplicate code from `analysis_dataset.py`:
  - `calculate_limits()` - already exists in `objects.py`
  - `c4()`, `b3()`, `b4()` - statistical helper functions
  - `detect_beyond_limits()` - limit detection logic
- **Result**: Single source of truth for all utility functions in `objects.py`
- **Impact**: Eliminates maintenance burden and potential divergence

### Issue #14: Replace print() with Proper Logging (MEDIUM PRIORITY)
- **Added** module-level logger: `logger = logging.getLogger(__name__)`
- **Replaced** 33+ print() statements with appropriate logging levels:
  - `logger.debug()` - 25 instances (data inspection, flow tracing)
  - `logger.info()` - 5 instances (important operations like zero-centering)
  - `logger.warning()` - 3 instances (limitations, edge cases)
- **Benefits**:
  - Professional logging output
  - Configurable verbosity levels
  - Better debugging in production
  - No performance impact when logging disabled

### Issue #16: Replace Magic Numbers with Named Constants (MEDIUM PRIORITY)
- **Added** well-documented statistical constants in `objects.py`:
  ```python
  SIGMA_MULTIPLIER = 3              # Standard 3-sigma control limits
  IMR_LIMIT_MULTIPLIER = 2.66       # E2 constant (n=2 moving range)
  R_UPPER_LIMIT_MULTIPLIER = 3.268  # D4 constant (n=2 range)
  ```
- **Updated** 8 occurrences of magic numbers:
  - `calculate_limits()`: Xbar, IMR, and R calculations
  - `b3()` and `b4()`: S chart limit calculations
- **Benefits**:
  - Self-documenting code
  - Easy to verify correctness
  - Centralized modification point
  - References to statistical theory included

### Issue #18: Document Sampling Design State Logic (MEDIUM PRIORITY)
- **Enhanced** `__calculate_sampling_design_state()` docstring with:
  - Clear explanation of SDS 0, 1, 2 (currently implemented)
  - Explicit list of SDS 3-6 (not yet implemented)
  - Return value documentation
  - Usage notes and statistical references
- **Benefits**:
  - Users understand current capabilities
  - Clear roadmap for future enhancements
  - No misleading documentation

## Impact Summary

### Code Quality Metrics
- **Lines removed**: 123 (duplicates + print statements)
- **Lines added**: 293 (logging, constants, documentation)
- **Net change**: +170 lines (more documentation, better structure)

### Benefits
- ✅ **Better maintainability**: Single source of truth for utilities
- ✅ **Professional logging**: Configurable debug output
- ✅ **Self-documenting code**: Named constants with references
- ✅ **Accurate documentation**: Clear about what's implemented
- ✅ **All tests passing**: 27/27 tests pass ✓

## Testing

```bash
pytest tests/ -v
# ============================== 27 passed in 0.75s ==============================
```

All existing tests pass without modification, confirming backward compatibility.

## Files Modified
- `analysis_dataset.py`: Logging, removed duplicates, enhanced docs
- `objects.py`: Added statistical constants, removed magic numbers

Closes #12
Closes #14
Closes #16
Closes #18

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant