Skip to content

prefer-switch-for-repeated-equality

Prefer switch over long if/else-if chains that compare one value for strict equality.

Why

A switch makes finite dispatch cases visually uniform and easier to extend without duplicating the discriminant.

Fix

Replace three or more strict-equality branches over the same discriminant with a switch; keep if statements for ranges, guards, and heterogeneous predicates.

Examples

Before — flagged Avoid repeating the discriminant
src/render.ts
if (kind === "a") return a();
else if (kind === "b") return b();
else if (kind === "c") return c();
After — preferred Make finite dispatch explicit
src/render.ts
switch (kind) {
case "a":
return a();
case "b":
return b();
case "c":
return c();
default:
return fallback();
}