Skip to content

Working example with detailed commit history on the "remove setting method" refactoring based on Fowler's "Refactoring" book

License

Notifications You must be signed in to change notification settings

kaiosilveira/remove-setting-method-refactoring

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Continuous Integration

ℹ️ This repository is part of my Refactoring catalog based on Fowler's book with the same title. Please see kaiosilveira/refactoring for more details.


Remove Setting Method

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.

Working example

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;
  }
}

Test suite

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.

Steps

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!

Commit history

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.

About

Working example with detailed commit history on the "remove setting method" refactoring based on Fowler's "Refactoring" book

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project