Break down complex or deeply nested code into smaller, well-named helper functions to improve readability and maintainability. Extract code into helpers when:
Break down complex or deeply nested code into smaller, well-named helper functions to improve readability and maintainability. Extract code into helpers when:
Example of improving complex nested code:
Before:
function processNextEditData(filePath, beforeContent, afterContent, cursorPosBeforeEdit, ...) {
// Get the current context data
const currentData = (global._editAggregator as any).latestContextData || {
configHandler: data.configHandler,
getDefsFromLspFunction: data.getDefsFromLspFunction,
recentlyEditedRanges: [],
recentlyVisitedRanges: [],
};
void processNextEditData(
beforeAfterdiff.filePath,
beforeAfterdiff.beforeContent,
beforeAfterdiff.afterContent,
cursorPosBeforeEdit,
cursorPosAfterPrevEdit,
this.ide,
currentData.configHandler,
currentData.getDefsFromLspFunction,
currentData.recentlyEditedRanges,
currentData.recentlyVisitedRanges,
currentData.workspaceDir,
);
}
After: ```typescript interface ProcessNextEditDataParams { filePath: string; beforeContent: string; afterContent: string; cursorPosBeforeEdit: Position; cursorPosAfterPrevEdit: Position; ide: IDE; configHandler: ConfigHandler; getDefsFromLspFunction: GetLspDefinitionsFunction; recentlyEditedRanges: RecentlyEditedRange[]; recentlyVisitedRanges: AutocompleteCodeSnippet[]; workspaceDir: string; }
function getCurrentContextData(): ContextData { return (global._editAggregator as any).latestContextData || { configHandler: data.configHandler, getDefsFromLspFunction: data.getDefsFromLspFunction, recentlyEditedRanges: [], recentlyVisitedRanges: [], }; }
function processNextEditData(params: ProcessNextEditDataParams) { const currentData = getCurrentContextData(); // Process using structured parameters… }
Enter the URL of a public GitHub repository