mirror of
https://github.com/labs42io/clean-code-typescript.git
synced 2025-04-18 15:13:34 +00:00
Adding recommendation for 4 or fewer constructor parameters
Adding recommendation for 4 or fewer constructor parameters and an alternative of using the Builder Creational Design pattern if the use case is such that is required.
This commit is contained in:
parent
9004fcde03
commit
b70884d20b
1 changed files with 8 additions and 0 deletions
|
@ -1468,6 +1468,14 @@ class Dashboard {
|
|||
|
||||
**[⬆ back to top](#table-of-contents)**
|
||||
|
||||
### Class constructor arguments (4 or fewer)
|
||||
|
||||
Having constructors with a vast amount of parameters can lead to chaotic dependencies or result to very complex client code that would be required in order to build all of these parameters. The recommendation is to have constructors with 4 or fewer arguments, as humans tend to be able to hold 4 things in their head at once *(Susan Weinschenk, 100 Things Every Designer Needs to Know about People, 48)*.
|
||||
|
||||
If the use case is such that your object is complex enough to possibly require more than 4 constructor parameters, evaluate using the *[Builder](https://refactoring.guru/design-patterns/builder)* Creational Design Pattern instead.
|
||||
|
||||
**[⬆ back to top](#table-of-contents)**
|
||||
|
||||
### High cohesion and low coupling
|
||||
|
||||
Cohesion defines the degree to which class members are related to each other. Ideally, all fields within a class should be used by each method.
|
||||
|
|
Loading…
Add table
Reference in a new issue