Refactor Frontend Functions to Reduce Nesting and Improve Code Readability #2487 #2561
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.


#2487
This PR is to refactor multiple functions in the frontend to reduce excessive nesting and improve code readability, maintainability, and testability. Deeply nested functions can be difficult to understand, debug, and test, so this refactor applies several best practices to streamline the code.
What’s Changed:
Reduced nesting depth: Several functions previously with more than 4 levels of nesting have been refactored to reduce complexity.
Guard clauses and early returns: Used early returns and guard clauses to handle edge cases and prevent deeply nested if conditions.
Extracted complex logic into separate, smaller functions to improve clarity and modularity.
Improved maintainability: Simplified logic makes it easier to modify or extend functionality in the future.
Improved testability: By breaking down complex logic, we ensure that each piece is easier to unit test in isolation.
for details of changes --->
Key Refactoring Areas:
Keyboard Event Handling:
The logic for handling keyboard events (Escape, Enter, ArrowDown, ArrowUp) was deeply nested and is now broken into smaller, reusable functions.
Suggestion Handling:
The logic for selecting, highlighting, and navigating through suggestions has been modularized, making it easier to follow and modify.
Early Returns:
Added early returns for edge cases, such as invalid or missing suggestions, to simplify the flow and avoid deep if statements.
Testing:
Verified that the refactored code functions as expected by running the existing tests.
No new functionality was introduced, so the focus was solely on reducing complexity and improving readability.
Checklist:
[ ✔] Refactored functions with deep nesting.
[ ✔] Used early returns and guard clauses to simplify logic.
[ ✔] Extracted complex logic into smaller functions.
[ ✔] Ensured no change in functionality (verified by tests).
[ ✔] I've read and followed the contributing guidelines.
[ ✔] I've run make check-test locally; all checks and tests passed.