白盒测试工具 - sonar报告常见示例分析,sonar代码质量问题分析演示
sonar 問題分析
- 第一章:bug分析
- ① 操作符兩邊使用相同的值
- ② 變量、類或函數沒有被定義就使用
- ③ html一些不推薦使用的元素,并提供用哪些來代替
- ④ html 元素沒有包含 lang 屬性
- 第二章:代碼味道
- ① 不要有注釋的代碼,會使代碼臃腫降低可讀性。
- ② 合并可折疊的語句會增加代碼的可讀性。
- ③ 結構中有兩個分支且實現(xiàn)相同容易出現(xiàn)問題,最好合并起來。
- ④ 函數命名不規(guī)范。
- ⑤ 聲明了局部變量但是沒有用,應該刪除來提高可維護性。
- ⑥ 函數過于復雜不利于維護
- ⑦ 重復的字符串文本使重構代碼的過程容易出錯
第一章: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的出現(xiàn),許多舊的元素被拋棄了。為了確保最佳的用戶體驗,不應該使用不贊成的元素。此規(guī)則檢查下列不贊成使用的元素
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.
③ 結構中有兩個分支且實現(xiàn)相同容易出現(xiàn)問題,最好合并起來。
可以看到我的兩個分支實現(xiàn)是一樣的。
譯文: 如果結構中有兩個分支,且實現(xiàn)相同,則最好的情況是重復代碼,最壞的情況是編碼錯誤。如果兩個實例確實需要相同的邏輯,那么應該將它們組合起來。
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.
④ 函數命名不規(guī)范。
譯文: 共享編碼約定允許團隊高效協(xié)作。該規(guī)則檢查所有函數名是否與提供的正則表達式匹配。
Shared coding conventions allow teams to collaborate efficiently. This rule checks that all function names match a provided regular expression.
⑤ 聲明了局部變量但是沒有用,應該刪除來提高可維護性。
譯文: 如果聲明了局部變量但沒有使用,那么它就是死代碼,應該被刪除。這樣做將提高可維護性,因為開發(fā)人員不需要考慮變量的用途。
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.
⑦ 重復的字符串文本使重構代碼的過程容易出錯
我圈住的字符串在代碼里出現(xiàn) 3 次,重構代碼時一定要小心出問題。
譯文: 重復的字符串文本使重構過程容易出錯,因為必須確保更新所有出現(xiàn)的字符串。
Duplicated string literals make the process of refactoring error-prone, since you must be sure to update all occurrences.
喜歡的點個贊?吧!
總結
以上是生活随笔為你收集整理的白盒测试工具 - sonar报告常见示例分析,sonar代码质量问题分析演示的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MySql8.0.19最新版本创建用户分
- 下一篇: windows批量修改文件、文件夹名工具