50

Return Modified Value


Goal

Instead of mutating a parameter in place, the function returns the modified value so the caller reassigns.

Before the refactoring
function addTax(order) {
  order.total *= 1.1;
}
addTax(order);
After the refactoring
function withTax(order) {
  return { ...order, total: order.total * 1.1 };
}
order = withTax(order);
Savings

Side effects on inputs disappear; the function reads as a transformation; equality and snapshotting become possible.

Note

Callers must remember to capture the returned value; if any forget, they keep the unmodified original. Mark the parameter readonly so the type system helps.