Arnolyzer: adding clean-code static analysis to C# in VS2015

arnolyzer-logoArnolyzer is a Roslyn-based analyzer for Visual Studio 2015 that provides a set of compiler rules that encourage modern, functional-orientated, coding standards in C# 6. Pure functions; no inheritance; no global state; immutable data classes and variables; and short, concise sections of code.

So how and why does Arnolyzer come to exist? Let’s roll back the clock a bit to life before C# 6 and the Roslyn compiler. There existed a tool called StyleCop. Its idea was a good one: automate the checking of coding standards. Its execution though was poor: it focused on the mundane (eg checking correct spacing around keywords and symbols) and the downright bad, such as insisting one clutter a source file was distracting noise. Noise like mandating the superfluous use of this. and the requirement to create long-winded XML-based comments for properties, resulting in nonsense like:

Fast forward to the summer of 2015 and Roslyn effectively killed off StyleCop. It introduced a new way of writing analyzers using the compiler. A number of analyzer projects exist, but the best known one disappointingly just replicates the rules of StyleCop. Yet Roslyn offers the opportunity to support such so much more, and thus Arnolyzer was born.

Thus far, the following analyzers have been implemented:

  • ClassPropertyShouldBePubliclyReadOnly
  • DoNotUseNotImplementedException
  • DoNotUseNotSupportedException
  • FileMustOnlyContainOneTypeDefinition
  • InnerTypesMustBePrivate
  • InterfacePropertiesShouldBeReadonly
  • MethodParameterMustNotBeRefOrOut
  • MethodShouldNotContainAnd
  • StaticMethodMustHaveAtLeastOneParameter
  • StaticMethodMustNotBeVoid

There is lot’s more planned though. The following analyzers are planned, but haven’t yet been implemented:

  • ClassesMustBeSealed
  • DoNotUseAbstractClasses
  • DoNotUseClassInheritance
  • DoNotUseStaticFields
  • DoNotUseStaticProperties
  • FileTooLong
  • MethodTooLong
  • StaticMethodMustNotAccessState
  • StaticMethodMustNotCreateState
  • ParameterShouldNotBeModified
  • VariableShouldBeAssignedOnce

Also, many of these analyzers would benefit from code fixers and ways of marking code as being an exception to the rule. Also, Arnolyzer supports a YAML-based configuration file, but only a couple of the analyzers pay attention to it and it’s not documented yet.

There’s lots to do and I anticipate lots of updates over the next few months, but it’s definitely ready to use if you are interested. You can read more about the individual analyzers on Github and you can install the package by searching for Arnolyzer on nuget.