CleanCodeNet 0.3.5

There is a newer version of this package available.
See the version list below for details.
dotnet add package CleanCodeNet --version 0.3.5                
NuGet\Install-Package CleanCodeNet -Version 0.3.5                
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="CleanCodeNet" Version="0.3.5" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add CleanCodeNet --version 0.3.5                
#r "nuget: CleanCodeNet, 0.3.5"                
#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.
// Install CleanCodeNet as a Cake Addin
#addin nuget:?package=CleanCodeNet&version=0.3.5

// Install CleanCodeNet as a Cake Tool
#tool nuget:?package=CleanCodeNet&version=0.3.5                

Clean Code .NET

Clean Code .NET is a set of Roslyn analyzers aimed to help developers build more correct and readable code. At this moment it contains 3 analyzers (hope to improve their count in future):

Switch analyzer

Ensures that all possible cases are covered in switch statement for enums and pattern matching. Consider example:

enum TestEnum
{
    Case1,
    Case2,
    Case3
}
    
switch (TestEnum.Case1)
{
    case Test.TestEnum.Case1: { break; }
    case Test.TestEnum.Case2: { break; }
    default: throw new NotImplementedException();
}

Will give a warning, because:

  1. Intentionally specified that default case is not a normal case (throws exception).
  2. Not all possible cases are covered (TestEnum.Case3). It will allow developer to not miss handling of newly added enum values.

Constructor null analyzer

Checks if constructor requires all reference type parameters to be not null and provide fixers for it:

public Program(string test1, object test2, TestStruct<string> test3, string test4)
{
}

This example will be transformed into:

public Program(string test1, object test2, TestStruct<string> test3, string test4)
{
    if (test1 == null)
        throw new ArgumentNullException(nameof(test1));
    if (test2 == null)
        throw new ArgumentNullException(nameof(test2));
    if (test4 == null)
        throw new ArgumentNullException(nameof(test4));
}

Various options are provided: if-check, if + assignment coalesce (?? throw new ArgumentNullException(nameof(param))) where possible and Contract.Requires

Named parameters analyzer

Ensures if method/constructor calls has 4 or more parameters to have parameter names. For example:

Method("Foo", "Bar", "Baz", "Qux");

Should be:

Method(foo: "Foo", bar: "Bar", baz: "Baz", qux: "Qux");

Exceptions analyzer

Catches common antipatterns of exceptions handling

catch(Exception e)
{
    throw e; // Leads to lost stack trace
    // Should be:
    throw;
}
catch(Exception e)
{
    // Swallow exception without any handling. Should be at least some exception usage
}
catch(NullReferenceException e)
{
    throw new Exception("Null reference exception"); // Rethrow without inner exception.
    // Should be:
    throw new Exception("Null reference exception", e);
}

How to use

There are 2 options:

How to select analyzers in Visual Studio extension

Tools → Options... → Clean Code .NET

Options window

How to configure analyzers referenced as NuGet packages

Solution explorer → {Project} → Dependencies → Analyzers → Right click on analyzers → Set Rule Set Severity

You will have new file {ProjectName}.ruleset opened in editor Options window

For more information about rulesets you can go to https://docs.microsoft.com/en-us/visualstudio/code-quality/use-roslyn-analyzers?view=vs-2017

There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.3.6 708 7/19/2019
0.3.5 639 12/20/2018
0.3.4 661 12/20/2018
0.3.3 640 12/19/2018
0.3.2 638 12/19/2018
0.3.1 777 9/17/2018
0.3.0 752 9/17/2018
0.1.2 737 9/12/2018
0.1.1 749 9/11/2018
0.1.0 742 9/7/2018

Align diagnostics ids in analyzers.