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

时间:2024-04-08 21:03:52

第一章: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.
白盒测试工具 - sonar报告常见示例分析,sonar代码质量问题分析演示

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

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

Variables, Classes and functions should be defined before they are used, otherwise the code will fail.
白盒测试工具 - sonar报告常见示例分析,sonar代码质量问题分析演示

③ 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
白盒测试工具 - sonar报告常见示例分析,sonar代码质量问题分析演示

④ 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.
白盒测试工具 - sonar报告常见示例分析,sonar代码质量问题分析演示

第二章:代码味道

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

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

Programmers should not comment out code as it bloats programs and reduces readability.白盒测试工具 - sonar报告常见示例分析,sonar代码质量问题分析演示

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

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

Merging collapsible if statements increases the code’s readability.
白盒测试工具 - sonar报告常见示例分析,sonar代码质量问题分析演示

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

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

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.白盒测试工具 - sonar报告常见示例分析,sonar代码质量问题分析演示

④ 函数命名不规范。

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

Shared coding conventions allow teams to collaborate efficiently. This rule checks that all function names match a provided regular expression.白盒测试工具 - sonar报告常见示例分析,sonar代码质量问题分析演示

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

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

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.
白盒测试工具 - sonar报告常见示例分析,sonar代码质量问题分析演示

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

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

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.
白盒测试工具 - sonar报告常见示例分析,sonar代码质量问题分析演示

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

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

Duplicated string literals make the process of refactoring error-prone, since you must be sure to update all occurrences.白盒测试工具 - sonar报告常见示例分析,sonar代码质量问题分析演示

喜欢的点个赞❤吧!