-
Notifications
You must be signed in to change notification settings - Fork 21
New rule: Records should be fully initialized in constructors #131
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
Comments
Thanks for the rule proposal! I think a rule checking if records are initialised during their constructors would be a really solid addition to the default quality profile - it's easy to accidentally write a record constructor that misses a field or two and it would be a good idea to have a rule that catches these cases.
I disagree with this - I do not think a constructor that simply initialises all fields is bad practice. It works as expected, and because SonarDelphi will raise a new issue if the constructor is not updated, it's not at risk of being left behind. I feel like assigning to It will also create a lot of "noise" (issues that are not real problems) that undermines the core usefulness of the rule - that SonarDelphi can actually definitively tell you, without many false positives, whenever a field has not been initialised in a constructor. To summarise:
Interested to hear others' thoughts? |
I'm quite enthusiastic about this one, definitely a worthwhile addition. In terms of feedback...
Thoughts? |
Yes, indeed, if fields can be guaranteed exhaustive, there is no longer a need to initialize to
Yes, I was. sorry. |
It's actually not specific to constructors though, as this rule should also target the |
Hmm, it could be a little confusing to be named so similarly to
Although that's true, there's an argument to be made that the How about |
These work. We could look at it from the "describing the problem" angle and call it |
I would like to add a (possibly optional) subtlety here. I formerly agreed that "if fields can be guaranteed exhaustive, there is no longer a need to initialize to The default hasher for non-packed records is wrong when there are "holes" due to field alignment, for example an Integer and an Int64 field. The default hasher uses also the "hole values", which can lead to nasty bugs if using such a record is used as a key dictionnary, for example, because this violates the expected invariant that two values that are Equal should have the same hash. I would require using full assignment (for example to |
That's a very good point - I think this would be good as another separate rule. Conceptually, the motivations are a bit different: the reason for using Maybe we could have:
|
Prerequisites
Suggested rule title
RecordsShouldBeFullyInitialized
Rule description
There was discussion in #127 about initializing records through a constructor: should constructors be trusted to properly initialize their results? It was suggested that it should be a separate rule.
This rule would check that even within a constructor, a proper initialization is done by:
Default(T)
, or a constant, or another initialized variable...)Calling
Initialize
isn't enough, because it will only properly initialize managed fields (unless the rule can test that every field in a record is either managed, or a sub-record having recursively the same property).Rationale
Suppose a record has only two fields,
X
andY
. The following constructor could be thought harmless:But if you add another field
Z
later, there is the risk of the constructor adaptation being forgotten.The following is guaranteed to evolve better:
The text was updated successfully, but these errors were encountered: