Few days ago I decided to release my CodeWatch library. It’s the first version to show the concept.
Why is this useful
In the .NET world we have a couple of tools for keeping code quality. FxCop, NDepend, R# or SourceMonitor are those most known. In addition we have a number of unit test frameworks like NUnit or xUnit. All of them are really useful when used properly.
Unfortunately due to various reasons there is still a gap. For example:
- Developer running VS Code or other alternative editor like OmniSharp does not have benefits of FxCop or R#.
- It’s hard to keep Coding Convention Guidelines defined for particular project aligned with the code in (sometimes long) development process.
- Addressing all issues found by NDepend or SourceMonitor may become very cumbersome and time consuming.
- Doing code reviews and reporting something that could be automatically reported is a waste of time. Read my post on automation
My intention behind writing CodeWatch was to mitigate and preferably remove the issues above :)
What is it
CodeWatch is a NuGet package meant to keep your code following defined code conventions.
At the moment it can do 3 things:
- Check fields naming (upper or lower case)
- Check properties naming (upper or lower case)
- Check if exceptions are handled in a try/catch block
Sample usage
Here is sample code to demonstrate how the dll works:
//Our production code
public class Test
{
public int myProp {get;set;}
public void MyMethodThrowingException()
{
try
{
throw new Exception("Error!");
}
catch
{
//don't do nothing
}
}
}
//CodeQuality guard code (in tests dll)
using NUnit.Framework;
[TestFixture]
public class CodeQualityWatcher
{
[Test]
public void CheckThatAllPropertiesAreUppercase()
{
PropertyNamingWatcher watcher = new PropertyNamingWatcher();
//Add types (or assemblies with WatchAssembly) to watch
watcher.WatchType(typeof(Test));
//Execute check
watcher.Execute(); //This will throw exception because we expect Uppercase property names and Test class has myProp
}
[Test]
public void CheckThatAllTryCatchBlockHandleExceptions()
{
ExceptionHandlingWatcher watcher = new ExceptionHandlingWatcher();
//Add types (or assemblies with WatchAssembly) to watch
watcher.WatchType(typeof(Test));
//Execute check
watcher.Execute(); //This will throw exception because we don't handle exception in MyMethodThrowingException method
}
}
Sample project is also available here.
How to get it
Here is its NuGet gallery page. All sources are available on GitHub. More to come.