Click here to Skip to main content
11,578,739 members (65,864 online)
Click here to Skip to main content

Tagged as

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

, 9 May 2013 CPOL 5.1K 2
Rate this:
Please Sign up or sign in to vote.
Performance analysis

Editorial Note

This article appears in the Third Party Product Reviews section. Articles in this section are for the members only and must not be used by tool vendors to promote or advertise products in any way, shape or form. Please report any spam or advertising.

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:

     [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:

    [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:

     [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)

Share

About the Author

PRANAV SINGH
India India
No Biography provided

You may also be interested in...

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150603.1 | Last Updated 9 May 2013
Article Copyright 2013 by PRANAV SINGH
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid