Avoid Over-Fragmented Logic
Keep cohesive logic together instead of splitting it into tiny, low-value functions
Source: .agents/rules/avoid-over-fragmented-logic.mdc
Metadata
- name: avoid-over-fragmented-logic
- alwaysApply: true
Content
Avoid Over-Fragmented Logic
Do not split readable, cohesive logic into many tiny functions merely to reduce function length.
- Keep steps together when they belong to one flow and are only used by that flow.
- Extract a function when it creates a meaningful boundary: reuse, independent testing, a distinct responsibility, substantial duplication, or a clear reduction in nesting or complexity.
- Avoid one-use wrappers that only rename a simple expression, delegate directly to another function, or force readers to jump between files to understand one operation.
- Prefer a direct local sequence over several helpers that pass intermediate values between them without hiding real complexity.
- Function or component size alone is not a reason to extract. Optimize for cohesive, easy-to-follow logic rather than the smallest possible functions.
- Existing guidance to keep handlers and components small means focused on one responsibility, not fragmented into minimal line counts.
Avoid: One Flow Split Into Tiny Wrappers
function getRefreshedEventInfo(response: EventResponse) {
return response.eventInfo;
}
function hasRefreshedEventInfo(eventInfo: EventInfo | undefined) {
return eventInfo !== undefined;
}
function applyRefreshedEventInfo(eventInfo: EventInfo) {
setEventInfo(eventInfo);
}
function handleRefresh(response: EventResponse) {
const eventInfo = getRefreshedEventInfo(response);
if (hasRefreshedEventInfo(eventInfo)) {
applyRefreshedEventInfo(eventInfo);
}
}
These helpers have no independent responsibility or reuse. Reading one refresh operation requires jumping through several names.
Prefer: Keep the Cohesive Flow Together
function handleRefresh(response: EventResponse) {
if (response.eventInfo) {
setEventInfo(response.eventInfo);
}
}
Extract When the Boundary Is Meaningful
function validateEventPricing(pricing: EventPricing): ValidationResult {
// Shared domain validation with multiple rules and dedicated tests.
}
function handleRefresh(response: EventResponse) {
if (!response.eventInfo) {
return;
}
const validation = validateEventPricing(response.eventInfo.pricing);
if (validation.isValid) {
setEventInfo(response.eventInfo);
}
}
Here the extracted function owns reusable domain behavior and can be tested independently, so the boundary reduces complexity instead of adding navigation overhead.