When you notice code patterns being repeated across multiple locations, extract them into reusable functions or constants to improve maintainability and follow DRY principles.
Look for these common duplication patterns:
Example from the codebase:
// Before: Duplicated logic in multiple methods
async storeCredentials(data: AuthFile): Promise<void> {
const authData: AuthFile = { tokens: this.loadedCredentials }
await mkdir(path.dirname(this.authFilePath), { recursive: true })
await writeFile(this.authFilePath, JSON.stringify(authData, null, 2))
}
async deleteCredentials(workspaceId: string): Promise<void> {
this.loadedCredentials = this.loadedCredentials?.filter((c) => c.workspaceId !== workspaceId) || []
const data: AuthFile = { tokens: this.loadedCredentials }
await mkdir(path.dirname(this.authFilePath), { recursive: true })
await writeFile(this.authFilePath, JSON.stringify(data, null, 2))
}
// After: Extract shared logic
private async writeAuthFile(data: AuthFile): Promise<void> {
await mkdir(path.dirname(this.authFilePath), { recursive: true })
await writeFile(this.authFilePath, JSON.stringify(data, null, 2))
}
This practice reduces maintenance burden, eliminates inconsistencies, and makes code changes easier to implement across the codebase.
Enter the URL of a public GitHub repository