|
| 1 | +[](https://www.nuget.org/packages/BoolParameterGenerator/) |
| 2 | +[](https://github.com/9swampy/BoolEnumGenerator/actions/workflows/nuget-publish.yml) |
| 3 | +# BoolParameterGenerator |
| 4 | + |
| 5 | +**BoolParameterGenerator** is a Roslyn analyzer and source generator that automatically creates replacement types for boolean parameters in C# code. This improves readability and maintainability by replacing ambiguous `bool` parameters with strongly typed, descriptive alternatives. |
| 6 | + |
| 7 | +## 🚫 Analyzer Rules Discouraging `bool` Parameters |
| 8 | + |
| 9 | +Using raw `bool` parameters in method signatures is often discouraged because it reduces code readability and clarity. Calls like `SetFeature(true)` can be ambiguous without context, making the code harder to understand and maintain. |
| 10 | + |
| 11 | +### Motivations for Avoiding `bool` Parameters |
| 12 | + |
| 13 | +- **Improved readability:** Boolean parameters often obscure the intent of the method call. |
| 14 | +- **Explicit intent:** Descriptive enums or strong types clarify the purpose. |
| 15 | +- **Better API discoverability:** Strongly typed parameters enhance IntelliSense and documentation. |
| 16 | +- **Easier maintenance:** Clearer code reduces bugs and onboarding time. |
| 17 | +- **Extensibility:** Enums or wrappers allow for additional states beyond simple true/false. |
| 18 | + |
| 19 | +### Popular Analyzers and Their Rules |
| 20 | + |
| 21 | +- **SonarAnalyzer (SonarLint / SonarQube)** |
| 22 | + Rule: [S1133 - Remove boolean parameters](https://rules.sonarsource.com/csharp/RSPEC-1133) |
| 23 | + Flags methods with boolean parameters to encourage more meaningful alternatives. |
| 24 | + |
| 25 | +- **Roslynator** |
| 26 | + Rule: [RCS1155 - Avoid boolean parameters in methods](https://github.com/JosefPihrt/Roslynator/blob/master/docs/analyzers/RCS1155.md) |
| 27 | + Suggests replacing boolean parameters with separate methods or enums for better readability. |
| 28 | + |
| 29 | +- **StyleCop Analyzers** |
| 30 | + While no specific rule bans boolean parameters, StyleCop encourages clear, descriptive API design that indirectly discourages ambiguous booleans. |
| 31 | + |
| 32 | +### How BoolParameterGenerator Addresses These Issues |
| 33 | + |
| 34 | +- Generates strongly typed, descriptive replacements for `bool` parameters. |
| 35 | +- Improves code clarity, intent, and discoverability. |
| 36 | +- Enables future extensibility beyond binary states. |
| 37 | +- Helps maintain cleaner and more maintainable APIs. |
| 38 | + |
| 39 | +--- |
| 40 | + |
| 41 | +## ✨ Features |
| 42 | + |
| 43 | +- Replaces `bool` parameters with source-generated binary types. |
| 44 | +- Supports generation of: |
| 45 | + - Binary enums |
| 46 | + - Struct-backed bool wrappers |
| 47 | +- Seamless integration with IntelliSense and analyzers. |
| 48 | +- Minimal configuration required. |
| 49 | + |
| 50 | +--- |
| 51 | + |
| 52 | +## 📦 Installation |
| 53 | + |
| 54 | +Install the main analyzer package via NuGet: |
| 55 | + |
| 56 | +```xml |
| 57 | +<PackageReference Include="BoolParameterGenerator" Version="1.0.0" /> |
| 58 | +``` |
| 59 | + |
| 60 | +This will **transitively install** the required helper package `BoolParameterGenerator.Shared`. |
| 61 | + |
| 62 | +✅ Works in: |
| 63 | +- .NET SDK-style projects |
| 64 | +- Class libraries |
| 65 | +- Console apps |
| 66 | +- Unit test projects |
| 67 | + |
| 68 | +--- |
| 69 | + |
| 70 | +## 🚀 Usage |
| 71 | + |
| 72 | +Annotate a `partial class` with one of the supported generator attributes: |
| 73 | + |
| 74 | +```csharp |
| 75 | +using PrimS.BoolParameterGenerator; |
| 76 | + |
| 77 | +[GenerateBinaryEnum("TrueValue", "FalseValue")] |
| 78 | +public partial class MyBinaryEnum { } |
| 79 | + |
| 80 | +[GenerateBoolEnum("TrueValue", "FalseValue")] |
| 81 | +public partial class MyBoolEnum { } |
| 82 | +``` |
| 83 | + |
| 84 | +🔧 Requirements: |
| 85 | +- The class **must** be `partial`. |
| 86 | +- The attribute arguments define the **true/false** semantics of the generated type. |
| 87 | + |
| 88 | +--- |
| 89 | + |
| 90 | +## 📚 Example Usage and Guidance |
| 91 | + |
| 92 | +For detailed examples illustrating the benefits of BoolParameterGenerator, see the following: |
| 93 | + |
| 94 | +- **Good Examples: Caller IntelliSense** — [CallerIntellisenseGoodExamples.cs](./BoolParameterGenerator.Github.Example/CallerIntellisenseGoodExamples.cs) |
| 95 | +- **Good Examples: Implementation Patterns** — [ImplementationGoodExamples.cs](./BoolParameterGenerator.Github.Example/ImplementationGoodExamples.cs) |
| 96 | +- **Bad Examples: Caller IntelliSense Pitfalls** — [CallerIntellisenseBadExamples.cs](./BoolParameterGenerator.Github.Example/CallerIntellisenseBadExamples.cs) |
| 97 | +- **Bad Examples: Implementation Pitfalls** — [ImplementationBadExamples.cs](./BoolParameterGenerator.Github.Example/ImplementationBadExamples.cs) |
| 98 | + |
| 99 | +These demonstrate why replacing raw `bool` parameters with strongly typed proxies enhances readability, API clarity, and maintainability. |
| 100 | + |
| 101 | +## 🔍 Where to Find Generated Code |
| 102 | + |
| 103 | +1. Open your project in **Visual Studio**. |
| 104 | +2. Navigate to `Dependencies > Analyzers > BoolParameterGenerator`. |
| 105 | +3. Expand the node to find the generated `.g.cs` files (e.g., `MyBinaryEnum.g.cs`). |
| 106 | + |
| 107 | +⚠️ If only `Heartbeat.g.cs` appears: |
| 108 | +- Ensure your partial class is declared correctly. |
| 109 | +- Verify that attribute arguments are valid. |
| 110 | +- Rebuild the project to trigger generation. |
| 111 | + |
| 112 | +--- |
| 113 | + |
| 114 | +## 📦 About the Shared Package |
| 115 | + |
| 116 | +Although the attributes (`GenerateBinaryEnum`, `GenerateBoolEnum`) are defined in a separate package `BoolParameterGenerator.Shared`, you do **not** need to reference it manually — it is installed transitively. |
| 117 | + |
| 118 | +There isn't much to choose one type attribute over the other atm. Under the hood the implementation is quite different and we expect the BinaryEnum could prove advantageous; especially with respect to extending to a tri-state "boolean". This is a Work-In-Progres and we would be very happy to receive feedback on useCases that may deviate in interesting ways from our own expectations... |
| 119 | + |
| 120 | +--- |
| 121 | + |
| 122 | +## 🧪 Confirmed Working Build/Contribution Setup |
| 123 | + |
| 124 | +Check out the latest master branch then validate everything is wired correctly: |
| 125 | +1. Open only the generator projects (`BoolParameterGenerator` and `BoolParameterGenerator.Shared`) and the test project (`BoolParameterGenerator.Pack.Tests`). |
| 126 | +2. Clean the solution. |
| 127 | +3. Rebuild `BoolParameterGenerator.Pack.Tests`. |
| 128 | +4. If BoolParameterGenerator analyzer references appear unresolved, try opening the file — often Visual Studio will resolve them automatically when the file is activated. |
| 129 | +5. Rebuild again if necessary |
| 130 | +1. You'll hopefully see `BEG004` diagnostics. |
| 131 | + |
| 132 | +--- |
| 133 | + |
| 134 | +## ⚠️ Namespace Caveat |
| 135 | + |
| 136 | +If the triggering class and the generated class are in **different namespaces**, generation may fail silently. Ensure the partial class declaration and the generated file reside in the same namespace, or adjust your generator logic to support custom namespaces. |
| 137 | + |
| 138 | +--- |
| 139 | + |
| 140 | +## 📄 License |
| 141 | + |
| 142 | +MIT — essentially use however you like, just don't sue me if it doesn't work out! |
| 143 | + |
| 144 | +--- |
| 145 | + |
| 146 | +## 🧵 See Also |
| 147 | + |
| 148 | +- [BoolParameterGenerator GitHub Repo](https://github.com/9swampy/BoolEnumGenerator) |
| 149 | +- [Source Generator Cookbook (Roslyn)](https://github.com/dotnet/roslyn/blob/main/docs/features/source-generators.cookbook.md) |
0 commit comments