Prefer Data Over Identity Branches
Represent identity validation and mappings as data instead of repetitive control-flow branches
Source: .agents/rules/prefer-data-over-identity-branches.mdc
Metadata
- name: prefer-data-over-identity-branches
- alwaysApply: true
Content
Prefer Data Over Identity Branches
When branches only repeat the value they match, the code is describing data, not distinct behavior. Represent the supported values or mapping once instead of duplicating identity branches.
Rules
- Do not write repeated identity branches such as
case 'EVENT': return 'EVENT'. - Use a typed allowlist when the operation only validates membership.
- Use a typed lookup when each input maps to a value without branch-specific behavior.
- Preserve boundary behavior: normalization, unsupported-value handling, and return types must remain explicit.
- Do not reintroduce an unsafe
asassertion to make the data-driven version compile. - Keep a
switchwhen branches perform genuinely different work, have different side effects, or benefit from exhaustive control-flow narrowing.
// ❌ Avoid: duplicated identity branches hide a simple membership check
function getEntityType(type?: string): MentionedCardEntityType | null {
switch (type?.toUpperCase()) {
case 'EVENT':
return 'EVENT';
case 'FOLDER':
return 'FOLDER';
default:
return null;
}
}
// ✅ Prefer: each supported value is represented once
const supportedEntityTypes: MentionedCardEntityType[] = [
'EVENT',
'FOLDER'
];
function getEntityType(type?: string): MentionedCardEntityType | null {
const normalizedType = type?.toUpperCase();
return (
supportedEntityTypes.find(
(supportedType) => supportedType === normalizedType
) ?? null
);
}