integrated-application-development / integrated-application-development/sonar-delphi
New rule: Records should be fully initialized in constructors
- Dominant language
- Java
- Stars
- 159
- Forks
- 31
- Avg merge
- 5d 4h
- Merged PRs (30d)
- 4
Description
### Prerequisites
- [X] This rule has not already been suggested.
- [X] This should be a new rule, not an improvement to an existing rule.
- [X] This rule would be generally useful, not specific to my code or setup.
### 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:
- Assigning the result (to `Default(T)`, or a constant, or another initialized variable...)
- Calling another constructor
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` and `Y`. The following constructor could be thought harmless:
```
constructor TMyRecord.Create(AValX: Integer);
begin
Self.X := AValX;
Self.Y := 0;
end;
```
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:
```
constructor TMyRecord.Create(AValX: Integer);
begin
Self := Default(TMyRecord); // or calling another constructor
Self.X := AValX;
end;
```
Contributor guide
Assessment
This issue has not been assessed yet.