Back to all reviewers

Simplify conditional logic

cloudflare/workers-sdk
Based on 4 comments
TypeScript

Prefer concise conditional expressions over verbose nested conditions and unnecessary boolean variables. This improves readability and reduces cognitive overhead.

Code Style TypeScript

Reviewer Prompt

Prefer concise conditional expressions over verbose nested conditions and unnecessary boolean variables. This improves readability and reduces cognitive overhead.

Key practices:

  • Combine multiple related conditions into a single statement
  • Remove redundant type checks when more specific checks are sufficient
  • Use direct assignment instead of conditional blocks when possible
  • Eliminate unnecessary boolean variables in favor of direct control flow

Examples:

Instead of nested conditions:

if (binding.binding && typeof binding.binding === "string") {
    if (binding.binding.toUpperCase() === normalizedName) {
        return true;
    }
}

Combine into single condition:

if (typeof binding.binding === "string" && binding.binding.toUpperCase() === normalizedName) {
    return true;
}

Instead of boolean tracking variables:

let isValid = false;
while (!isValid) {
    // validation logic
    if (validation.valid) {
        isValid = true;
    }
}

Use direct control flow:

while (true) {
    // validation logic
    if (validation.valid) {
        return bindingName;
    }
}

Instead of conditional assignment:

if (match) {
    const cachedAssetKey = await match.text();
    if (cachedAssetKey === assetKey) {
        shouldUpdateCache = false;
    }
}

Use direct assignment:

if (match) {
    const cachedAssetKey = await match.text();
    shouldUpdateCache = cachedAssetKey !== assetKey;
}
4
Comments Analyzed
TypeScript
Primary Language
Code Style
Category

Source Discussions