Friday, September 19, 2008

After a period of initial bad taste, I've come to like StyleCop. I dislike a few of their rules, and I turn those off. But overall I like how it helps many developers maintain a consistent coding style within a project. I wrote a few rules of my own, and offer them here for free.

And here are the rules that are currently in the library, with more likely to be added soon. Remember you can pick and choose which rules you want to use within the Settings.StyleCop file.

NoTrailingWhiteSpace

This rule will make sure that lines of code don't end with extra spaces or tabs that don't do anything. What does it hurt? Well, it doesn't much. But just like almost any rule in StyleCop, it helps maintain consistent formatting. I like enabling visible whitespace in Visual Studio so that I can make sure I'm consistent with tabs vs. spaces within a file. And seeing whitespace characters at the end of a line is tacky. But the bigger reason is that auto-formatting in C# will remove that trailing whitespace, which means if you're using revision control you'll often get diffs on lines that have nothing but removed whitespace changes, making your diff bigger than it needs to be. This rule helps you avoid the whitespace to begin with.

IndentUsingTabs

This is the inverse of the SA1027 rule: TabsMustNotBeUsed. It ensure that you do use tabs instead of spaces for indentation. Beautiful. I like tabs instead of spaces so that whoever is viewing the code can choose their preferred indentation size rather than having to use whatever the author thought was readable.

NoSpacesBeforeTabs

Sometimes when one person is using tabs with size 4 and another has size 3 tabs, extra spaces can make formatted code look wrong. This rule just makes sure you don't have a tab, a space, and another tab or something strange like that.

8 comments:

Are you talking about the Download button in github? It should work, but it seems to use some javascript so make sure you're using a popular browser with javascript enabled... would be my only suggestion.