Tuesday, May 6, 2008

FxCop Rules, Causes and Fixes - Part 1

Using FxCop as a code analysis tool provides a lot of benefits in fixings the errors / coding standards in the application. I will try to list out the common errors and fixes for those errors.

Rule: MarkAssembliesWithClsCompliant

Cause: Assemblies should explicitly indicate CLS compliance with CLSCompliantAttribute. If the attribute is not present on an assembly, the assembly is not CLS compliant. Assembly does not have the System.CLSCompliantAttribute attribute applied to it.

Fix: Add the reference to the namespace System in the AssemblyInfo.cs file and add the attribute [assembly: CLSCompliant(true)] to the file

Rule: AvoidNamespacesWithFewTypes

Cause: A namespace other than the global namespace contains fewer than five types.

Fix: Combine namespaces that contain a small number of types (Less than 5) into a single namespace.

Rule: AssembliesShouldHaveValidStrongNames

Cause: An assembly is not signed with a strong name, the strong name could not be verified, or the strong name would not be valid without the current registry settings of the computer. You have delay signed the assembly. You have used the strong name tool to skip verification for the assembly.

Fix: Create a strong name file using the sn tool and sign the assembly.

Rule: LongAcronymsShouldBePascalCased

Cause: The name of an identifier contains an acronym of three or more uppercase letters

Fix: Change any three-letter acronyms in the name to Pascal case, and change any two-letter acronyms to uppercase

Rule: ShortAcronymsShouldBeUppercase

Cause: The name of an identifier contains a two-letter acronym and the second letter is lowercase.

Fix: Change the second letter of acronym to upper case.

Rule: UsePropertiesWhereAppropriate

Cause: A public method that does not take a parameter starts with a Get and does not return a collection.

Fix: Change the method to a property.

Rule: AvoidUninstantiatedInternalClasses

Cause: Internal classes that are not instantiated in the assembly. The class may have only static members.

Fix: Create a private constructor to prevent the compiler creating an instance of the class.

No comments: