前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >白盒测试工具 - sonar报告常见示例分析,sonar代码质量问题分析演示

白盒测试工具 - sonar报告常见示例分析,sonar代码质量问题分析演示

作者头像
小蓝枣
发布2020-09-24 09:55:48
2.1K0
发布2020-09-24 09:55:48
举报
文章被收录于专栏:CSDN博客专家-小蓝枣的博客

第一章:bug分析

① 操作符两边使用相同的值

译文: 在二进制操作符的两边使用相同的值几乎总是错误的。在逻辑操作符的情况下,它要么是一个复制/粘贴错误,因此是一个bug,要么只是浪费代码,应该进行简化。在逐位运算符和大多数二进制数学运算符的情况下,在运算符的两边都有相同的值会产生可预测的结果,应该加以简化。

Using the same value on either side of a binary operator is almost always a mistake. In the case of logical operators, it is either a copy/paste error and therefore a bug, or it is simply wasted code, and should be simplified. In the case of bitwise operators and most binary mathematical operators, having the same value on both sides of an operator yields predictable results, and should be simplified.

在这里插入图片描述
在这里插入图片描述

② 变量、类或函数没有被定义就使用

译文: 变量、类和函数应该在使用之前定义,否则代码会失败。

Variables, Classes and functions should be defined before they are used, otherwise the code will fail.

在这里插入图片描述
在这里插入图片描述

③ html一些不推荐使用的元素,并提供用哪些来代替

不推荐用 font 来设置字体,建议使用 css译文: 随着HTML5的出现,许多旧的元素被抛弃了。为了确保最佳的用户体验,不应该使用不赞成的元素。此规则检查下列不赞成使用的元素

With the advent of HTML5, many old elements were deprecated. To ensure the best user experience, deprecated elements should not be used. This rule checks for the following deprecated elements

在这里插入图片描述
在这里插入图片描述

④ html元素没有包含lang属性

译文: <html>元素应该提供lang和/或xml:lang属性,以便识别文档的默认语言。

The element should provide the lang and/or xml:lang attribute in order to identify the default language of a document.

在这里插入图片描述
在这里插入图片描述

第二章:代码味道

① 不要有注释的代码,会使代码臃肿降低可读性。

译文: 程序员不应该注释掉代码,因为代码会使程序臃肿,降低可读性。

Programmers should not comment out code as it bloats programs and reduces readability.

在这里插入图片描述
在这里插入图片描述

② 合并可折叠的语句会增加代码的可读性。

两个 if 条件语句嵌套,完全可以用一个 if ,加上 and 连接词即可。 译文: 合并可折叠的语句会增加代码的可读性。

Merging collapsible if statements increases the code’s readability.

在这里插入图片描述
在这里插入图片描述

③ 结构中有两个分支且实现相同容易出现问题,最好合并起来。

可以看到我的两个分支实现是一样的。 译文: 如果结构中有两个分支,且实现相同,则最好的情况是重复代码,最坏的情况是编码错误。如果两个实例确实需要相同的逻辑,那么应该将它们组合起来。

Having two branches in the same if structure with the same implementation is at best duplicate code, and at worst a coding error. If the same logic is truly needed for both instances, then they should be combined.

在这里插入图片描述
在这里插入图片描述

④ 函数命名不规范。

译文: 共享编码约定允许团队高效协作。该规则检查所有函数名是否与提供的正则表达式匹配。

Shared coding conventions allow teams to collaborate efficiently. This rule checks that all function names match a provided regular expression.

在这里插入图片描述
在这里插入图片描述

⑤ 声明了局部变量但是没有用,应该删除来提高可维护性。

译文: 如果声明了局部变量但没有使用,那么它就是死代码,应该被删除。这样做将提高可维护性,因为开发人员不需要考虑变量的用途。

If a local variable is declared but not used, it is dead code and should be removed. Doing so will improve maintainability because developers will not wonder what the variable is used for.

在这里插入图片描述
在这里插入图片描述

⑥ 函数过于复杂不利于维护

译文: 认知复杂性是衡量一个函数的控制流有多难以理解的一个指标。具有高度认知复杂性的功能将难以维持。

Cognitive Complexity is a measure of how hard the control flow of a function is to understand. Functions with high Cognitive Complexity will be difficult to maintain.

在这里插入图片描述
在这里插入图片描述

⑦ 重复的字符串文本使重构代码的过程容易出错

我圈住的字符串在代码里出现 3 次,重构代码时一定要小心出问题。 译文: 重复的字符串文本使重构过程容易出错,因为必须确保更新所有出现的字符串。

Duplicated string literals make the process of refactoring error-prone, since you must be sure to update all occurrences.

在这里插入图片描述
在这里插入图片描述
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020/05/29 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 第一章:bug分析
  • ① 操作符两边使用相同的值
  • ② 变量、类或函数没有被定义就使用
  • ③ html一些不推荐使用的元素,并提供用哪些来代替
  • ④ html元素没有包含lang属性
  • 第二章:代码味道
  • ① 不要有注释的代码,会使代码臃肿降低可读性。
  • ② 合并可折叠的语句会增加代码的可读性。
  • ③ 结构中有两个分支且实现相同容易出现问题,最好合并起来。
  • ④ 函数命名不规范。
  • ⑤ 声明了局部变量但是没有用,应该删除来提高可维护性。
  • ⑥ 函数过于复杂不利于维护
  • ⑦ 重复的字符串文本使重构代码的过程容易出错
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档