Software Design
Code Review: Overengineered Code
mediumCode ReviewJS / TS
Problem Statement
Review the following user name formatting module. Identify unnecessary design abstractions, premature generalization, and refactor it into a clean, simple, readable function.
Code under review
JAVASCRIPT• handler.js
Pairlet Snippet
// Formatting a user's display name: "FirstName LastName"
class AbstractNameFormatterFactory {
createFormatter() { throw new Error("Abstract method"); }
}
class StandardNameFormatterStrategy {
format(firstName, lastName) {
return `${firstName} ${lastName}`.trim();
}
}
class DefaultNameFormatterFactory extends AbstractNameFormatterFactory {
createFormatter() {
return new StandardNameFormatterStrategy();
}
}
function formatUserDisplayName(user) {
const factory = new DefaultNameFormatterFactory();
const formatter = factory.createFormatter();
return formatter.format(user.firstName, user.lastName);
}
Read only. Write down what you would flag below, then compare with the reference notes.
JavaScript & TypeScript versions included
Run this as a live interviewPractice solo
Write down what you would flag in this code, then compare it with the reference notes.
Write your findings first, then check yourself against the notes.