Calculate code metrics in VS

Source: Internet
Author: User
Starting with VS2008, there is a new tool to help developers better understand the code they write. This tool is "calculated code metrics."

For a specific description of this feature and several indicators, please refer to the official documentation below

Code metrics are a set of software metrics that enable developers to better understand the code they are developing. With code metrics, developers can see which types and/or methods should be reworked or tested more thoroughly. The development team can identify potential risks, understand the current state of the project, and track the progress of the software development.

Software metrics

The following list shows the results of the code metrics calculated by Visual Studio:

Maintainability Indexcalculates an exponential value between 0 and 100 that represents the relative ease of maintaining code. A larger value means better maintainability. The calculation is based on the Halstead Volume, cyclomatic complexity, and the number of lines of code. The level of color markers can be used to quickly discover points of failure in your code. The green level is between 20 and 100, indicating good code maintainability. The yellow level is between 10 and 19, indicating that the code is moderately maintainable. The red level is a rating between 0 and 9, indicating low maintainability.

Cyclomatic complexity-Measures how complex the code is in structure. It calculates the number of different code paths in the program flow (such as if blocks, switch cases, and do, while, foreach, and for loops), and then adds a creation to the total. programs with complex control flows require more unit testing to achieve good code coverage and are not easy to maintain.

Inheritance Depth – indicates the number of class definitions extended to the root of the class hierarchy. The deeper the hierarchy, the harder it is to understand where a particular method and field are defined or/and redefined. at the class level, the number is created by calculating the number of types (excluding interfaces) on top of the 0-based type in the inheritance tree. At the namespace and project level, this calculation includes the highest inheritance depth calculations for all types within a namespace or project.

Class coupling – Measure the degree of coupling with a unique class by parameters, local variables, return type, method invocation, generic or template instantiation, base class, interface implementation, fields defined on external types, and property adornments. The calculation does not include primitive types and built-in types, such as Int32, strings, and objects. Good software design requirements types and methods should have high cohesion and low coupling. high coupling indicates that the design is difficult to reuse and maintain because it has many dependencies with other types.

Number of lines of code-indicates the approximate number of lines in the code. The count is based on the IL code, so it is not the exact number of rows in the source code file . The calculation does not include whitespace, comments, parentheses, and declarations of members, types, and namespaces. A high count may indicate that a type or method is trying to do too much work and should be split. It may also indicate that the type or method is difficult to maintain.

anonymous method

Anonymous method is a method that has no name. Anonymous methods are often used to pass code blocks as delegate parameters. The measurement result of an anonymous method declared in a member, such as a method or accessor, is associated with the member that declares the method, not with the member that called the method.

For more information about how code metrics handles anonymous methods, see Anonymous methods and code analysis.

Generated code

Some software tools and compilers generate some code and add it to a project that the project developer cannot see or change. When code metrics calculate measures, the generated code is typically ignored. In this way, measures can reflect what developers can see and change.

Code generated for Windows forms is not ignored, because developers can see and change the code.

Code Metrics Results Window

When Visual Studio analyzes the code, the results are displayed in the Code Metrics Results window. There is a toolbar at the top of the window, and a few columns to display the results of the calculation.

The hierarchy column contains a code hierarchy tree view that you can expand or collapse to view the level of detail you want. The remaining columns show the results of the calculation. You can hide or arrange the result columns as needed. For more information, see How to: Add, remove, or rearrange columns.

The maintainability column also contains an icon in addition to the numeric results. A green icon indicates a relatively high degree of maintainability. The yellow icon indicates moderate maintainability. A red icon indicates low maintainability or potential point of failure. these color indicators correspond to the severity levels used by the FxCop rule Avoidunmaintainablecode. This rule fires an error if the maintainability index is below 10, fires a warning if the exponent is between 10 and 20, and if the exponent is higher than 20, neither the error nor the warning is fired. The maintainability index is a comprehensive representation of the following three metrics: cyclomatic complexity, number of lines of code, and arithmetic complexity. The value of the maintainability index has no units.

You can sort the results in that window by any column. To sort, click the column heading. Click the column heading again, and the window will be sorted in reverse order.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.