-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
doc: document AvoidEnumParametersAnalyzer
- Loading branch information
Showing
4 changed files
with
124 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
# PH2138: Avoid enum parameters | ||
|
||
| Property | Value | | ||
|--|--| | ||
| Package | [Philips.CodeAnalysis.MaintainabilityAnalyzers](https://www.nuget.org/packages/Philips.CodeAnalysis.MaintainabilityAnalyzers) | | ||
| Diagnostic ID | PH2140 | | ||
| Category | [Functional Programming](../FunctionalProgramming.md) | | ||
| Analyzer | [AvoidEnumParametersAnalyzer](https://github.com/philips-software/roslyn-analyzers/blob/main/Philips.CodeAnalysis.MaintainabilityAnalyzers/Cardinality/AvoidEnumParametersAnalyzer.cs) | ||
| CodeFix | No | | ||
| Severity | Error | | ||
| Enabled By Default | No | | ||
|
||
## Introduction | ||
|
||
This analyzer warns about methods or functions that have enums as parameters. Whenever a method takes in a known subset of values there is an opportunity to invert the control flow and reduce the complexity of the call site. | ||
|
||
Often times the method does not apply to all values in the enum, but only a subset of them. In these cases it is better to define the method only for those values. | ||
|
||
Otherwise, when all values are addressable it is often the case each individual value has its own particular set of rules it follows. In these cases it is better to isolate each different ruleset to its relevant enum value. | ||
|
||
## How to solve | ||
|
||
Upgrade the enum to a record and have each value implement its own particular version. | ||
If that is not possible, create an extension method that performs the relevant pattern matching. | ||
See this [blog post](https://spencerfarley.com/2021/03/26/unions-in-csharp/) for some more details. | ||
|
||
## Example | ||
|
||
Code that triggeres the diagnostic: | ||
```cs | ||
class MyClass | ||
{ | ||
public ResultCode processFilename(String filename) | ||
{ | ||
var envConfig = Config.FromEnvironment(); | ||
return processSysConfig(filename, envConfig); | ||
} | ||
public ResultCode processSysConfig(String filename, ConfigEnum config) | ||
{ | ||
if (config == ReadOnUpdate) | ||
{ | ||
... | ||
} | ||
else if (config == ReadWeekly || config == ReadMonthly) | ||
{ | ||
... | ||
} | ||
else | ||
{ | ||
... | ||
} | ||
} | ||
} | ||
|
||
enum ConfigEnum { | ||
ReadOnUpdate, ReadMonthly, ReadWeekly, ReadNever | ||
} | ||
``` | ||
|
||
And the corrected code: | ||
```cs | ||
class MyClass | ||
{ | ||
public ResultCode processFilename(String filename) | ||
{ | ||
var envConfig = Config.FromEnvironment(); | ||
return envConfig.process(filename); | ||
} | ||
|
||
} | ||
|
||
public abstract record ConfigRecord | ||
{ | ||
public abstract long ProcessFile(string filename); | ||
|
||
public record ReadOnUpdate() : ConfigRecord() | ||
{ | ||
public override long ProcessFile(string filename) { ... } | ||
}; | ||
|
||
public record ReadWeekly() : ConfigRecord() | ||
{ | ||
public override long ProcessFile(string filename) { ... } | ||
} | ||
|
||
public record ReadMonthly() : ConfigRecord() | ||
{ | ||
public override long ProcessFile(string filename) { ... } | ||
} | ||
|
||
public record ReadNever() : ConfigRecord() { | ||
public override long ProcessFile(string filename) { ... } | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters