Problem in rule validation for local member variables

HI,
            I have created a Custom rule to check the naming conventions. It works well for the module level variables/controls etc. 

         when we want to validate the variables of a private function/method . It does the validation for all its varibales. But the rule violation is reported for only one variable. While debugging every thing seems to be proper(it validates all the variables and adds the violations to the Problems collection). It is not reported in the Fxcop rule violations window.


for example :

private functionTestMethod()
   Dim MyInt as integer
   Dim MyStr as string
   Dim MyDate as Datetime

End function
           
   The rule violation is reported for only one member in the above method.I debugged and found that rule violation is generated for all the variables. But it is not reported in the Fxcop screen.

I have attached the code below.

Thanks in advance

Regards,
Suriya Prakasam R







Answer this question

Problem in rule validation for local member variables

  • Rizzi Davide

    Hi, Suriya,

    I edited your post to remove the rule code, it made your message difficult to read. I have a good answer for you (with a relative snippet), so I don't think we'll miss the information. 8)

    You've put your finger on a subtle issue in custom rules development. If you double-click the violation you received in FxCop, or if you save the project and look at the XML, you will see that there are actually multiple issues associated with the single violation that refers to TestMethod. This is due to the way you created your Problem instances, you did not provide an Id for each.

    When analyzing a method with a rule that can raise multiple violations, you need to consider whether the individual problem instances you're creating should appear in the UI as separate violations which can be individually excluded. If a user will reasonably either fix all occurrences of a problem, or exclude them, there's no point in generating a separate violation for each. Consider the FxCop rule that flags misspellings in method names. If FxCop analyzes a method named FooTestFooMethodFoo, there's not point in raising three violations in the UI. The user is very unlikely to fix two occurrences of 'Foo' but not the other. Instead, we raise a single violation that has three issues.

    To allow rule control over when discrete violations (actually referred to as Messages in the FxCop api) are created, there's a notion of a rule Id. This is a string identifier that uniquely identifies an issue. This identifier should be stable over time (you shouldn't use an IL offset as an Id, for example). The identifier should group violations that will either be fixed or excluded as a set. Getting back to the spelling rule, consider a public method named FooBarFoo. The FxCop rule creates three issues, one for each unrecognized token, Foo, Bar, Foo. The token is used as a message id in each case. The result is two violations in the FxCop UI, one message (with two issues) for 'Foo' and one message (with one issue) for Bar.

    Getting back to your specific rule, if you expect users to fix all violations of your rule that fire against a method (or to ignore your rule altogether), you could continue with the rule as written. There are two downsides here. The first is that it's not immediately obvious there are three problems with the method. Multiple issues are not extremely discoverable in our current UI. This can create developer frustration as well, when someone fixes one issue, analyzes again, and still sees a message (which has one fewer issue, however).

    What you can do to resolve this is add an Id to each problem you create. In this case, the variable name is well-suited for the purpose. The string Id argument is provided to the Problem constructor:



    larrSource[0] = local.Name.Name;
    Problems.Add(new Problem(GetNamedResolution("ValueTypes",larrSource), local.Name.Name));


     


  • Sha Sea

    HI Michael,
                   THANKS A LOT. 
                   With your help I am able to solve my issue. Sorry for difficulty in understanding the problem mail. I will improve the presentation styles.Your detailed mail also helped me in unserstanding the fxcop behaviour.

    Rehards,

    Suriya Prakash.

  • Problem in rule validation for local member variables