Click here to Skip to main content
15,891,253 members
Articles / Programming Languages / C#

Performance Analysis: Disabling Code-analysis Warnings and Excluding Code Coverage for Tested Common Methods

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
9 May 2013CPOL3 min read 12.6K   2  
Performance analysis

Introduction

When the project in which I was working was going through performance analysis, we found our project has a lot of warning generated by code analysis and also code coverage is not up to the mark. We analyze warnings and methods that were not under test coverage. What we found was that we were mostly following best practices but still code analysis left us with a lot of warnings. The same goes with code-coverage, all new methods were having proper test cases for all scenarios. What was going wrong???

The actual problem was:

  1. Some code components (commonly used components and third-party code) developed earlier were considered tested and free from warnings, but that wasn't the case.
  2. Also we could not change that code for warnings and test coverage (because nor time-constraint allow that neither we want to do so for ample other reasons like copyright with change, etc.)

So what can the solution be?

The solution was nevertheless easier then we thought. Microsoft has provision for disabling code-analysis warnings and excluding code-coverage for such a scenarios only in System.Diagnostics.CodeAnalysis namespace.

NOTE: It’s not recommended to disable code-analysis warnings and also disable code-coverage as doing this can project wrong code-analysis data, but our scenario was just perfect for what Microsoft has provisioned disabling.

The System.Diagnostics.CodeAnalysis namespace contains classes for interaction with code analysis tools:

  1. ExcludeFromCodeCoverageAttribute: Specifies that the attributed code should be excluded from code coverage information.

    Actual working example:

    C#
    [System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
    public static OperationControl GetInstance()
    {
      //Your code goes here
    }
    

    In the above code, ExcludeFromCodeCoverage attribute will prevent the method from being covered in test coverage and thus increase code coverage percentage. Please note only third-party tested code, test methods and such cases are recommended for using the attribute and it must not be applied to all/most of methods that will defeat the purpose of code-coverage check.

  2. SuppressMessageAttribute: Suppresses reporting of a specific static analysis tool rule violation, allowing multiple suppressions on a single code artifact.

    The SuppressMessage attribute has the following format:

    C#
    [Scope:SuppressMessage("Rule Category", "Rule Id", 
    Justification = "Justification", MessageId = "MessageId", 
    Scope = "Scope", Target = "Target")]

    Where:

    • Rule Category – The category in which the rule is defined. More information on categories can be found on MSDN.
    • Rule Id – The identifier of the rule. Support includes both a short and long name for the rule identifier. The short name is CAXXXX; the long name is CAXXXX:FriendlyTypeName.
    • Justification – The text that is used to document the reason for suppressing the message.
    • Message Id – Unique identifier of a problem for each message.
    • Scope – The target on which the warning is being suppressed. If the target is not specified, it is set to the target of the attribute. Supported scopes include the following:
      • Module
      • Namespace
      • Resource
      • Type
      • Member
    • Target – An identifier that is used to specify the target on which the warning is being suppressed. It must contain a fully-qualified item name.

    Actual working example:

    C#
     [System.Diagnostics.CodeAnalysis.SuppressMessage
    ("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate")]
     public static OperationControl GetInstance()
     {
       //Your code goes here
     }

    In the above code, SuppressMessage attribute will prevent Microsoft.Design warning”CA1024?.

For more detailed information, you can refer to the following links on MSDN:


License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --