Wednesday, May 7, 2008

FxCop Rules, Causes and Fixes - Part 2

Continuing the previous article on FxCop rules. I have added some more rules and their reasons.


Rule: StaticHolderTypesShouldNotHaveConstructors

Cause: A Public type declares only static members and has a public or default constructor.

Fix: Remove the public constructor and replace that with a private constructor.

Rule: StaticHolderTypesShouldBeSealed

Cause: A Public type declares only static members is not declared with a sealed modifier

Fix: Mark the type as sealed.

Rule: IdentifiersShouldHaveCorrectSuffix

Cause: Types that extend certain base types or that implement certain interfaces, or types derived from these types, have a suffix that is associated with the base type or interface.

The following table lists the base types and interfaces that have associated suffixes.

Base type/Interface

Suffix

System.Attribute

Attribute

System.EventArgs

EventArgs

System.Exception

Exception

System.Collections.ICollection

Collection

System.Collections.IDictionary

Dictionary

System.Collections.IEnumerable

Collection

System.Collections.Queue

Collection or Queue

System.Collections.Stack

Collection or Stack

System.Collections.Generic.ICollection

Collection

System.Collections.Generic.IDictionary

Dictionary

System.Data.DataSet

DataSet

System.Data.DataTable

Collection or DataTable

System.IO.Stream

Stream

System.Security.IPermission

Permission

System.Security.Policy.IMembershipCondition

Condition

An event-handler delegate.

EventHandler

Fix: Rename the type so that it is suffixed with the correct term.

Rule: IdentifiersShouldNotContainUnderscores

Cause: identifier contains the underscore (_) character.

Fix: Remove the underscore.

Rule: UriPropertiesShouldNotBeStrings, UriParametersShouldNotBeStrings, UriReturnValuesShouldNotBeStrings

Cause: A Property, Parameter or a method that contains Uri, Url etc in the name is declared as a String. A string representation of a URI is prone to parsing and encoding errors, and can lead to security vulnerabilities. The System.Uri class provides these services in a safe and secure manner.

Fix: Change the return type to Uri.

Rule: DoNotDeclareVisibleInstanceFields

Cause: An externally visible type with modifiers (Public, Protected/ Protected Internal) has an externally visible instance field.

Fix: Make the externally visible field private or internal and expose it using an externally visible property.

Rule: DoNotInitializeUnnecessarily

Cause: A field is initialized to its default value. The common language runtime initializes all fields to their default values before running the constructor. In most cases, initializing a field to its default value in a constructor is redundant, which degrades performance and adds to maintenance costs.

Fix: Remove the unnecessary initializations.

Rule: IdentifiersShouldBeCasedCorrectly

Cause: The name of an identifier is not cased correctly. By convention, parameter names use camel casing; namespace, type, and member names use Pascal casing. In a camel-cased name, the first letter is lower case, and the first letter of any remaining words in the name is in uppercase. In a Pascal-cased name, the first letter is upper case, and the first letter of any remaining words in the name is in uppercase.

Fix: Change the name so that it is cased correctly.

Rule: CompoundWordsShouldBeCasedCorrectly

Cause: The name of an identifier contains multiple words and at least one of the words appears to be a compound word that is not cased correctly.

Fix: Change the name so that it is cased correctly.

Rule: IdentifiersShouldNotMatchKeywords

Cause: A namespace name or a type name matches a reserved keyword in a case-insensitive comparison. Using a reserved keyword as the name of a virtual/interface member makes it harder for consumers in other languages to override/implement the member.

Fix: Change the name to something that does not appear in the list of keywords.

Rule: PropertyNamesShouldNotMatchGetMethods

Cause: The name of a public or protected member starts with 'Get' and otherwise matches the name of a public or protected property.

Fix: Change the name so that it does not match the name of a method prefixed with 'Get'.

No comments: