ℹ️ This repository is part of my Refactoring catalog based on Fowler's book with the same title. Please see kaiosilveira/refactoring for more details.
| Before | After |
|---|---|
class Person {
get name() {
/* ... */
}
set name(aString) {
/* ... */
}
} |
class Person {
get name() {
/* ... */
}
} |
Making objects immutable is a practical way of avoiding overhead (in the form of tracking changes throughout a codebase) and unintended side effects. This refactoring helps with just that.
Our working example is a program that contains a Person class, which contains getters and setters for name and id. The goal here is to make id immutable after initialization. To do so, we need to remove its setter. The code looks like this:
export class Person {
get name() {
return this._name;
}
set name(arg) {
this._name = arg;
}
get id() {
return this._id;
}
set id(arg) {
this._id = arg;
}
}The test suite, as the rest of this example, is quite simple and covers the basic behavior of the Person class:
describe('Person', () => {
it('should allow to configure a name', () => {
const person = new Person();
person.name = 'John Doe';
expect(person.name).toEqual('John Doe');
});
it('should allow to configure an id', () => {
const person = new Person();
person.id = 123;
expect(person.id).toEqual(123);
});
});With that in place, we can proceed.
We start by introducing a construtor for Person, it takes an id as argument, and we assign it to our internal _id variable, via its setter:
diff --git Person...
+ constructor(id) {
+ this.id = id;
+ }
+We, then, update the creation script to provide a person id via the constructor:
diff --git caller...
-const kaio = new Person();
-kaio.id = 1;
+const kaio = new Person(1);And then we inline the setting method for id at Person's constructor, effectively making the setter useless:
diff --git Person...
constructor(id) {
- this.id = id;
+ this._id = id;
}We can then safely remove the id setter altogether:
diff --git Person...
- set id(arg) {
- this._id = arg;
- }And that's it!
Below there's the commit history for the steps detailed above.
| Commit SHA | Message |
|---|---|
| 92b8007 | introduce constructor for Person |
| 2051a20 | update creation script to provide person id via constructor |
| d541695 | inline setting method for id at Person's constructor |
| b9c27b9 | remove id setter |
For the full commit history for this project, check the Commit History tab.