Rule | Description | Example | KPI |
---|---|---|---|
AllocationOfReferenceTypeEnumerator | Non-ValueType enumerator may result in an heap allocation. | Non-complaint Code: IList iListData = new[] { 123, 32, 4 }; Complaint Code: int[] intData = new[] { 123, 32, 4 }; | Efficiency |
ArrayAllocationForParamsParameter | Unnecessary array allocation will occur even if no parameter is passed in for the params parameter. | Non-complaint Code:class Test Complaint Code: class Test | Efficiency |
CodeAnalysisSuppressionMustHaveJustification | The SuppressMessage attribute does not include a justification. Justification increases the long term maintainability of the code. | Cause: A violation of this rule occurs when the code contains a Code Analysis SuppressMessage attribute, but a justification for the suppression has not been provided within the attribute.Whenever a Code Analysis rule is suppressed, a justification should be provided.This can increase the long-term maintainability of the code. Complaint Code: [SuppressMessage(“Microsoft.Performance”, “TestSuppressAttribute”, Justification = “Used for testing”)]public bool Method1() To fix an instance of this violation, add a Justification tag and justification text to the SuppressMessage attribute describing the reason for the suppression. | Robustness |
DisposablesShouldCallSuppressFinalize | Classes implementing IDisposable should call the GC.SuppressFinalize method in their finalize method to avoid any finalizer from being called. This rule should be followed even if the class doesn’t have a finalizer as a derived class could have one. | Cause: Classes implementing IDisposable should call the GC.SuppressFinalize method in their finalize method to avoid any finalizer from being called. This rule should be followed even if the class doesn’t have a finalizer as a derived class could have one. Non-complaint code: public class MyClass : System.IDisposable Compliant code: public class MyClass : System.IDisposable | Robustness |
DoNotChangeLoopVariables | Don’t change a loop variable inside a for loop. | Robustness | |
DontConcatenateStringsInLoops | Don’t concatenate strings in a loop. Using a StringBuilder will require less memory and time. | Cause: Do not concatenate a string on a loop. It will allocate a lot of memory. Use a StringBuilder instead. It will will require less allocation, less garbage collector work, less CPU cycles, and less overall time. Non-complaint code: var values = “”; Complaint code: var values = “”; | Efficiency |
MakeFieldReadonly | A field that is only assigned in the constructor can be made readonly. | Cause: A field that is only assigned when declared or in the constructor can be made read-only. A read-only field cannot be assigned anywhere else after the object is created, making it immutable. Non-complaint code: class Class1 Complaint code: class Class1 | Maintainability |
NonOverriddenVirtualMethodCallOnValueType | Non-overridden virtual method is called on a value type. | Cause: An additional boxing or constrained instruction is needed for a non-overridden virtual method call on a value type because the CLR will automatically instantiate the corresponding class from a value type if you call some method on it. Complaint code: using System; | Efficiency |
RemoveEmptyFinalizers | An empty finalizer will stop your object from being collected immediately by the Garbage Collector when no longer used. It will instead be placed in the finalizer queue needlessly using resources. | Cause: Due to the finalize method, GC will not clear the entire memory associated with object in the first attempt. Memory used by managed resources is still in heap as inaccessible reference. Hence it is recommended that the Finalize method should not be used until it is extremely necessary. Non-complaint code: public class MyClass Complaint code: public class MyClass | Resource Utilization |
SwitchWithoutDefaultClause | Consider adding default case to the switch | Non-complaint code:switch (value) Complaint code: switch (value) | Robustness |
Invalid Logging Class Name | Consider creating the logger name in context to the current class name. | // Cause: //The log instace created should belong to the current class for logging consistency, consider changing the log class name to current class. //Example For Issue Occurrence: class ExampleClass { private static ILog logger = LogManager.GetLogger("otherClass"); static void Main(string[] args) { try { logger.Info(""); } catch (Exception e) { } } } //CODE FIX: class ExampleClass { private static ILog logger = LogManager.GetLogger("ExampleClass"); static void Main(string[] args) { try { logger.Info(""); } catch (Exception e) { } } } | Usage |
Class Implements ICloneable | Consider defining your own Clone() or Copy() methods and document whether they perform deep or shallow copying instead of using ICloneable interface. | // Cause: //Implementing 'ICloneable' is discouraged due to its ambiguous effect on the code. //Example For Issue Occurrence: using System; public class Class1 : ICloneable { public int a; public int b; public Class1(int aa, int bb) { a = aa; b = bb; } public string ToString() { return "(" + a + "," + b + ")"; } public virtual object Cloning() { return new Class1(a, b); } object ICloneable.Clone() { return Cloning(); } } | Reliability |