Microsoft Source Analysis aka StyleCop

A bit about code style guidelines, my own preferences and a pointer to StyleCop.

A couple of weeks back, StyleCop, which used to be an internal tool was rebranded and publicly released as Microsoft Source Analysis. Right around that time, we were having an internal discussion within the team around what sort of coding style guidelines we should put in place for a new project.

Such discussions are always fun, and can serve as fodder for ongoing back and forth unless someone just gives in. If design guidelines are debatable (eg. Id vs ID), style guidelines are at least an order of magnitude more debatable; everyone has an opinion and a set of preferences around source code. I think what we really need is a system around how code is a model, not just text, and the editor is simply a view over that model that reflects a user's choice. In the meantime, we have guidelines, and tools such as StyleCop to help enforce them.

Speaking about debatable guidelines, it seems one of the big point of debates was around the tool's default rule that tabs should not be used for indentation. I personally agree with that one, but I'll throw in an even more debatable rule that is a preference of mine (which unfortunately the tool doesn't default to): K&R bracing style. You've already seen me use it in blog posts that contains code snippets. Ultimately, independent of specific stylistic choices, a consistent and predictable code base is a first step toward facilitating readability and discoverability. Definitely check out the tool if you haven't already.

For those curious, I actually have a coding guidelines document I use that is mostly based on what we agreed on for the ASP.NET code base some years back. I tried to tweak the settings offered by the tool to create a matching set, though I think I'll need to write some custom rules around ordering and spacing if I want the tool to exactly match my preferences.

On a related note, this tool completely works as expected with Script# projects, as I expected. So, if you're using Script# to build an Ajax app or component, you too can now benefit from this tool. This just shows how a good chunk of both new and existing .NET tooling can be repurposed without much work.

Posted on Thursday, 6/5/2008 @ 9:00 PM | #Projects


Comments

13 comments have been posted.

Skwr

Posted on 6/5/2008 @ 10:02 PM
While K&R is the one true style, allman is the style that is most visibly associated with C# - e.g. msdn, ...
As a reader, interpreting K&R vs Allman style is fairly easy, however it is a minor annoyance as it breaks the flow of reading - do you find the opposite (interpreting Allman vs K&R)?

mike

Posted on 6/6/2008 @ 12:40 AM
I'll never understand tabs vs spaces. If I submit this comment I only have to press a button once, not twice or 4 times. So why do so many programmers like to press their spacebar twice or even 4 times, instead of pressing the tab once. Sam goes for undoing indentation, press backspace twice or 4 times as often when using spaces.

Then there's the argument that a tab has no meaning, it could be 8 spaces equivalent. Let's be really honest here. Is it possible to install Windows XP or Vista, then install Visual Studio 2005 or 2008 and end up with anything but 4 spaces equivalent tabs? I don't think so. And just as long as you don't use tabs for _alignment_ everyone should be happy.

Lastly, tabs CAN be adjusted to your liking, spaces can't. If my coworkers insists on two spaces and I really like 4 spaces, tabs are the only way to make us both happy.

These simple facts should convince anyone. It doesn't, but that's because people take it way too far, like it's a religion to them that just can't be reasoned with, and must be followed and everything else be damned!

Oh well, I'm firmly in the tab camp, but I can be convinced by reason. So give it a shot.

RichB

Posted on 6/6/2008 @ 1:01 AM
I've used StyleCop extensively. The main problem with StyleCop is that it's implemented incorrectly as a compile-time checker of code style instead of a code pretty printer. It should have been written to hook into the existing ctrl-K, F chord in Visual Studio which performs code reformatting.

It once took me 5 attempts to work out how it wanted the generic constraint token ':' to be spaced.

The second problem with StyleCop is that new rules are introduced in each version which result in a lot of work to update an existing codebase to the new rule. For example, when the rule which forced comments to end in a period '.' was introduced, large codebases were broken - I personally spent the best part of a day updating a smallish codebase to these new rules. That's a waste of time. StyleCop should do this for me.

These are simple rules which should be handled by a code formatter, not a style checker.

RichB

Posted on 6/6/2008 @ 1:05 AM
Skwr:
C# Allman code is seen in the wild because Visual Studio has always defaulted to it. However, when I began learning C# I had a read through the ROTOR (SSCLI) codebase and found that most of the code in the .Net BCL/Framework/WinForms/XSP was written in K&R style. Hence K&R became my preferred C# coding style. For the same reason, C# became my preferred .Net language.

RichB

Posted on 6/6/2008 @ 1:08 AM
Mike:
The argument for spaces always comes down to ASCII drawings/tables in comments. If you use tabs and then change the tabstop distance, the ASCII drawings/tables will no longer line up.

This problem is easily solved by always using spaces in your ASCII drawings inside comment blocks. Hence there's no reason not to use tabs.

Nikhil Kothari

Posted on 6/6/2008 @ 5:38 AM
Mike:
First you don't really need to press spacebar 4 times - the VS editor will do it in response to a tab. Yes, that doesn't happen in notepad.

One problem with tab is that not all tools have the ability to render tabs as 4 spaces - eg. windiff, which is used all too often. A second problem is often times you have to indent followed by add a few spaces to align (eg. align parameters across multiple lines), and this fails if your tabs are rendered variably by different users.

Skwr:
I do find Allman style to be a distraction - one really gets used to a particular style I guess. I think its a waste of vertical screen real estate, esp. when combined with the rule that says even a single line block should be within braces.

HeartattacK

Posted on 6/6/2008 @ 6:34 AM
But it does have the benefit of being able to do this:

//if(blah blah)
{
code;
more code;
}

You can't do that with K&R.

Mark Hildreth

Posted on 6/6/2008 @ 6:57 AM
I just starting looking at StyleCop. I think this tool is a great idea. I can't figure out how to adjust or enforce/not enforce the rules. Is that even possible?

Nikhil Kothari

Posted on 6/6/2008 @ 7:35 AM
Mark:
Once you've installed StyleCop, simply right click on your project in Solution Explorer inside VS, and choose Source Analysis settings... you can customize the rules, and the tool saves the settings in a Settings.SourceAnalysis file alongside your .csproj.

Adam Ford

Posted on 6/6/2008 @ 8:54 AM
Unfortunately, it doesn't seem to work with ASP.NET Web Site projects (at least in 2005). When you right-click on the project and select Source Analysis Settings, you get an "Unable to get the path to the project." error, and if you try to run it, the output says there is no file to analyze.

There's already an issue for this on the Code Gallery page, so make sure to vote for it if you're having the same problem!

Robert Barth

Posted on 6/6/2008 @ 9:17 AM
Tabs. Always tabs. Nothing other than tabs. Ever. Align things on tab boundaries and then everything always aligns. I have the advantage of working exclusively in VS2008 or something that permits tab space specification via options, so this always works for my teams and me.

The argument for spaces in which the programmer doesn't need to hit spacebar four times because the VS IDE will do it for them, and then the argument against tabs in which notepad doesn't space the tabs the way you want contradict's the first argument for the same reason -- notepad doesn't work that way. Pick your tool and use it's facilities. Who uses notepad anyhow? Notepad2 and Crimson Editor (and many others) all support specifying the tab indent.

Regarding curly braces, I don't mind scrolling up and down, but I don't care for scrolling left to right. So, I use Allman with a tab indent specified to two spaces. Line contunations are three tabs (six spaces) to differentiate them from a subsequent control block.

At least the tab/space argument only has two options. The curly brace argument has more options than one would care to read about.

Speednet

Posted on 6/7/2008 @ 12:30 PM
I completely agree with Mike about the tabs. You mentioned all the points I would have made myself.

I actually get annoying when I import code that was created with spaces. Thank God for the VS2008 auto-formatting capabilities. The only thing it doesn't help me with is JavaScript.

Nikhil, why would you even use an editor that can't handle tabs? Something that can't handle tabs is not even worthy of installation, IMHO.

I'm more of a VB coder, but for both C# and JavaScript, it's K&R all the way, except the opening curly braces for functions are always on the first line for me (as opposed to true K&R style, which places the opening function brace on the second line).

I know the number of non-space characters is the same for both styles, but Allman style just seems too verbose for my tastes, and in K&R I like how the end brace matches visually closes the actual control word. To each their own on that argument, however, as points from both sides make sense.

Manish

Posted on 9/1/2008 @ 5:09 AM
Our company has some coding guidelines in which we need to check Naming Convention like..For Hashtable we need the variable should start with ht if it is private or function variable.

Is it possible to check the same.
Post your comment and continue the discussion.
 
 
 

 

 
Refresh this form if the spam-protection code is not readable, or has expired. (Your input will be preserved)