-
Notifications
You must be signed in to change notification settings - Fork 1
Add Item 12: Prefer Member Initializers to Assignment Statements #63
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 all commits
c06ff97
55ca307
4dd9f2b
aaae648
74e8931
31cd29b
8f352f7
cc88272
cba51c9
12662a8
d886813
b07c606
b877bd3
3761ec5
52b2850
58d7878
b678fdb
ef99b90
ceaa593
d2de257
dde05f8
d6a903b
570bc93
e3cdf69
0b13830
97c51a4
2c031a2
85b9dfc
10cd64a
addd00a
1d08ebd
25295cf
72dd42e
bffb223
14a8a08
c6cc54e
aa6c00b
b876bad
b7597af
431c5df
df4f185
6d8dd69
0d16a21
a3d8d98
afd9108
c31c0c8
54b7fc4
a738a84
af68eff
5800482
24d3aae
984e3cc
e91a232
26b5491
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,89 @@ | ||||||||||||||||||||||
# ECS1200: Prefer member initializers to assignment statements | ||||||||||||||||||||||
|
||||||||||||||||||||||
This rule is described in detail in [Effective C#: 50 Specific Ways to Improve your C#](https://www.oreilly.com/library/view/effective-c-50/9780134579290/). | ||||||||||||||||||||||
|
||||||||||||||||||||||
## Cause | ||||||||||||||||||||||
|
||||||||||||||||||||||
The rule identifies fields in a type that are being initialized both at the point of declaration and again within a constructor. This can lead to redundancy and potential confusion, as the field is unnecessarily re-initialized, which could also introduce bugs if the constructor logic changes. | ||||||||||||||||||||||
|
||||||||||||||||||||||
## Rule description | ||||||||||||||||||||||
|
||||||||||||||||||||||
Fields should be initialized at the point of declaration rather than within constructors when the value being assigned does not depend on any constructor parameters or instance members. This ensures consistency, reduces redundancy, and avoids potential issues with fields being out of sync. | ||||||||||||||||||||||
|
||||||||||||||||||||||
The rule will trigger when it detects that a field is initialized both at the declaration and again within a constructor with the same or a redundant value. It encourages developers to consolidate this initialization into a single place—__the field declaration__—whenever possible. | ||||||||||||||||||||||
|
||||||||||||||||||||||
## How to fix violations | ||||||||||||||||||||||
|
||||||||||||||||||||||
To fix violations, remove the redundant initialization from the constructor if the field is already initialized at the point of declaration with the same value. If the initialization must depend on constructor logic, ensure that it's not duplicated unnecessarily. | ||||||||||||||||||||||
|
||||||||||||||||||||||
## When to suppress warnings | ||||||||||||||||||||||
|
||||||||||||||||||||||
It is always safe to suppress the warning, as it highlights potentially unnecessary code and work that may be avoided. | ||||||||||||||||||||||
|
||||||||||||||||||||||
### Suppress a warning | ||||||||||||||||||||||
|
||||||||||||||||||||||
If the initialization in the constructor is necessary due to specific requirements or conditions, you might want to suppress the warning. | ||||||||||||||||||||||
|
||||||||||||||||||||||
#pragma warning disable ECS1200 | ||||||||||||||||||||||
// The code that's violating the rule | ||||||||||||||||||||||
#pragma warning restore ECS1200 | ||||||||||||||||||||||
|
||||||||||||||||||||||
To disable the rule for a file, folder, or project, set its severity to none in the [configuration file](https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/configuration-files). | ||||||||||||||||||||||
|
||||||||||||||||||||||
```ini | ||||||||||||||||||||||
[*.cs] | ||||||||||||||||||||||
dotnet_diagnostic.ECS1200.severity = none | ||||||||||||||||||||||
``` | ||||||||||||||||||||||
|
||||||||||||||||||||||
## Example of a violation | ||||||||||||||||||||||
|
||||||||||||||||||||||
### Description | ||||||||||||||||||||||
|
||||||||||||||||||||||
The following example shows a violation where a field `listOfString` is initialized both at its declaration and within the default constructor. | ||||||||||||||||||||||
|
||||||||||||||||||||||
### Code | ||||||||||||||||||||||
|
||||||||||||||||||||||
```csharp | ||||||||||||||||||||||
public class MyClass | ||||||||||||||||||||||
{ | ||||||||||||||||||||||
private List<string> listOfString = new List<string>(); | ||||||||||||||||||||||
|
||||||||||||||||||||||
public MyClass() | ||||||||||||||||||||||
{ | ||||||||||||||||||||||
listOfString = new List<string>(); | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
public MyClass(int size) | ||||||||||||||||||||||
{ | ||||||||||||||||||||||
listOfString = new List<string>(size); | ||||||||||||||||||||||
} | ||||||||||||||||||||||
} | ||||||||||||||||||||||
``` | ||||||||||||||||||||||
|
||||||||||||||||||||||
## Example of how to fix | ||||||||||||||||||||||
|
||||||||||||||||||||||
### Description | ||||||||||||||||||||||
|
||||||||||||||||||||||
To fix the violation, remove the redundant initialization from the default constructor, as the field `listOfString` is already initialized with the same value at the point of declaration. | ||||||||||||||||||||||
|
||||||||||||||||||||||
### Code | ||||||||||||||||||||||
Comment on lines
+65
to
+69
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. Avoid using the same content for multiple headings. The static analysis tool suggests avoiding using the same content for multiple headings, as it may be confusing for readers. Consider using different content for the headings to make them more descriptive and distinguishable. For example: -### Description
+### Violation Example
The following example shows a violation where a field `listOfString` is initialized both at its declaration and within the default constructor.
-### Code
+### Violation Code Committable suggestion
Suggested change
ToolsGitHub Check: Codacy Static Code Analysis
|
||||||||||||||||||||||
|
||||||||||||||||||||||
```csharp | ||||||||||||||||||||||
public class MyClass | ||||||||||||||||||||||
{ | ||||||||||||||||||||||
private List<string> listOfString = new List<string>(); | ||||||||||||||||||||||
|
||||||||||||||||||||||
public MyClass() | ||||||||||||||||||||||
{ | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
public MyClass(int size) | ||||||||||||||||||||||
{ | ||||||||||||||||||||||
listOfString = new List<string>(size); | ||||||||||||||||||||||
} | ||||||||||||||||||||||
} | ||||||||||||||||||||||
``` | ||||||||||||||||||||||
|
||||||||||||||||||||||
## Related Rules | ||||||||||||||||||||||
|
||||||||||||||||||||||
[CA1805: Do not initialize unnecessarily](https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca1805) | ||||||||||||||||||||||
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. Add a single newline character at the end of the file. The static analysis tool suggests adding a single newline character at the end of the file to follow the Markdown formatting conventions. Add a single newline character at the end of the file. ToolsMarkdownlint
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,4 +6,4 @@ | |
"msbuild-sdks": { | ||
"DotNet.ReproducibleBuilds.Isolated": "1.2.4" | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,8 @@ | ||
; Unshipped analyzer release | ||
; https://github.com/dotnet/roslyn-analyzers/blob/main/src/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md | ||
|
||
### New Rules | ||
|
||
Rule ID | Category | Severity | Notes | ||
--------|----------|----------|------- | ||
ECS1200 | Maintainability | Info | PreferMemberInitializerAnalyzer, [Documentation](https://github.com/rjmurillo/EffectiveCSharp.Analyzers/blob/97c51a41b53d059cfcd06f0c8ce5ab178b070e6b/docs/rules/ECS1200.md) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a space after the hash symbol in the ATX-style headings.
The static analysis tools suggest adding a space after the hash symbol in the ATX-style headings at lines 27 and 29 to follow the correct Markdown formatting.
Apply this diff to fix the formatting issue:
Tools
Markdownlint
GitHub Check: Codacy Static Code Analysis