C# Code Analysis with StyleCop for ReSharper

October 4, 2009 18:14

I'm always looking for new tools to add to my developer's toolbox. One such tool I recently came across is StyleCop for ReSharper.

StyleCop for ReSharper is a ReSharper add-on that enables real-time syntax highlighting of StyleCop rules. Normally, you run StyleCop by selecting "Run StyleCop" from the Tools menu:

image

The StyleCop for ReSharper takes it one step further by providing the same analysis, but in real-time and with squigglies:

image

The first thing you'll notice is that every single line has been flagged for violations! That's kind of ridiculous. Fortunately, there's a quick way to reduce that count by loosening up the rules a bit.

Prerequisites

Of course, to use the StyleCop for ReSharper add-on you'll need to have ReSharper. Also, if you attempt to install the add-on without first having installed StyleCop, you'll see this dialog:image

The latest version of StyleCop can be downloaded here.

Loosening up StyleCop's Rules

Before we start modifying the default rules, let's learn how to share a single StyleCop settings file across multiple solutions or projects.

StyleCop starts looking for a Settings.StyleCop rules or settings file in the project's local folder first. Regardless of whether it finds one or not, it works up the folder tree, looking for other copies of the file. Once it reaches the top of the chain, it goes back to the default install folder where there should always be a the global version of the file. It is possible to override the global settings by placing a copy of Settings.StyleCop in the local solution or project file. Also, you can have global settings which are overridden by solution-level settings which in turn might be overridden by project-level settings. All depends on how granular you want to get with your rules.

Personally, I only maintain a single Settings.StyleCop file for all of my projects and solutions by putting the file at the development root (d:\dev).

You'll find the global copy of Settings.StyleCop here:

image

Copy that into your development tree, then double-click on it. That will bring up the StyleCop Project Settings editor:

image

If you expand the "Enabled rules" nodes you'll see all sorts of rules that you can turn off.

Rules I Can Live Without

Here is the list of rules that I operate without. I'll add to this list as I get further into StyleCop's rule set. In general, I'm trying to adhere to the rules as given, but some of them are just a bit too nitpicky for my tastes.

 

StyleCop Rule Definition Why I Turned It Off
SA1000 KeywordsMustBeSpacedCorrectly This rule is telling me my choice of spacing around, for example, the keyword 'typeof' is not right. "Not right", in this case, is in conflict to my standard formatting rules, so this one is out.
SA1008 OpeningParenthesisMustBeSpacedCorrectly This rule seems to dictate no spaces between, say, a method name and an opening parenthesis. I like have a single space between such things, so this one stays off.
SA1027 TabsMustNotBeUsed Ah, yeah. I don't like spaces. I use tabs.
SA1101 PrefixLocalCallsWithThis In my mind, the "this" keyword is implied by its absence, and I generally do not use it unless there is some naming conflict present otherwise.
SA1117 ParametersMustBeOnSameLineOrSeparateLines  
SA1309 FieldNamesMustNotBeginWithUnderscore This is in exact conflict with the style guide I use, which advocates using an underscore as the prefix on private variables.
SA1502 ElementMustNotBeOnSingleLine There are times, for example for a getter/setter property definition, where you might want everyting on one line, including the curly braces. This ones gets turned off.
SA1503 CurlyBracketsMustNotBeOmitted I think this goes a bit too far. There are times where, for readability if nothing else, it's OK to omit the curly braces.
SA1512 SingleLineCommentMustNotBeFollowedByBlankLine Another one that is a bit too nitpicky. I'm happy just having comments in the code. Who cares about blank lines?
SA1515 SingleLineCommentMustBePrecededByBlankLine Same reasoning as SA1512.

Rules Off By Default That I Use

The other side of turning off default rules is turning on some that are off by default. Here are those:

 

StyleCop Rule Definition Why I Turned It Off
SA1124 DoNotUseRegions I hate regions. Nuff' said.

Conclusion

If we take a look at the code snippet from above we'll see that the squiggly-indicated violations have gone way down:

image

Still have some work to do, but at least it's a bit more manageable and realistic.


Podcasts

January 23, 2009 07:17

If you want to truly immerse yourself in the programming experience you need to listen to technology podcasts.

Here's my favorites (if I've missed any, please add a comment below; I'd love to make this a more comprehensive list):

  1. .NET Rocks
  2. Alt.NET Podcast
  3. Hanselminutes
  4. Herding Code
  5. Misfit Geek Podcast
  6. NPR: Technology Podcast
  7. Polymorphic Podcast
  8. RunAs Radio
  9. StackOverflow
  10. The Pragmatic Programmers
  11. The CodeCast
  12. The Thirsty Developer
  13. User Group Radio
  14. Philly Tech Guys
  15. Mike Tech Show
  16. Mind of Root
  17. Spaghetti Code
  18. CodeCast
  19. Windows Weekly
  20. Tech News Today
  21. This Week In Google

My Developer's Toolbox

January 14, 2009 20:23

All software developers have a myriad of tools they use to help them design, test, and develop code and applications. This is mine. It is by no means comprehensive; I learned long ago there are too many technologies and tools out there for any one person to use or experience. The tools you'll find below are thus ones I (mostly) use routinely. Most are free. I've paid for the ones that aren't; I've found them that useful. Besides, I like to help support my fellow developer, especially when they make my life easier.

Development

These are the core tools I use to get my development work or coding done.

Visual Studio

There's probably not much I can say about Visual Studio that you don't already know. It's matured into one of the best IDE's out there and is my main development environment. I use Visual Studio Team System 2008, Development Edition. Microsoft has a number of virtual labs available where you can try Visual Studio out, or go for one of the free, scaled down Express versions.

Visual Studio 2010 Add-On's

ReSharper

ReSharper is a wonderful productivity tool. It "provides solution-wide error highlighting on the fly, advanced code completion, superior unit testing tools, over 30 advanced code refactorings, multiple handy navigation and search utilities, single-click code formatting and cleanup, automatic code generation and templates, and a lot more productivity features for C#, VB.NET, ASP.NET, XML, and XAML." Oh, it integrates with Visual Studio, too.

log4net

A great logging utility that fits into about any logging scenario.

FxCop

[ read my post VisualStudio Code Analysis vs. FxCop vs. StyleCop… what'sthe diff? ]

FxCop is an application that analyzes managed code assemblies and reports information about those assemblies, such as possible design, localization, performance, and security improvements.

The latest version of FxCop (1.36 as I type this) can be downloaded from here. Look to the Code Analysis and Code Metrics blog for future release information as well as other good info about FxCop.

StyleCop

[ read my post VisualStudio Code Analysis vs. FxCop vs. StyleCop… what'sthe diff? ] 

StyleCop analyzes C# source code to enforce a set of style and consistency rules. It can be run from inside of Visual Studio or integrated into an MSBuild project. More information about StyleCop can be found at the Microsoft StyleCop blog.

StyleCop for ReSharper

Nice add-on to add real-time syntax highlighting of StyleCop rules violations. Read the my post about this tool.

Source Control

Visual SVN Server/Client

Visual SVN is one of the many SVN implementations. The client piece is free and has great integration with Visual Studio. The server piece is not free, but the price is fairly low. This is the source control system I use.

Tortoise SVN

A free SVN implementation that integrates well with Windows Explorer. If you install Visual SVN, they will recommend that you also install Tortoise SVN.

SVN Utility: Remove SVN Folders

A nice little shell utility to delete .svn folders. A huge time saver.

Debugging/Diagnostics

.NET Reflector

Great tool for breaking down your managed assemblies and inspecting the .NET libraries. You'll likely find out more than ever wanted to know.

Depends/Dependency Walker

The must-have tool for sorting out your application's dependencies.

Firebug

Great little Firefox add-on to help edit, debug, and monitor CSS, HTML, and JavaScript, live and in any web page. The ability to highlight any section of a web page and see the underlying make-up is invaluable

YSlow for Firefox/Firebug

Analyze the performance of your web site and get some tips on how to make it faster.

Internet Explorer Developer Toolbar

Analogous to Firebug, but for Internet Explorer. I find myself often using both, especially when faced with a cross-browser issue.

PageSpy

This one provides similar functionality to Firebug and the IE Dev Toolbar. It's one I keep meaning to experiment with more. I thought I'd at least list it in case you want to check it out.

Fiddler

Fiddler is a tool that allows for network and HTTP monitoring. It's one I haven't played with too much, but adding it anyway since I think it's a tool that potentially could provide value when you need its functionality.

ELMAH

[ read my post AddingELMAH to your ASP.NET Web Site ]

How did I ever live without this? Reprting for unhandled exception handling and other errors your site may encounter but which you probably don't even know about.

Testing

NUnit

My default unit testing platform. I also use it for integration style testing prior to deployment.

TestDriven.NET

I can't say enough about this Visual Studio add-in. TestDriven.NET gets used almost every day by me. It's specialty is allowing you to run and debug into unit tests. I love it.

Silverlight Unit Testing Framework

This is one that's on my radar, thus the listing here. I hope to investigate what it offers more as I get into development with Silverlight.

Documentation

NDoc

We use this one at work. It's a nice tool for generating code-level documentation.

Continuous Integration

CruiseControl.NET

[ read my series on Settingup a Continuous Integration System ]

CruiseControl.NET is incredibly flexible, somewhat easy to configure, and should be part of any development shop's build, test, and deploy process. It's the crux of my continuous integration environment.

Setup 

IExpress

A free utility that comes with Windows for creating self-extracting executables. You can read my post about it here

Deployment

At work, I don't do deployments. We have other groups dedicated to QA and subsequent deployment into our production environment. On the personal side, however, I perform the heavy chore of deploying (web site) files myself.

SmartFTP

This is FTP at its best. SmartFTP is quick, efficient, uses threaded uploads (so you can upload multiple files at the same time), has a real slick interface, and just works. It's also regularly updated. Licensing is not free, but this one is worth the purchase.

Web Site Statistics

Google Analytics

View web site statistics galore with this one. Great for tracking content visitor hits over time. 

Quantcast

Free, public web site statistics and demographic information. View stats for this site.

W3Counter

Similar to Analytics, but W3Counter provides real-time web traffic data. The "free" account, however, only includes monitoring one web site and has a daily limit on reported page views. Still, it's become my web stats provider of choice. If you want to see it in action, take a look at the public stats for this site.

RSS

So these technically aren't development tools, but you need some way to keep up the development community and that happens more often than not on blogs. These are my readers of choice.

RSSBandit

Great offline RSS reader. I switched to RSSBandit after we lost Internet connectivity for a day during a winter storm. It's does a good job keeping up with my feeds and, one of the best things, it will automatically download attachments, so my podcasts are always there ready for a listen.

Google Reader

Though I mostly use RSSBandit, if I don't have my laptop but still have Internet access through another machine I go with Google Reader. It's browser-based, fast, and has a nice tagging system similar to Google Mail.

Blogging

Blogging facilitates better development. Here's what I use when blogging.

Windows Live Writer

This is really the only blogging tool I've ever used. With add-in support, a slick editor, the ability to preview a blog entry and then publish that entry right to your blog, I haven't had much need to try anything else. Get updates and more information about WLW at the WLW team blog, the Writer Zone.

Backup/Online Storage

Dropbox

Dropbox is my cloud storage provider of choice. You get 2GB of storage for free, it's fast, secure, and is completely unobtrusive.

Utilities

These tools don't fit into the above categories but they're equally useful for different tasks. They might not be development specific, but they're part of my overall suite of tools that I use to get things done.

Windows Sysinternals

I use a variety of tools from the Sysinternals suite: Process Explorer, FileMon, Process Monitor, and others. There's a wide variety of tools contained therein; it's the Swiss army knife of Windows utilities.

Beyond Compare

This is the best file comparison tool I've ever used. It's not free, but well worth the (trivial) cost.

Winsnap

Winsnap knocked me off my feet when I started using it. I had to have it. A quick purchase later and I've been happy with it ever since. This is the best tool out there (that I've seen) for taking screenshots. It has a timed shot feature, and readily grabs the entire screen, an application window, or region. It's what I use to take snapshots of most of the images on this blog.

Winkey

If you're like me and can't figure out how to map hot keys in Windows Vista, just download Winkey and don't worry about it. It's easy to use, works well with Vista, and saves you mouse clicks.

Launchy

The most excellent keyboard shortcut accelerator I've ever used. Alt-Space and go. Amazing.

CPU-Z

Everything you'd ever want to know about your machine's CPU, motherboard, memory, graphics card, and more.

The Password Meter

Not really a development tool, but a neat online utility that tests the strength of your password and reports a breakdown of why it's weak or strong.

Browser Add-on's

IEView / IEView Lite (for Firefox)

Opens web pages in Internet Explorer from Firefox.

FirefoxView (for IE)

Opens web pages in Firefox from Internet Explorer.

Additional Resources

I obviously can't list everything here, so here's some other toolboxes filled with some great tools:

  1. csharptools.com
  2. Scott Hanselman's Ultimate Developer and Power Users Tool List for Windows
  3. SharpToolbox (suggested from comments)
  4. Jeff Key's .net stuff
  5. The Best Visual Studio 2010 Productivity Power Tools, Power Commands, and Extensions

Windows Live Writer and BlogEngine.NET: Can't view Properties

December 14, 2008 10:52

If you're using Windows Live Writer (WLW) and BlogEngine.NET and you find yourself unable to view properties or set tags on your blog posts (like I couldn't do up until just a little while ago), your site is probably missing the "wlwmanifest.xml" file. This file comes standard with the BlogEngine.NET 1.4 release.

For some reason, when I recently upgraded my site from 1.3 to 1.4.5 I missed copying this file into my web site's root folder. The result was that I was missing out on some of WLW's functionality.

Copying the file over is easy enough. It goes in the root of your site.

Much better. Now I can add tags (keywords) from within WLW:

image

Also, I get some extra features where I can view my current tag cloud and access some of management features directly from WLW:

image

I thought I'd look a little further and see what exactly the wlwmanifest.xml file is all about.

The wlwmanifest.xml file is a set of meta-data used as inputs into WLW's metaweblog API. The API is a standard set of functions that allows weblog content tools like WLW to know about what functionality a particular blogging platform supports.

From this MSDN article, WLW will look for the wlwmanifest.xml file in the web site's root when you add a new web site (weblog) or when updating one in WLW. If it can't find it there, it cracks open the default home page and examines the <head> section for a <link…> that might point it in the right direction.

Let's take a look at the contents of the wlwmanifest.xml file:

   1: <?xml version="1.0" encoding="utf-8" ?> 
   2: <manifest xmlns="http://schemas.microsoft.com/wlw/manifest/weblog">
   3:   <options>
   4:     <clientType>Metaweblog</clientType>
   5:     <supportsEmbeds>Yes</supportsEmbeds>
   6:       <supportsKeywords>Yes</supportsKeywords>
   7:       <supportsNewCategories>Yes</supportsNewCategories>
   8:       <supportsNewCategoriesInline>Yes</supportsNewCategoriesInline>
   9:       <supportsCommentPolicy>Yes</supportsCommentPolicy>
  10:       <supportsSlug>Yes</supportsSlug>
  11:       <supportsExcerpt>Yes</supportsExcerpt>
  12:       <supportsPages>Yes</supportsPages>
  13:       <supportsPageParent>Yes</supportsPageParent>
  14:     <supportsAuthor>Yes</supportsAuthor>
  15:     <requiresHtmlTitles>No</requiresHtmlTitles>
  16: </options>
  17:   <weblog>
  18:     <ServiceName>BlogEngine.NET</ServiceName>
  19:     <imageUrl>pics/wrenchicon16.png</imageUrl>
  20:     <watermarkImageUrl>pics/wrench84watermark.png</watermarkImageUrl>
  21:     <homepageLinkText>View your blog</homepageLinkText>
  22:     <adminLinkText>Manage your blog</adminLinkText>  
  23:     <adminUrl><![CDATA[{blog-homepage-url}login.aspx]]></adminUrl>
  24:   </weblog>
  25:   <buttons>
  26:     <button>
  27:       <id>2</id>
  28:       <text>Tags</text>
  29:       <imageUrl>pics/benTag24.png</imageUrl>
  30:       <contentUrl><![CDATA[ 
  31:          {blog-homepage-url}api/tagminiview.aspx
  32:       ]]></contentUrl>
  33:       <contentDisplaySize>250,250</contentDisplaySize>
  34:     </button>     
  35:   </buttons>
  36: </manifest>

You can see a number of tags that begin with "<supports…". These are simply telling WLW that BlogEngine.NET supports these functions and that it should make them available through its interface.

There's also some tags directly associated with identifying BlogEngine.NET as the blogging service as well as the mini-tag application written by Al Nyveldt.

In the end, all I really wanted was to be able to set tags while within WLW. Functionality restored.