mirror of
https://github.com/labs42io/clean-code-typescript.git
synced 2025-04-18 15:13:34 +00:00
Fix typo in 'Remove Duplicate Code' section and added union type suggestion
This commit is contained in:
parent
263c7a578b
commit
c4c6c52c99
1 changed files with 20 additions and 1 deletions
21
README.md
21
README.md
|
@ -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)**
|
||||
|
|
Loading…
Add table
Reference in a new issue