21 Apr 2015

If you want to commit to the .NET open source code you need to follow the coding style that the teams use and to assist in that there is a helpful tool you can run over your code called Code Formatter. It is a simple command line tool which you can run over your code to fix up the style.

Clipboard01

Ultimately I think this is a great tool and something that can easily be added into your build process to keep the code clean. I would like to see someone like Steve Cadwallader amazing CodeMaid start to introduce this tool so it can just be part of Visual Studio.

A quick test

I decide to take it for a spin over my array fighter code, since it is a fairly messy piece of quick code I wrote,  to see what would change and it is was really minor.  First up was that copyright headers were added and I did play with that to tweak the header a bit, which is a great option.

image

Second up was that all my items had accessors placed on them and my fields were changed from starting with a capital to starting with a lowercase & _.

image

A bigger test

For a second test I ran it over a HUGE project which is frankly a disaster, and it took less than 10 seconds to handle just a few thousand files. Some things I saw there were the removal of blank lines:

image

Changing the copyright symbol to a unicode number

image

Static fields were prefixed with s_

image

Addition of spaces in auto-properties and between member names and getters/setters

image

image

I don’t personally like all of these, but they make sense and it is configurable.

Looking at the code

Since this is an open source tool we can look through the code for some interesting bits of info. the first area I looked at was the unit tests, since it is all unit tested using XUnit and I really like how they have crafted the tests. Makes a lot of sense and is simple to read & work with.

image

Nice to see someone using the System.ComponentModel.Composition for a lightweight DI framework, since it has been spoken about so often but I never see anyone use it. The most interesting part though is that this is built with Roslyn and it is amazing how easy it is to read that code. For example here is the code to insert the copyright header in 20ish lines of code:

image

if you are looking for Roslyn examples, this is a great place to start.

Dead Regions

“Hidden” inside the solution is a second tool named Dead Regions, which is used to find and potentially edit conditional compiler blocks of code which are always TRUE/FALSE. It is interesting, but I am not sure it has as much broad use as the formatter. Checkout the readme.md for good write up on it.

Tags: 

Comments

Tyler's picture

Thanks for this!

Add new comment