BoolParameterGenerator.Shared 0.4.0

dotnet add package BoolParameterGenerator.Shared --version 0.4.0
                    
NuGet\Install-Package BoolParameterGenerator.Shared -Version 0.4.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="BoolParameterGenerator.Shared" Version="0.4.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="BoolParameterGenerator.Shared" Version="0.4.0" />
                    
Directory.Packages.props
<PackageReference Include="BoolParameterGenerator.Shared" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add BoolParameterGenerator.Shared --version 0.4.0
                    
#r "nuget: BoolParameterGenerator.Shared, 0.4.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#addin nuget:?package=BoolParameterGenerator.Shared&version=0.4.0
                    
Install BoolParameterGenerator.Shared as a Cake Addin
#tool nuget:?package=BoolParameterGenerator.Shared&version=0.4.0
                    
Install BoolParameterGenerator.Shared as a Cake Tool

NuGet Build Status

BoolParameterGenerator

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.

๐Ÿšซ Analyzer Rules Discouraging bool Parameters

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.

Motivations for Avoiding bool Parameters

  • Improved readability: Boolean parameters often obscure the intent of the method call.
  • Explicit intent: Descriptive enums or strong types clarify the purpose.
  • Better API discoverability: Strongly typed parameters enhance IntelliSense and documentation.
  • Easier maintenance: Clearer code reduces bugs and onboarding time.
  • Extensibility: Enums or wrappers allow for additional states beyond simple true/false.
  • SonarAnalyzer (SonarLint / SonarQube)
    Rule: S1133 - Remove boolean parameters
    Flags methods with boolean parameters to encourage more meaningful alternatives.

  • Roslynator
    Rule: RCS1155 - Avoid boolean parameters in methods
    Suggests replacing boolean parameters with separate methods or enums for better readability.

  • StyleCop Analyzers
    While no specific rule bans boolean parameters, StyleCop encourages clear, descriptive API design that indirectly discourages ambiguous booleans.

How BoolParameterGenerator Addresses These Issues

  • Generates strongly typed, descriptive replacements for bool parameters.
  • Improves code clarity, intent, and discoverability.
  • Enables future extensibility beyond binary states.
  • Helps maintain cleaner and more maintainable APIs.

โœจ Features

  • Replaces bool parameters with source-generated binary types.
  • Supports generation of:
    • Binary enums
    • Struct-backed bool wrappers
  • Seamless integration with IntelliSense and analyzers.
  • Minimal configuration required.

๐Ÿ“ฆ Installation

Install the main analyzer package via NuGet:

<PackageReference Include="BoolParameterGenerator" Version="1.0.0" />

This will transitively install the required helper package BoolParameterGenerator.Shared.

โœ… Works in:

  • .NET SDK-style projects
  • Class libraries
  • Console apps
  • Unit test projects

๐Ÿš€ Usage

Annotate a partial class with one of the supported generator attributes:

using PrimS.BoolParameterGenerator;

[GenerateBinaryEnum("TrueValue", "FalseValue")]
public partial class MyBinaryEnum { }

[GenerateBoolEnum("TrueValue", "FalseValue")]
public partial class MyBoolEnum { }

๐Ÿ”ง Requirements:

  • The class must be partial.
  • The attribute arguments define the true/false semantics of the generated type.

๐Ÿ“š Example Usage and Guidance

For detailed examples illustrating the benefits of BoolParameterGenerator, see the following:

These demonstrate why replacing raw bool parameters with strongly typed proxies enhances readability, API clarity, and maintainability.

๐Ÿ” Where to Find Generated Code

  1. Open your project in Visual Studio.
  2. Navigate to Dependencies > Analyzers > BoolParameterGenerator.
  3. Expand the node to find the generated .g.cs files (e.g., MyBinaryEnum.g.cs).

โš ๏ธ If only Heartbeat.g.cs appears:

  • Ensure your partial class is declared correctly.
  • Verify that attribute arguments are valid.
  • Rebuild the project to trigger generation.

๐Ÿ“ฆ About the Shared Package

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.

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...


๐Ÿงช Confirmed Working Build/Contribution Setup

Check out the latest master branch then validate everything is wired correctly:

  1. Open only the generator projects (BoolParameterGenerator and BoolParameterGenerator.Shared) and the test project (BoolParameterGenerator.Pack.Tests).
  2. Clean the solution.
  3. Rebuild BoolParameterGenerator.Pack.Tests.
  4. If BoolParameterGenerator analyzer references appear unresolved, try opening the file โ€” often Visual Studio will resolve them automatically when the file is activated.
  5. Rebuild again if necessary
  6. You'll hopefully see BEG004 diagnostics.

โš ๏ธ Namespace Caveat

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.


๐Ÿ“„ License

MIT โ€” essentially use however you like, just don't sue me if it doesn't work out!


๐Ÿงต See Also

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on BoolParameterGenerator.Shared:

Package Downloads
BoolParameterGenerator

BoolParameterGenerator is a Roslyn analyzer and source generator that automatically creates replacement types for boolean parameters in C# code. This improves code readability and maintainability by replacing ambiguous bools with descriptive types. See the README for usage and examples.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.4.0 115 5/28/2025
0.3.1 111 5/28/2025