Back to all reviewers

Extract repeated logic

microsoft/playwright
Based on 6 comments
TypeScript

When you find yourself writing similar code patterns multiple times, extract them into reusable methods, constants, or leverage existing helpers. This improves maintainability and reduces the chance of inconsistencies.

Code Style TypeScript

Reviewer Prompt

When you find yourself writing similar code patterns multiple times, extract them into reusable methods, constants, or leverage existing helpers. This improves maintainability and reduces the chance of inconsistencies.

Examples of what to extract:

  1. Repeated code blocks - Extract into shared methods: ```typescript // Instead of duplicating enqueue logic: for (let i = 0; i < repeatCount; ++i) this._frameQueue.push(this._lastFrameBuffer);

// Extract to: private _enqueueFrames(count: number, buffer: Buffer) { for (let i = 0; i < count; ++i) this._frameQueue.push(buffer); }


2. **Magic values used multiple times** - Use constants:
```typescript
// Instead of repeating colors:
const color = elements.length > 1 ? '#f6b26b7f' : '#6fa8dc7f';

// Use constants:
const HIGHLIGHT_COLOR_MULTIPLE = '#f6b26b7f';
const HIGHLIGHT_COLOR_SINGLE = '#6fa8dc7f';
  1. Leverage existing utilities - Before writing new code, check if helpers already exist:
    // Instead of custom implementation, use existing helper:
    const callLocation = wrapFunctionWithLocation((location: Location, ...args) => 
      this._modifier('skip', location, ...args)
    );
    
  2. Avoid redundant computations - Pass computed values instead of recomputing:
    // Instead of retrieving from map twice:
    const data = this._dataByTestId.get(params.testId);
    this._updateTest(data); // Pass the data directly
    
6
Comments Analyzed
TypeScript
Primary Language
Code Style
Category

Source Discussions