Avoid duplicating logic across files or within multiple branches of the same function. When the same algorithm, HTTP call, parsing routine, or computation appears in more than one place, extract it into a single shared helper function or module and have all call sites use it. Duplicated code drifts over time—bug fixes and improvements applied to one copy are easily forgotten in the other(s), leading to inconsistent behavior.
Apply this whenever you notice:
concerns/toolArgs.js, ZedService.refreshLlmToken()) and import it from both call sites.computeFinishReason(state), and call it from both branches.Example refactor:
// Before: duplicated in two branches
if (!state.finishReasonSent && state.started) {
state.finishReasonSent = true;
return { ...buildChunk(state), choices: [{ index: 0, delta: {}, finish_reason: "stop" }] };
}
// ...later, same pattern repeated for a different event...
// After: single source of truth
function computeFinishReason(state) {
return state.toolCallIndex > 0 || state.currentToolCallId ? "tool_calls" : "stop";
}
When extracting shared logic that depends on non-obvious state (e.g., sticky flags like state.currentToolCallId), add a brief comment explaining why that state matters, so future maintainers understand the intent without re-deriving it.