Enforcing Coding Standards

One of the things that I’m working on as part of my new job is the creation of a Coding Standards document.  This is an interesting prospect as the software has already been developed for a significant number of months, resulting in a sizable existing codebase.  Writing a coding standards document in this situation is not about creating a fresh new way of doing things, but instead it involves a lot of code review and deciding what have the unwritten defacto standards been thus far.

Part of the reason for creating this document is to empower the developers to enforce their own consistency.  One of the other reasons is to have a document that the Team Leads can point to when they’re performing code reviews and enforcing standards.  In my experience, following naming conventions and other coding standards can become second nature, but everyone has moments where they just slip up and a standard gets broken.  What I wanted to be able to do for our developers was offer them an easy, unobtrusive way to review their standards adherence.  I think I found it.

I was out on and about on the web and I stumbled across Joel Fjorden’s blog.  Joel has been writing a CodeStyleEnforcer add-in for Visual Studio.  He’s leveraged the add-in capabilities of DXCore from Developer Express to integrate the tool seamlessly into the IDE.

Installation of the tool was a breeze.  First download DXCore, then download the DX_CodeStyleEnforcer.dll file from Joel and copy it to the following path.

C:\Program Files\Developer Express Inc\DXCore for Visual Studio .NET\2.0\Bin\Plugins

After that all you have to do is start the IDE and the add-in will be functioning.  All the rules for the Code Style Enforcer are enabled either through RegEx (for naming conventions) or checkboxes (valid visibilities).  This gives you a number of options for customization, assuming you can write the RegEx expression that you need.

 By default the Name Rules are pretty basic.  To test the ability for the application to be customized based on my newly written tome on coding standards I customized them to what you see in the pictures.  The first thing that I did was change the Field Names and Readonly Names to switch them from an ‘m‘ prefix to a ‘‘ prefix.  After closing the window the code in my IDE was updated to show that it now met the standard.  I also changed the Name Rules to allow for Class names to use alpha-numeric instead of just alpha, and restricted the numeric values so that they couldn’t appear as the start of the Class name.  Below is a screen shot show the code using these standards and the unobtrusive reminder (red pencil mark underline) that you have not met the naming requirement.

I did notice one problem with the add-in, but I’m not sure if it’s related to the DXCore tool, or the Code Style Enforcer.  When I used snippets and in a some other cases, the proper formatting of the newly inserted code from the snippet was not correct.  Usually this was immediately noticeable as the code did not indent correctly.  Other than that small issue, this is a great tool and I’m going to be working with it a lot more in the future.