圈复杂度(cyclomatic complexity)是一种衡量程序复杂程度的方法。它通过计算程序从开始到结束的线性独立路径数量来评估代码的复杂性。
圈复杂度越高,代码就越复杂,维护难度也随之增加。每遇到关键字如if、while、repeat、for、and、or时,圈复杂度会增加1。对于case语句中的每一种情况,圈复杂度也增加1。
例如,以下函数的圈复杂度为1,意味着代码只有一条路径:
def add(a, b):
return a + b而对于有一条分支的代码,其圈复杂度为2,如下面的递归计算阶乘的代码:
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)圈复杂度的计算公式为:V(G) = E - N + 2P,其中E是控制流图中边的数量,N是控制流图中节点的数量,P是图的连接组件数目。由于控制流图通常是连通的,所以P为1。

| 圈复杂度 | 代码状况 | 可测性 | 维护成本 |
|---|---|---|---|
| 1-10 | 清晰、结构化 | 高 | 低 |
| 11-20 | 复杂 | 中 | 中 |
| 21-30 | 非常复杂 | 低 | 高 |
| >30 | 不可读 | 不可测 | 非常高 |
要测量程序的圈复杂度,可以在Python中使用mccabe包。安装mccabe包的命令如下:
pip install mccabe
然后,使用以下命令检测test.py文件的圈复杂度:
python -m mccabe --min 5 test.py
其中,--min 5表示只输出圈复杂度大于5的函数。例如,输出结果可能会显示91行的roundRobin函数的复杂度为7。

除了mccabe,还有许多其他工具可用于检测圈复杂度:
| 工具 | 类型 | 系统平台 | 扫描语言 |
|---|---|---|---|
| PMD/Checkstyle | 免费 | Windows/Linux/Mac | Java,JS |
| OClint | 免费 | Mac | OC |
| Coverity | 商业 | Windows/Linux/Mac | C/C++,Java,C#,OC/C++,JS,Python,Ruby,PHP |
| SourceMonitor | 免费 | Windows | C/C++,C#,VB.NET,Java,Delphi,VB6,HTML |
| CCM | 免费 | Windows | JS,C/C+,C# |
| HFCCA | 免费 | Windows/Linux/Mac | C/C++,OC |
| Lizard | 免费 | Windows/Linux/Mac | C/C++,Java,C#,JS,OC/C++,Swift,Python,Ruby,TTCN-3,PHP,Scala,GDScript |
为了优化代码质量,可以采取以下措施:

例如,将以下代码:
function test($number){
if($number 替换为:
function test($number){
$number = getMin($number);
for($i = 0; $i if($str == 'China'){
$result = '中国人';
}else if($str == 'US'){
$result = '美国人';
}else if($str == 'France'){
$result = '法国人';
}替换为:
$people = [
'China' => '中国人',
'US' => '美国人',
'France' => '法国人'
];
$result = $people[$str];if((条件1 && 条件2) || !条件1){
return true;
}else{
return false;
}替换为:
if(条件1 && !条件2){
return false;
}
return true;if(do_some_1($number) || do_some_2($number)){
$number = $number.$someStr1.$someStr2.'123456789';
}else{
$number = $number.$someStr3.$someStr4.'123456789';
}替换为:
if(do_some_fun($number)){
$number = do_some_fun1($number);
}else{
$number = do_some_fun2($number);
}if($x 10) return 0; if($z != 0) return 0;
替换为:
if(get_result($x,$y,$z)) return 0;
$bool = false;
foreach($arrs as $arr){
if(!$bool){
if($arr == 1){
someFunction();
$bool = true;
}
if($arr == 2){
someFunction();
$bool = true;
}
}
}替换为:
foreach($arrs as $arr){
if($arr == 1 || $arr == 2){
someFunction();
}
break;
}switch ($cat){
case ‘fish’:
eatFish();
case ‘moss’:
eatMoss();
}
function eatFish() {
echo "Whale eats fish";
}
function eatMoss() {
echo "Whale eat moss";
}替换为:
interface Eat {
function eatFish();
function eatMoss();
}
class Whale implements Eat {
public function eatFish() {
echo "Whale eats fish";
}
public function eatMoss() {
echo "Whale eat moss";
}
}$result = min(lastUsage(), 100) * 0.03;
if(lastUsage() > 100){
$result += (min(lastUsage(), 200) - 100) * 0.05;
}替换为:
$result = getMin(0,100) * 0.03;
$result += getMin(100,200) * 0.03;
function getMin($start, $end){
if(lastUsage() > $start){
return (min(lastUsage(),$end) - $start);
}
return 0;
}if($name == 'width'){
$width = $value;
} else if ($name == 'height'){
$height = $value;
}替换为:
function setWidth($value){
$width = $value;
}
function setHeight($value){
$height = $value;
}参考视频:谷歌:简洁代码之道
以上就是圈复杂度和代码质量优化(附带示例代码纠正代码质量)的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号