Skip to content

Refactor: Extract common pinned directories file operations to reduce code duplication #1082

@coderabbitai

Description

@coderabbitai

Problem

Code duplication exists between removeNotExistingDirectories and TogglePinnedDirectory functions in src/internal/ui/sidebar/directory_utils.go.

Duplicated code patterns:

  • JSON marshaling of directory slice: json.Marshal(dirs)
  • File writing to pinned file: os.WriteFile(variable.PinnedFile, updatedData, 0644)
  • Similar error handling and logging patterns

Problems caused by duplication:

  • Maintenance burden: Changes to file writing logic need to be made in multiple places
  • Inconsistency risk: One location might get updated while others don't, leading to bugs
  • Code bloat: Unnecessary repetition increases file size and complexity
  • Testing overhead: Same logic needs to be tested in multiple contexts
  • Error-prone: Easy to forget updating all locations when making changes

Proposed Solution

Option 1: Extract common utility function

  • Create a savePinnedDirectories(dirs []directory) error function
  • Both functions can call this utility to handle marshaling and file writing

Option 2: Create PinnedDirectoryManager

  • Implement a dedicated manager struct to handle all Read/Write operations to PinnedFile
  • Provides better encapsulation and single responsibility

Related

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions