9

I'm just looking for some inspiration. Especially in the area of performance and security, naming conventions are important but not as 'cool' ;)

Even if your rule was only applicable to your domain/project but demonstrates how powerful a rule can be, please let me know.

I work with C#, but I'm interested in rules for any language.

tpower
  • 56,100
  • 19
  • 68
  • 100

3 Answers3

7

In my experience, the developers that have started out creating custom rule sets for FxCop, usually give up after pulling out much hair. It seems like a great idea, but the pain is just not worth the effort.

Mitch Wheat
  • 295,962
  • 43
  • 465
  • 541
3

An alternative to the mess of writing FxCop custom rules would be to use the commercial tool NDepend. With this tool one can write Code Rule over LINQ Queries (namely CQLinq). Disclaimer: I am one of the developers of the tool

More than 200 code rules are proposed by default, these include naming conventions, design, architecture, code quality, code evolution, dead code, .NET Fx usage...

CQLinq is dedicated to write code rules that can be verified live in Visual Studio, or that can be verified during build process and reported in an HTML/javascript report.

The strength of CQLinq over FxCop API or other tools, is that it is straightforward to write a code rule, and get immediately results. Facilities are proposed to browse matched code elements. Concretely this looks like that:

CQLinq code rule

Patrick from NDepend team
  • 13,237
  • 6
  • 61
  • 92
  • This looks really powerful indeed. No need to say from your username that you have a "preference" for this tool, but it's very relevant and certainly valid, plus the answer is well written, so +1. – Camilo Martin Mar 26 '12 at 09:14
1

I've got a nice functioning base with 2 rules so far at breusable.codeplex.com under the fxcop directory

ConfigKeyExistsInConfig (make sure that any references to ConfigurationManager with a magic string key actually exists in the config file.

NoUnderscoresInProperties

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.FxCop.Sdk;

namespace RulesByImaginaryDevelopment
{
  public class NoUnderscoresInProperties : BaseRule
  {
    public NoUnderscoresInProperties() : base("NoUnderscoresInProperties") { }

    public override ProblemCollection Check(Member member)
    {
        var prop = member as PropertyNode;
        if(prop==null)
            return Problems;
        if(prop.Name.Name.Contains("_"))
        {
            Problems.Add(new Problem(new Resolution("Remove any '_' from name "+prop.Name.Name)));
        }
        return Problems;
    }
  }
}

Also http://msdn.microsoft.com/en-us/magazine/cc163930.aspx

Maslow
  • 18,464
  • 20
  • 106
  • 193