Skip to content

refactor: simplify code structure and reduce complexity#3

Merged
hrayleung merged 2 commits into
devfrom
refactor
Jan 18, 2026
Merged

refactor: simplify code structure and reduce complexity#3
hrayleung merged 2 commits into
devfrom
refactor

Conversation

@hrayleung

@hrayleung hrayleung commented Jan 18, 2026

Copy link
Copy Markdown
Owner

Summary

Code simplification and refactoring to improve maintainability and readability across multiple components.

Changes Made

  • Embeddings Backend (src/embeddings/backend.py): Simplified _validate_api() by removing redundant conditional logic
  • Indexer Service (src/indexer/service.py): Extracted nested functions to class methods for better testability
  • Retrieval Service (src/retrieval/service.py): Split MMR candidate selection logic into separate method to reduce nesting
  • BM25 Search (src/retrieval/bm25_search.py): Added early returns for clearer control flow
  • Hybrid Search (src/retrieval/hybrid.py): Extracted future handling logic to improve parallel execution readability

Key Improvements

  • Reduced nesting depth across multiple methods
  • Improved testability by extracting nested functions
  • Made control flow more explicit with guard clauses
  • Better separation of concerns
  • No behavioral changes - purely structural refactoring

Test Plan

  • Run existing test suite to verify no regressions
  • Verify all functionality remains intact

🤖 Generated with Claude Code

Greptile Summary

Refactored code across embeddings, indexing, and retrieval services to reduce complexity through systematic extraction of nested logic into dedicated methods and simplification of control flow.

Key Changes:

  • Embeddings: Inverted conditional in _validate_api() for clearer early-return pattern
  • Indexer: Extracted _get_repo_safe() and _check_files_modified() from inline lambdas/nested loops to testable class methods
  • BM25 Search: Added guard clauses with early returns to flatten nested conditionals
  • Hybrid Search: Extracted _await_search_result() from inline future handling to improve parallel execution readability
  • Retrieval Service: Extracted _select_best_candidate() from nested MMR loop to reduce indentation depth

Impact:
All changes are structural refactorings with no behavioral modifications. The changes improve testability (extracted methods can be unit tested), readability (reduced nesting), and maintainability (clearer separation of concerns).

Confidence Score: 5/5

  • This PR is safe to merge with no risk
  • All changes are pure structural refactorings that preserve existing behavior while improving code organization. Each change reduces complexity through well-established patterns: extracting methods, adding guard clauses, and inverting conditionals for early returns. No logic changes, no new dependencies, no external behavior changes.
  • No files require special attention

Important Files Changed

Filename Overview
src/embeddings/backend.py Inverted conditional logic to simplify control flow, maintaining identical behavior
src/indexer/service.py Extracted nested functions to class methods for improved testability
src/retrieval/bm25_search.py Added early returns for clearer control flow in two search functions
src/retrieval/hybrid.py Extracted future handling logic into separate method for better readability
src/retrieval/service.py Split MMR candidate selection into dedicated method to reduce nesting depth

Sequence Diagram

sequenceDiagram
    participant User
    participant SearchService
    participant HybridSearch
    participant VectorSearch
    participant BM25Search
    participant SymbolSearch
    participant IndexerService
    
    User->>SearchService: search(repo_name, query)
    SearchService->>HybridSearch: hybrid_search(repo_name, query)
    
    par Parallel Search Execution
        HybridSearch->>VectorSearch: vector_search()
        HybridSearch->>BM25Search: bm25_search()
        Note over BM25Search: _search_pg_search() or<br/>_search_native_fts()<br/>(with early returns)
        HybridSearch->>SymbolSearch: symbol_hybrid_search()
    end
    
    VectorSearch-->>HybridSearch: vector results
    BM25Search-->>HybridSearch: bm25 results
    SymbolSearch-->>HybridSearch: symbol results
    
    HybridSearch->>HybridSearch: _await_search_result()<br/>(extracted method)
    HybridSearch->>HybridSearch: reciprocal_rank_fusion()
    HybridSearch-->>SearchService: fused results
    
    SearchService->>SearchService: _mmr_diversify()
    SearchService->>SearchService: _select_best_candidate()<br/>(extracted method)
    SearchService-->>User: CodeSnippet results
    
    Note over User,IndexerService: Indexing Flow (Background)
    User->>IndexerService: ensure_indexed(path)
    IndexerService->>IndexerService: resolve_repo_name()
    IndexerService->>IndexerService: _get_repo_safe()<br/>(extracted method)
    IndexerService->>IndexerService: _has_files_changed()
    IndexerService->>IndexerService: _check_files_modified()<br/>(extracted method)
    IndexerService-->>User: IndexResult
Loading

- Simplified validation logic in embeddings backend
- Extracted nested functions to class methods in indexer service
- Split complex methods for better readability (MMR, parallel search)
- Added early returns in BM25 search for clearer control flow
- Improved testability and maintainability across retrieval components

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

5 files reviewed, 1 comment

Edit Code Review Agent Settings | Greptile

Comment thread src/embeddings/backend.py Outdated
Restored the warning message that logs non-200 status codes during API
validation. This debugging information is valuable for troubleshooting
API connection issues.

Addresses Greptile review feedback.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>

@hrayleung hrayleung left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

restore API validation warning log

@hrayleung hrayleung closed this Jan 18, 2026
@hrayleung hrayleung reopened this Jan 18, 2026
@hrayleung hrayleung merged commit 752899b into dev Jan 18, 2026
1 check passed
@hrayleung hrayleung deleted the refactor branch January 18, 2026 06:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant