1 Introduction
When a software application develops to an appropriate time, "refactoring" is an inevitable task in the development process. Refactor the code to adapt to diverse scenarios that were not considered at the beginning of the current module design, and increase the maintainability, robustness, and testability of the module. So, how to clarify the direction of reconstruction and quantify the results of reconstruction? Code cyclomatic complexity can be an alternative metric. The following describes how to obtain the code circle complexity of the application for online monitoring, and provide data support for the complexity of the review program.
2 Background knowledge
2.1 Cyclomatic complexity
Cyclomatic complexity (CC for short), also known as conditional complexity, is a measure of code complexity. Proposed by Thomas J. McCabe, Sr. in 1976, it is used to express the complexity of a program, and its symbol is VG or M. It can be used to measure the complexity of the decision structure of a module, which is expressed as the number of independent current paths, and can also be understood as the number of test cases that cover all possible situations with the least use. The large cyclomatic complexity indicates that the judgment logic of the program code is complex, the quality may be low, and it is difficult to test and maintain. The possible bugs of a program have a lot to do with high cyclomatic complexity.
2.2 Cyclomatic complexity
The calculation method is commonly used to calculate the cyclomatic complexity of the structure: the sequential structure has a complexity of 1.
if-else-else, switch-case: Every time a branch is added, the complexity increases by 1, and the && and || operations are also a branch.
Looping structure: Add a looping structure and increase the complexity by 1.
return: Add a return statement, and the complexity will increase by 1.
2.3 Cyclomatic complexity metrics
The relatively recognized measurement data in the industry is listed above. In fact, this is completely determined by your own business volume and project situation. Suppose your business is very simple, and it is a single application with simple CRUD functions, then your cyclomatic complexity is not so easy even if you think about it. At this point you can choose to set the reconstruction threshold for cyclomatic complexity to 10.
Assuming that your business is very complex and involves multiple other microservice system calls, plus the judgment of corner cases in various businesses, the cyclomatic complexity of 100 may not be a problem.
2.4 The Cyclomatic Complexity Reduction Method
1) Function extraction and splitting, splitting a single responsibility into sub-functions. Each function must have a clear function implementation. Do not merge functions to achieve a small number of lines. Logic modules and data modules should be written separately
2) Optimize the algorithm to reduce unnecessary conditions and loop branches, use if ... else ... as little as possible, and use ternary expressions to replace if else
3) Expression logic optimizes combined conditional expressions, such as using a || b || c
4) Reduce early return3
Program Overview 3.1
script design
1) Development language
python
2) Dependent environment
lizard
APScheduler
smtplib
pymysql
3) Script Architecture
3.2 Function introduction
1) Support search language range:
Supports 15 development languages, including commonly used languages as follows
C/C++ (works with C++14)
Java
C# (C Sharp)
JavaScript (With ES6 and JSX)
Python
Golang
2) Scan parameters
Configuration instructions: use lizard to perform scanning, the commonly used commands are as follows:
Configure the inspection scope:
List the programming languages to analyze. If left blank, all supported languages will be searched.
-l LANGUAGES, --languages LANGUAGES
Exclude files matching the pattern. match everything? Matching any single character, "/folder/" recursively excludes everything in the folder. Multiple modes can be specified. Don't forget to put "" around the pattern.
-x EXCLUDE, --exclude EXCLUDE
Set whitelist, default './whitelizard.txt'
-W WHITELIST, --whitelist WHITELIST
Configure threshold warnings:
Threshold for cyclomatic complexity warnings, the default value is 15, >15 will generate warnings.
-C CCN, --CCN CCN
Sets the limit on the number of fields. Can be lines of code, cyclomatic complexity, tokens, parameters or custom fields. If the function setting exceeds the limit, an alarm will be issued.
-T THRESHOLDS, --Threshold THRESHOLDS
Configure report output:
Output to file according to format
-o OUTPUT_FILE, --output_file OUTPUT_FILE
Official website address: http://www.lizard.ws
Source address: https://github.com/terryyin/lizard
3) Execute the scan task regularly:
Create scheduling tasks through BackgroundScheduler, automatically trigger scanning methods, and write the results to the library
def dojob(): scheduler = BackgroundScheduler() scheduler.add_job(func, "cron", hour=21, minute=30) scheduler.start()
3.3 Display of results
3.3.1 Explanation of report terms
Cyclomatic complexity, the cyclomatic complexity is also the branch complexity, it is best to keep it below 15, the current script sets the threshold to 10.
LOC, the number of lines of code including comments, currently sets a threshold of 200.
Token count, the number of tokens, a program can have up to 8192 tokens, each token is a word, such as keywords, identifiers, constants, punctuation, operators.
Count brackets and strings as 1 token. Commas, periods, LOCALs, semicolons, ENDs, and comments are not counted.
Parameter count, the parameter count is the number of parameters of the function. Currently, the script sets a threshold of 10.
3.3.2 Execution result display
Run the script in the Windows environment, enter the file_root (file address) to scan, and support the automatic pop-up browser to display the Html report of this operation.
Executed on a weekly basis, scanned according to the system dimension, and supported triggering email notifications for corresponding system R&D to view the method name that exceeds the threshold
3.3.3 Application Data Monitoring
Regularly pull the latest code of the specified branch every week, perform file analysis, store scan results, and display through data charts
4 Summary
For the measurement of software code quality, cyclomatic complexity can be used as a reference index. R&D can reduce the cyclomatic complexity of modules (functions) by refining split functions, optimizing algorithms, and optimizing logical expressions. The above describes an online monitoring method for cyclomatic complexity. Only by making good use of online data and combining existing team projects can a better practical mechanism be formed.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。