When functions contain complex inline logic or duplicated code, extract this logic into separate functions or shared utilities to improve readability and maintainability. Complex inline code makes functions harder to understand and test, while duplication creates synchronization issues and increases the likelihood of bugs.
When functions contain complex inline logic or duplicated code, extract this logic into separate functions or shared utilities to improve readability and maintainability. Complex inline code makes functions harder to understand and test, while duplication creates synchronization issues and increases the likelihood of bugs.
For complex inline logic, create dedicated functions:
// Instead of complex inline logic:
function mapToCallback(context, callback, onError) {
return async function (req) {
const asyncContext = getAsyncContext();
setAsyncContext(context.asyncContext);
try {
// ... complex logic here ...
} finally {
// ... cleanup logic ...
}
};
}
// Extract into a separate function:
function mapToCallback(context, callback, onError) {
return async function (req) {
return handleRequestWithContext(req, context, callback, onError);
};
}
For duplicated code between similar implementations (like sync/async versions), consider:
This approach reduces cognitive load, makes code easier to test, and prevents bugs that arise from maintaining multiple copies of similar logic.
Enter the URL of a public GitHub repository