Refactor Labeller.java: improve exception handling, input validation, and code maintainability#1326
Open
akm19117012 wants to merge 1 commit into
Open
Refactor Labeller.java: improve exception handling, input validation, and code maintainability#1326akm19117012 wants to merge 1 commit into
akm19117012 wants to merge 1 commit into
Conversation
This PR refactors Labeller.java to improve robustness, readability, exception safety, input validation, and maintainability while preserving the existing labelling workflow behavior. The changes focus on eliminating unsafe patterns from the original implementation, improving dependency management, reducing mutable state, and making the CLI interaction flow safer and easier to maintain.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR refactors
Labeller.javato improve robustness, readability, exception safety, input validation, and maintainability while preserving the existing labelling workflow behavior.The changes focus on eliminating unsafe patterns from the original implementation, improving dependency management, reducing mutable state, and making the CLI interaction flow safer and easier to maintain.
Key Improvements
1. Replaced Magic Numbers with
LabelActionEnumIntroduced a strongly typed
LabelActionenum for label actions:MATCHNO_MATCHNOT_SUREQUITWhy
The original implementation used hardcoded numeric values (
0,1,2,9) throughout the codebase, which reduced readability and increased the chance of incorrect usage.Benefits
2. Added Constructor-Based Dependency Injection
Refactored initialization of:
ITrainingDataModelILabelDataViewHelperto constructor injection.
Why
The original implementation lazily initialized dependencies internally, which tightly coupled object creation with business logic.
Benefits
3. Converted Dependencies to
finalMarked core dependencies as immutable:
Why
These objects should not change during object lifecycle.
Benefits
4. Improved Exception Handling
Refactored exception handling logic across:
execute()processRecordsCli()getUnmarkedRecords()Changes
Exceptionhandling with more specific exception handlingRuntimeExceptionwrappingWhy
The original implementation suppressed failures in several places, making debugging difficult.
Benefits
5. Added Safe CLI Input Validation
Refactored CLI input validation logic.
Improvements
Why
The original implementation relied on regex matching and unsafe scanner usage.
Benefits
6. Added
ScannerLifecycle ManagementAdded scanner cleanup in
finallyblock.Why
The original implementation instantiated scanners repeatedly and never released resources.
Benefits
7. Replaced Hardcoded Join Type with Constant
Introduced:
Why
The original implementation used inline string literals for join types.
Benefits
8. Improved Logging Messages
Updated multiple logging statements to:
Examples
Benefits
9. Added Null Validation Using
Objects.requireNonNullAdded defensive validation for:
Why
These values are critical for processing flow.
Benefits
10. Removed Unused Lazy Initialization Logic
Removed setter-based mutation and lazy initialization logic from:
getTrainingDataModel()getLabelDataViewHelper()Why
Dependencies are now constructor-injected and immutable.
Benefits
11. Improved Code Readability
Refactored:
Benefits
Behavior Compatibility
This refactor preserves the existing CLI labelling workflow and does not change:
The changes are primarily focused on code quality, stability, and maintainability improvements.
Areas for Future Improvement
The following improvements are intentionally left out of this PR to keep the refactor focused:
processRecordsCli()into smaller methodsunpersist()Testing Notes
Validated: