|
| 1 | +# ECS1400: Minimize duplicate initialization logic |
| 2 | + |
| 3 | +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/). |
| 4 | + |
| 5 | +## Cause |
| 6 | + |
| 7 | +Constructors within a class contain duplicate initialization logic, leading to code duplication and potential maintenance issues. |
| 8 | + |
| 9 | +## Rule Description |
| 10 | + |
| 11 | +This rule identifies constructors in a class that perform the same or similar initialization steps independently, rather than delegating to a common constructor. Duplicate initialization logic can make the code harder to maintain and increase the risk of bugs when changes are made. By minimizing duplicate initialization logic through constructor chaining, you centralize the initialization code, making it easier to manage and update. |
| 12 | + |
| 13 | +## How to fix violations |
| 14 | + |
| 15 | +Refactor the constructors to use constructor chaining. Identify a primary constructor that contains the shared initialization logic, and have other constructors delegate to it using the `this()` syntax. This ensures that all initialization steps are centralized in one place. |
| 16 | + |
| 17 | +## When to suppress warnings |
| 18 | + |
| 19 | +You might suppress this warning if: |
| 20 | + |
| 21 | +- Constructor chaining is not feasible due to specific requirements or design constraints. |
| 22 | +- The initialization logic, while similar, needs to be executed separately in each constructor for correctness. |
| 23 | +- Performance considerations prevent the use of constructor chaining in a particular scenario. |
| 24 | + |
| 25 | +### Suppress a warning |
| 26 | + |
| 27 | +If you just want to suppress a single violation, add preprocessor directives to your source file to disable and then re-enable the rule. |
| 28 | + |
| 29 | +```csharp |
| 30 | +#pragma warning disable ECS1400 |
| 31 | +// The code that's violating the rule |
| 32 | +#pragma warning restore ECS1400 |
| 33 | +``` |
| 34 | + |
| 35 | +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). |
| 36 | + |
| 37 | +```ini |
| 38 | +[*.cs] |
| 39 | +dotnet_diagnostic.ECS1400.severity = none |
| 40 | +``` |
| 41 | + |
| 42 | +## Example of a violation |
| 43 | + |
| 44 | +### Description |
| 45 | + |
| 46 | +In this example, the `MyClass` class has multiple constructors that contain duplicate initialization logic. Each constructor initializes the `_value` field independently with the same value. |
| 47 | + |
| 48 | +### Code |
| 49 | + |
| 50 | +```csharp |
| 51 | +public class MyClass |
| 52 | +{ |
| 53 | + private int _value; |
| 54 | + |
| 55 | + public MyClass() |
| 56 | + { |
| 57 | + _value = 10; |
| 58 | + } |
| 59 | + |
| 60 | + public MyClass(int value) |
| 61 | + { |
| 62 | + _value = value; |
| 63 | + } |
| 64 | +} |
| 65 | +``` |
| 66 | + |
| 67 | +## Example of how to fix |
| 68 | + |
| 69 | +### Description |
| 70 | + |
| 71 | +Refactor the constructors to use constructor chaining. Designate one constructor as the primary initializer (e.g., the one accepting parameters), and have other constructors delegate to it using the `this()` syntax. |
| 72 | + |
| 73 | +### Code |
| 74 | + |
| 75 | +```csharp |
| 76 | +public class MyClass |
| 77 | +{ |
| 78 | + private int _value; |
| 79 | + |
| 80 | + public MyClass() : this(10) |
| 81 | + { |
| 82 | + } |
| 83 | + |
| 84 | + public MyClass(int value) |
| 85 | + { |
| 86 | + _value = value; |
| 87 | + } |
| 88 | +} |
| 89 | +``` |
0 commit comments