-
Notifications
You must be signed in to change notification settings - Fork 274
Add AV1002: Only pass things to a constructor that most or all members need #309
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| --- | ||
| rule_id: 1002 | ||
| rule_category: class-design | ||
| title: Only pass things to a constructor that most or all members need | ||
| severity: 3 | ||
| --- | ||
| A constructor exists to put the object in a valid, usable state. If a dependency or value is only used by one or two members out of many, passing it through the constructor forces every caller to provide something that is barely relevant to the object as a whole. This is a strong signal that those members belong in a separate, more focused class (see [AV1000](#{{ site.default_rule_prefix }}1000)). | ||
|
|
||
| **Exception:** Cross-cutting concerns such as logging or clock abstractions are often needed broadly and may reasonably be injected through the constructor even if not every member uses them directly. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a bit vague regarding injected dependencies.
So, suppose we define a class with 5 members, and only one needs the I think it would help to distinguish between the kinds of parameters: values and dependencies. Then the rule definition can be about values, while the exception is about dependencies.
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, you're right, and I don't think we can define this rule in such a way it is comprehensive enough to provide clear guidance. Unless you know a better way.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another issue with this rule is that API breaking changes are required if the implementation changes. For example, in the current version, the parameter X is omitted from the constructor because it's used by only two members. In the next version, a new member that also depends on X is added, so X must now be added as a constructor parameter and removed from the existing members. I think we should drop this rule. |
||
Uh oh!
There was an error while loading. Please reload this page.