Refactoring
Refactoring means improving how code is written without changing what it does. Rename unclear variables, remove duplication, and split long functions, in small steps. Having tests makes refactoring safe, because they tell you if you broke something.
// Before: duplicated logic
function getAdultNames(users) {
const result = []
for (let i = 0; i < users.length; i++) {
if (users[i].age >= 18) result.push(users[i].name)
}
return result
}
// After: same behavior, clearer
const isAdult = (user) => user.age >= 18
function getAdultNames(users) {
return users.filter(isAdult).map((user) => user.name)
}