<!--
title: Extract reusable logic
domain: ai-agents
topic: Code Style
language: TSX
source: openai/codex
updated: 2025-06-15
url: https://awesomereviewers.com/reviewers/codex-extract-reusable-logic/
-->

Avoid duplicating logic across the codebase. Instead, extract common functionality into well-named helper functions or standalone utilities. This improves code readability, simplifies maintenance, and centralizes logic for easier updates.

Examples where this applies:
- Login flows that appear in multiple code branches
- Notification logic embedded within UI components
- Utility functions that perform specific tasks (like generating summaries)

**Before:**
```javascript
if (provider.toLowerCase() === "githubcopilot" && !apiKey) {
  apiKey = await fetchGithubCopilotApiKey();
  // Additional logic for handling the API key...
} else if (cli.flags.login) {
  if (provider.toLowerCase() === "githubcopilot") {
    apiKey = await fetchGithubCopilotApiKey();
    // Duplicate logic...
  }
}
```

**After:**
```javascript
function handleGithubCopilotLogin() {
  // Centralized login logic
}

if (provider.toLowerCase() === "githubcopilot" && !apiKey) {
  apiKey = await handleGithubCopilotLogin();
} else if (cli.flags.login) {
  if (provider.toLowerCase() === "githubcopilot") {
    apiKey = await handleGithubCopilotLogin();
  }
}
```
