ℹ️ 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 manager() {
return this._department.manager;
}
// ...
} |
manager = aPerson.department.manager; |
Inverse of: Hide Delegate
Encapsulation comes with a price: many small chunks of code hiding internal details and doing tiny bits of processing here and there. Although it's a good guideline to keep our code encapsulated and modular enough, there's always a blurred space where these guidelines can take us too far. This refactoring helps to revert our path when we find ourselves in this situation.
As our working example, we're going to use the same code scenario presented at Hide Delegate, where a Person
class contains a maanger
getter, that hides a delegate to department.manager
. Our goal here, though, is the inverse of that: we want to remove this getter, so clients access the department
property directly.
Our test suite remains the same: we cover the getManager()
function, to make sure it keeps returning the expected data while we perform our refactoring steps:
describe('getManager', () => {
it('should fetch the manager of a person', () => {
const department = new Department();
department.chargeCode = '123';
department.manager = 'Martin';
const aPerson = new Person({ name: 'Kaio' });
aPerson.department = department;
expect(getManager(aPerson)).toEqual('Martin');
});
});
We start by introducing a department
getter at Person
:
diff --git a/src/Person.js b/src/Person.js
@@ -14,4 +14,8 @@
export class Person {
set department(arg) {
this._department = arg;
}
+
+ get department() {
+ return this._department;
+ }
}
Then, we can update Person
's clients to use the department
getter:
diff --git a/src/client/index.js b/src/client/index.js
@@ -1,4 +1,4 @@
export function getManager(aPerson) {
- const manager = aPerson.manager;
+ const manager = aPerson.department.manager;
return manager;
}
Finally, we can remove the manager
getter altogether:
diff --git a/src/Person.js b/src/Person.js
@@ -7,10 +7,6 @@
export class Person {
return this._name;
}
- get manager() {
- return this._department.manager;
- }
-
set department(arg) {
this._department = arg;
}
And that's it!
Below there's the commit history for the steps detailed above.
Commit SHA | Message |
---|---|
790549f | introduce department getter at Person |
c928bd8 | update Person clients to use department getter |
2ebb052 | remove manager getter |
For the full commit history for this project, check the Commit History tab.