Fix typo in 'Remove Duplicate Code' section and added union type suggestion

This commit is contained in:
Samuel Šulovský 2022-06-30 14:00:08 +02:00
parent 263c7a578b
commit c4c6c52c99

View file

@ -563,7 +563,7 @@ class Manager {
}
}
function showEmployeeList(employee: Developer | Manager) {
function showEmployeeList(employee: (Developer | Manager)[]) {
employee.forEach((employee) => {
const expectedSalary = employee.calculateExpectedSalary();
const experience = employee.getExperience();
@ -580,6 +580,25 @@ function showEmployeeList(employee: Developer | Manager) {
}
```
You may also consider adding a union type, or common parent class if it suits your abstraction.
```ts
class Developer {
// ...
}
class Manager {
// ...
}
type Employee = Developer | Manager
function showEmployeeList(employee: Employee[]) {
// ...
});
}
```
You should be critical about code duplication. Sometimes there is a tradeoff between duplicated code and increased complexity by introducing unnecessary abstraction. When two implementations from two different modules look similar but live in different domains, duplication might be acceptable and preferred over extracting the common code. The extracted common code, in this case, introduces an indirect dependency between the two modules.
**[ back to top](#table-of-contents)**