54

Remove Setting Method


Goal

Fields whose values should only be set at construction lose their setters; callers either construct a new object or call a domain method that changes the field as a side effect of doing real work.

Before the refactoring
class Person {
  setName(n) { this._name = n; }
}
After the refactoring
class Person {
  constructor(name) { this._name = name; }
  name() { return this._name; }
}
Savings

Immutable-by-default classes; bugs from late mutation vanish; the API expresses what users can actually do.

Note

Removing a setter forces every legitimate update through a more meaningful method — verify there's a domain action behind every setter call before deleting it.