Code measurement tool-learning and using sourcemonitor

Source: Internet
Author: User

Introduction

We advocate writing functions with a single function, clear structure, and simple interfaces, because overly complex functions bring us a lot of problems: deepen understanding of other developers.CodeDifficult; it is not convenient for testers to compile test cases; it is easy to hide errors; problems are difficult to locate ...... What kind of function is complex? What code smells bad "? Besides relying on experiencedProgramWe can also use tools to measure our functions and code.

Unlike a strict and demanding code reviewer, the code measurement tool does not say to us: "Well ...... This code is terrible! ", It gives us a set of metric values (metrics). How we look at these metric values varies from person to person. For a project team, functions with a circle complexity (one of the measurement values, which will be explained in detail below) of more than 10 need to be reworked, and for another project team, this standard may be reduced to 15. With these measurements, we can learn which methods should be reworked or perform more thorough tests, understand the current state of the project, and track the progress of software development.

Next we will look at a common code measurement tool-sourcemonitor.

Sourcemonitor

1. General Introduction

Sourcemonitor is a free software running on Windows. It can measure the code written in multiple languages, including C, C ++, C #, Java, VB, Delphi, and HTML, and target different languages, outputs different code metric values.

Like other code measurement tools, sourcemonitor only pays attention to the code and provides timely feedback to coding personnel. It is not a project management tool, focus on the entire process from functional analysis to design code and testing in project implementation.

2. C language metric value (C metrics)

As mentioned above, we have not mentioned the core content of code measurement-the measurement value. The following uses the C language metric value as an example to see what information sourcemonitor has provided to us.

Total lines): Number of lines of code, including empty lines;

Statement count (statements): In C, the statement ends with a semicolon. The branch statement if, loop statement for, while, and jump statement goto are all included, and the pre-processing statement # include, # define, and # UNDEF are also included, other pre-processing statements are not calculated. The statements between # else and # endif, # Elif and # endif are ignored;

Branch statement ratio (percent branch statements): The value indicates the proportion of the branch statement to the number of statements. The "branch statement" here refers to the statements that make the program run in an unordered manner, including if, else, for, while, and switch;

Comments ratio (percent lines with comments): This value indicates the comment line (including /*...... */And //...... Form comment) proportion of the total number of rows;

Number of functions): Indicates the number of functions;

Average number of statements contained in each function (average statements per function): This value is obtained by dividing the total number of function statements by the number of functions;

Function complexity)Circle complexity indicates the number of executable paths of a function. The following statement contributes 1 to the Circle complexity value: If/else/For/while statement, ternary operator statement, if/For/while in the judgment condition "&" or "|", switch statement, followed by the case statement of break/goto/return/throw/continue statement, catch/catch t statement;

Function depth (Block depth): The depth of the function indicates the number of nested branches in the function.

For other languages, sourcemonitor outputs different metric values. For example, the C ++ metric value includes the class number (classes), and the HTML contains the number of tags (HTML tags) and the number of hyperlinks (hyperlinks.

3. display style of metric values

Sourcemonitor shows the metric values listed above from several different view levels, including the project view, checkpoint view, and function view.


Project View)

A project created under sourcemonitor must be in a folder. The source code files in this folder can be divided into one or several checkpoints. The project view lists the measurement values of each checkpoint.

Checkpoint View)

The checkpoint view lists the items contained in a checkpoint.Source codeFile measurement value information.

Method View)

The function view shows the measurement information of all functions in a source file under a checkpoint. Double-click a function to jump to the corresponding position of the function in the source file.


Summary

This article introduces the use of the code measurement tool sourcemonitor and the concept of sphere complexity. sourcemonitor helps programmers learn more about their own code. In my opinion, the purpose of using the code measurement tool is to understand the results of our work, ensure the quality of the product at the code level, examine and improve our code, and improve our programming level.

Reference: sourcemonitor help

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.