![Java代碼飛檢案例輸出_第1頁](http://file3.renrendoc.com/fileroot_temp3/2022-7/3/f538cd77-ba53-4f6b-ac57-0da10c033b8f/f538cd77-ba53-4f6b-ac57-0da10c033b8f1.gif)
![Java代碼飛檢案例輸出_第2頁](http://file3.renrendoc.com/fileroot_temp3/2022-7/3/f538cd77-ba53-4f6b-ac57-0da10c033b8f/f538cd77-ba53-4f6b-ac57-0da10c033b8f2.gif)
![Java代碼飛檢案例輸出_第3頁](http://file3.renrendoc.com/fileroot_temp3/2022-7/3/f538cd77-ba53-4f6b-ac57-0da10c033b8f/f538cd77-ba53-4f6b-ac57-0da10c033b8f3.gif)
![Java代碼飛檢案例輸出_第4頁](http://file3.renrendoc.com/fileroot_temp3/2022-7/3/f538cd77-ba53-4f6b-ac57-0da10c033b8f/f538cd77-ba53-4f6b-ac57-0da10c033b8f4.gif)
![Java代碼飛檢案例輸出_第5頁](http://file3.renrendoc.com/fileroot_temp3/2022-7/3/f538cd77-ba53-4f6b-ac57-0da10c033b8f/f538cd77-ba53-4f6b-ac57-0da10c033b8f5.gif)
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、文檔名稱文檔密級1 類、方法和變量缺少注釋1.1 【問題描述】變量沒有注釋:方法沒有注釋:類沒有注釋: 文件名:ConfigNetworkService.java起始行:40上下文:public class ConfigNetworkService【問題解讀】 變量、方法缺少注釋,影響代碼走讀效率,對java類沒有做注釋說明,會導致使用該類的人不知道這個類的功能是什么【糾正措施】在變量、方法和類上面添加必要的注釋,方便后來維護者維護理解【舉一反三】平時開發(fā)是嚴格按照華為java編程規(guī)范編碼2 代碼注釋未與上方代碼空行隔開2.1 【問題描述】為了提高代碼的可讀性,跟層次感,注釋應該跟上面的代碼空
2、一行緊貼下面代碼(以DGAlarmService.java為例)/* * 獲取油機的原始當前告警列表 * * param dgInfo * return */ public List<OmsAlarm> getOrginalCurAlarms(DGInfo dgInfo) List<String> tempDns = dgInfo.getDgDnList(); List<OmsAlarm> currentAlarmList = getDGAlarmsBySiteDn(dgInfo.getSiteDN(); / 告警分2套,原來的告警可以取到subFdn,新的
3、告警取不到,只有Mocid和InsId if (currentAlarmList != null) return currentAlarmList; 在上面的代碼中紅色字體中的注釋沒有跟上面代碼中空一行或者跟下面代碼空一行,都不符合編碼規(guī)范?!締栴}解讀】為了提高代碼的可讀性(減少歧義)跟層次感,代碼規(guī)范要求注釋應該跟上面的代碼空一行,緊貼下面代碼行?!炯m正措施】根據(jù)編碼規(guī)范,該場景的解決方法如下:/* * 獲取油機的原始當前告警列表 * * param dgInfo * return */ public List<OmsAlarm> getOrginalCurAlarms(DGIn
4、fo dgInfo) List<String> tempDns = dgInfo.getDgDnList(); List<OmsAlarm> currentAlarmList = getDGAlarmsBySiteDn(dgInfo.getSiteDN(); / 告警分2套,原來的告警可以取到subFdn,新的告警取不到,只有Mocid和InsId if (null != currentAlarmList) return currentAlarmList; 【舉一反三】在編碼中為了之后的維護,我們要增強代碼的可讀性我們在編碼的過程中應適當加一些注釋,提高代碼的層次感,以
5、方便維護人員能更好的維護。3 對象判斷是否為空,應該把Null放在前面(常量和變量作比較未把常量放在前面)3.1 【問題描述】判斷一個對象是否為null時,應該把null放在前面如if(null = XXX),null放后面不符合編碼規(guī)范(以DGAlarmService.java為例)/* * 獲取油機的原始當前告警列表 * * param dgInfo * return */ public List<OmsAlarm> getOrginalCurAlarms(DGInfo dgInfo) List<String> tempDns = dgInfo.getDgDnLis
6、t(); List<OmsAlarm> currentAlarmList = getDGAlarmsBySiteDn(dgInfo.getSiteDN(); / 告警分2套,原來的告警可以取到subFdn,新的告警取不到,只有Mocid和InsId if (currentAlarmList != null) String subFdn = null; String mocId = null; String insId = null; for (Iterator<OmsAlarm> it = currentAlarmList.iterator(); it.hasNext(
7、);) Properties userData = it.next().getUserData(); subFdn = userData.getProperty(FmDataPropertyDefine.PROP_SUB_FDN); if (subFdn = null | subFdn.isEmpty() mocId = userData.getProperty(FmDataPropertyDefine.PROP_MOC_ID); insId = userData.getProperty(FmDataPropertyDefine.PROP_INS_ID); if (!tempDns.conta
8、ins(mocId + "_" + insId) it.remove(); else if (!tempDns.contains(subFdn) it.remove(); return currentAlarmList; 在上面的代碼中紅色字體中的空判斷雖然不會對系統(tǒng)跟邏輯產(chǎn)生影響但不符合編碼規(guī)范?!締栴}解讀】為了防止程序報空指針異常,對象為null的比較經(jīng)常會用到,按照編碼規(guī)范來可以養(yǎng)成良好的習慣,減少程序異常率?!炯m正措施】根據(jù)編碼規(guī)范,該場景的解決方法如下:/* * 獲取油機的原始當前告警列表 * * param dgInfo * return */ public L
9、ist<OmsAlarm> getOrginalCurAlarms(DGInfo dgInfo) List<String> tempDns = dgInfo.getDgDnList(); List<OmsAlarm> currentAlarmList = getDGAlarmsBySiteDn(dgInfo.getSiteDN(); / 告警分2套,原來的告警可以取到subFdn,新的告警取不到,只有Mocid和InsId if (null != currentAlarmList) String subFdn = null; String mocId = n
10、ull; String insId = null; for (Iterator<OmsAlarm> it = currentAlarmList.iterator(); it.hasNext();) Properties userData = it.next().getUserData(); subFdn = userData.getProperty(FmDataPropertyDefine.PROP_SUB_FDN); if (null = subFdn | subFdn.isEmpty() mocId = userData.getProperty(FmDataPropertyDe
11、fine.PROP_MOC_ID); insId = userData.getProperty(FmDataPropertyDefine.PROP_INS_ID); if (!tempDns.contains(mocId + "_" + insId) it.remove(); else if (!tempDns.contains(subFdn) it.remove(); return currentAlarmList; 【舉一反三】在編碼中我們要防止null對象的引用,防止因拋空指針程序無法正常運行,所以對Null的判斷要按照編碼規(guī)范來,比方對二個對象進行equals比較,
12、就應該把有可能為Null的放在equals里面,把不可能為null的放equals前面。對null進行 = 比較時,把null放在前面在閱讀代碼時也能讓一眼就看出做了Null 判斷4 代碼包含很多 Tab 鍵4.1 【問題描述】【問題解讀】程序塊縮進風格不規(guī)范,采用的是 Tab 鍵縮進方式,違反規(guī)則程序塊要采用縮進風格編寫,縮進的空格數(shù)為4個,不允許使用TAB縮進【糾正措施】導入項目組通用的代碼格式模板樣式【舉一反三】導入項目組通用的代碼格式模板樣式5 重復代碼5.1 【問題描述】【問題解讀】 重復代碼,影響系統(tǒng)運行效率【糾正措施】把重復代碼重構(gòu)為公共方法調(diào)用【舉一反三】 平時編碼嚴格按照華為
13、java編程規(guī)范編寫,重復代碼可以重構(gòu)公共方法6 “”未換行,出現(xiàn) 100 多次6.1 【問題描述】【問題解讀】 違法規(guī)則分界符(如大括號)【糾正措施】 應各獨占一行【舉一反三】 平時編碼嚴格按照華為java編程規(guī)范編寫7 注釋有誤/方法命名不規(guī)范7.1 【問題描述】【問題解讀】 違反華為的Java編碼規(guī)范。錯誤的注釋對不了解系統(tǒng)的員工產(chǎn)生誤導。降低工作交接、定位解決問題的效率?!炯m正措施】 在代碼調(diào)整后,應該同步更新相關(guān)注釋?!九e一反三】 方法的注釋應一句話介紹方法的功能及使用場景。還應介紹輸入?yún)?shù)條件限制條件,出差參數(shù)的含義等。8 對象、字符串強轉(zhuǎn)之前沒有非空判斷8.1 【問題描述】對一個
14、對象、字符串進行強制轉(zhuǎn)換時,必須要先做類型、非空判斷(包含空字符)。若未做校驗,可能直接導致功能不可用。(QueryRemoteHAStatusTask.java) Override public int getTimeout() String stringtimeout=getConfigName("timeout"); if (null != stringtimeout) int timeout = Integer.valueOf(stringtimeout); LOGGER.info("timeout valure is: " + timeout)
15、; return timeout; else return 600000; 在上面的代碼中紅色字體中的方法有可能會返回空字符串,而黃色字體處未對空字符串添加判斷就進行類型強制轉(zhuǎn)換,會報異常?!締栴}解讀】為了防止程序報類型轉(zhuǎn)換異常,在對對象、字符串進行強制轉(zhuǎn)換時,添加必要的判斷是必須的,否則會導致功能失效。【糾正措施】此處正確的判斷應該為(添加代碼中加粗代碼的判斷): Override public int getTimeout() String stringtimeout=getConfigName("timeout"); if (null != stringtimeout
16、 && !"".equals(stringtimeout) int timeout = Integer.valueOf(stringtimeout); LOGGER.info("timeout valure is: " + timeout); return timeout; else return 600000; 【舉一反三】在編碼中經(jīng)常會用到強制轉(zhuǎn)換,如果必要的校驗沒有處理的話,會導致異常,從而可能功能不可用的現(xiàn)象。如對象的強轉(zhuǎn),那么我們在強轉(zhuǎn)之前必須添加:if (name instanceof type) 類似的判斷,若是字符串的強轉(zhuǎn)
17、,就必須添加:if (null != string && !"".equals(string)判斷。9 對校驗的充分考慮能提高代碼的可讀性9.1 【問題描述】為了提高代碼的可讀性,不應該添加沒必要的判斷以及必要的校驗需要充分考慮添加或者去除:(RpcQueryHAStatus.java) public static String getConfigName(String configName) String path = getOSSPath() + File.separator + "engr" + File.separator + &
18、quot;engineering" + File.separator + "ha_review" + File.separator + "change_timeout.conf" File file = new File(path); FileInputStream fis = null; String configValues = "" try fis = new FileInputStream(file); catch (FileNotFoundException e) LOGGER.error("getCon
19、figName exception: " + e.getMessage(); InputStreamReader read = new InputStreamReader(fis); BufferedReader br = new BufferedReader(read); String line = "" StringBuffer sb = new StringBuffer(); try while (true) line = br.readLine(); if(null = line) break; if(line.indexOf(configName) !=
20、 -1) sb.append(line); catch (IOException e) LOGGER.error("getConfigName exception: " + e.getMessage(); if (null != sb.toString() String str = sb.toString().split("="); configValues = str1.trim(); LOGGER.info("getConfigName success: the key " + configName +" value i
21、s :" + configValues); else LOGGER.error("getConfigName error: the file: " + path +" doesn't have the key :" + configName); try if (null != br) br.close(); if (null != read) read.close(); if (null != fis) fis.close(); catch (IOException e) LOGGER.error("getConfigName
22、 exception: " + e.getMessage(); return configValues; 在上面的代碼中紅色字體中代碼StringBuffer 在聲明的時候已經(jīng)new了一個對象,后面的null判斷永遠為true(null != sb.toString() ),且在判斷里對數(shù)組直接使用:configValues = str1.trim(),如果數(shù)組的大小不大于等于2的話會報數(shù)組越界?!締栴}解讀】為提高代碼的可讀性,非必要的判斷應該去掉,以及對數(shù)組大小的校驗需要添加?!炯m正措施】 public static String getConfigName(String conf
23、igName) String path = getOSSPath() + File.separator + "engr" + File.separator + "engineering" + File.separator + "ha_review" + File.separator + "change_timeout.conf" File file = new File(path); FileInputStream fis = null; String configValues = "" try
24、 fis = new FileInputStream(file); catch (FileNotFoundException e) LOGGER.error("getConfigName exception: " + e.getMessage(); InputStreamReader read = new InputStreamReader(fis); BufferedReader br = new BufferedReader(read); String line = "" StringBuffer sb = new StringBuffer(); t
25、ry while (true) line = br.readLine(); if(null = line) break; if(line.indexOf(configName) != -1) sb.append(line); catch (IOException e) LOGGER.error("getConfigName exception: " + e.getMessage(); String str = sb.toString().split("="); if (str.length >= 2) configValues = str1.tri
26、m(); LOGGER.info("getConfigName success: the key " + configName +" value is :" + configValues); else LOGGER.error("getConfigName error: the file: " + path +" doesn't have the key :" + configName); try if (null != br) br.close(); if (null != read) read.clos
27、e(); if (null != fis) fis.close(); catch (IOException e) LOGGER.error("getConfigName exception: " + e.getMessage(); return configValues; 【舉一反三】在編碼中對于非必要的校驗以及必要的校驗需要充分考慮到,只有充分考慮了這些情況,寫出來的代碼的可讀性才可能直觀,后續(xù)維護也更容易。10 校驗的順序以及變量的抽取能有效的提高性能10.1 【問題描述】校驗的順序能夠有效的提高性能以及內(nèi)存的使用,局部變量的抽取能有效的提高效率。(ParseResul
28、tXmlTask.java)private static void parseDesc(final String lang, final Element desc, final ErrorInfo errorInfo, final List<CheckItem> checkItemList) throws DataConversionException final String type = desc.getAttributeValue("type"); if (null= lang | HAStatusUtil.isEmpty(type) return; fi
29、nal CheckItem checkItem = new CheckItem(); checkItem.setCheckSubItemList(new ArrayList<CheckSubItem>(); checkItem.setItemType(type); final List<Element> scriptList = desc.getChildren("SCRIPT"); if (HAStatusUtil.isEmpty(scriptList) return; for (final Element script : scriptList)
30、 final int id = script.getChild("ID").getAttribute("name").getIntValue(); final int result = script.getChild("RESULT").getAttribute("name").getIntValue(); final String enName = script.getChild("check_item_en").getAttribute("name").getValue(
31、); final String cnName = script.getChild("check_item_cn").getAttribute("name").getValue(); final String alarmLevel = script.getChild("alarm_level").getAttribute("name").getValue(); final String enDesc = script.getChild("desc_en").getAttribute("n
32、ame").getValue(); final String cnDesc = script.getChild("desc_cn").getAttribute("name").getValue(); final CheckSubItem checkSubItem = new CheckSubItem(); checkSubItem.setSubItemID(id); checkSubItem.setSubItemType("zh_CN".equals(lang) ? cnName : enName); checkSubIte
33、m.setAlarmLevel(HAStatusUtil.getAlarmLevel(alarmLevel); checkSubItem.getErrorInfo().setErrorCode(result); checkSubItem.getErrorInfo().setErrorMsg("zh_CN".equals(lang) ? cnDesc : enDesc); if (HAStatusConstant.ErrorCode.NOT_SUPPORT = result) continue; / 設(shè)置檢查大項的值 if (HAStatusConstant.SUCCESS
34、!= result) checkItem.getErrorInfo().setErrorCode(result); if (HAStatusUtil.getAlarmLevel(alarmLevel) > checkItem.getAlarmLevel() checkItem.setAlarmLevel(HAStatusUtil.getAlarmLevel(alarmLevel); checkItem.getCheckSubItemList().add(checkSubItem); if (!HAStatusUtil.isEmpty(checkItem.getCheckSubItemLi
35、st() checkItemList.add(checkItem); 在上面的代碼中標紅的判斷位置不對,可能會導致內(nèi)存消耗過大;標紅加粗代碼,數(shù)據(jù)量大的時候可能會導致效率低,性能存在問題。【問題解讀】在編碼過程中,所需的校驗應該在方法(循環(huán))的入口,這樣可以避免不必要的內(nèi)存開銷,而局部變量的抽取,多次使用的值,抽取成局部變量,對性能是有很大的提高的,不需要每次使用都需要去取值?!炯m正措施】校驗放在方法(循環(huán))入口,多次使用的值,抽取成局部變量: SuppressWarnings("unchecked") private static void parseDesc(final
36、 String lang, final Element desc, final ErrorInfo errorInfo, final List<CheckItem> checkItemList) throws DataConversionException final String type = desc.getAttributeValue("type"); if (null= lang | HAStatusUtil.isEmpty(type) return; final CheckItem checkItem = new CheckItem(); checkI
37、tem.setCheckSubItemList(new ArrayList<CheckSubItem>(); checkItem.setItemType(type); final List<Element> scriptList = desc.getChildren("SCRIPT"); if (HAStatusUtil.isEmpty(scriptList) return; int tempAlarmLevel = 0; for (final Element script : scriptList) final int result = scrip
38、t.getChild("RESULT").getAttribute("name").getIntValue(); if (HAStatusConstant.ErrorCode.NOT_SUPPORT = result) continue; final int id = script.getChild("ID").getAttribute("name").getIntValue(); final String enName = script.getChild("check_item_en").ge
39、tAttribute("name").getValue(); final String cnName = script.getChild("check_item_cn").getAttribute("name").getValue(); final String alarmLevel = script.getChild("alarm_level").getAttribute("name").getValue(); final String enDesc = script.getChild(&qu
40、ot;desc_en").getAttribute("name").getValue(); final String cnDesc = script.getChild("desc_cn").getAttribute("name").getValue(); final CheckSubItem checkSubItem = new CheckSubItem(); checkSubItem.setSubItemID(id); checkSubItem.setSubItemType("zh_CN".equals
41、(lang) ? cnName : enName); tempAlarmLevel = HAStatusUtil.getAlarmLevel(alarmLevel); checkSubItem.setAlarmLevel(tempAlarmLevel); checkSubItem.getErrorInfo().setErrorCode(result); checkSubItem.getErrorInfo().setErrorMsg("zh_CN".equals(lang) ? cnDesc : enDesc); / 設(shè)置檢查大項的值 if (HAStatusConstant
42、.SUCCESS != result) checkItem.getErrorInfo().setErrorCode(result); if (tempAlarmLevel > checkItem.getAlarmLevel() checkItem.setAlarmLevel(tempAlarmLevel); checkItem.getCheckSubItemList().add(checkSubItem); if (!HAStatusUtil.isEmpty(checkItem.getCheckSubItemList() checkItemList.add(checkItem); 【舉一
43、反三】在編碼中對于校驗的位置,我們是盡可能的減少開銷,提高效率,校驗一般都放在入口,而校驗順序一般是:權(quán)限校驗 -參數(shù)合法性校驗 - 需求特定的條件性校驗。11 固定值字符串需要定義成靜態(tài)常量11.1 【問題描述】為了提高代碼的可讀性,一些特殊的固定值的字符串要定義成靜態(tài)常量,并且準確命名常量,方便維護的時候理解和閱讀。(以BatchUpgradeNeByModbusExecutor.java為例)if ("WHOLE_VERSION".equals(neVersionPackage.getReleaseType() / 標識升級包 releaseType = "
44、0xE0"else / 標識補丁包 releaseType = "0xE1"在上面的代碼中紅色字體中的字符串應該用靜態(tài)常量表示【問題解讀】為了提高代碼的可閱讀性,以及方便后面的代碼維護修改,應該將固定值的字符串定義成靜態(tài)常量。【糾正措施】根據(jù)編碼規(guī)范,該場景的解決方法如下:public static final String RELEANSE_TYPE_WHOLE_VERSION = "WHOLE_VERSION"public static final String RELEANSE_TYPE_UPGRADE_CODE = "0xE0"public static final String RELEANSE_TYPE_PATCH_CODE = "0xE1"if(RELEANSE_TYPE_WHOLE_VERSION.equals(neVersionPackage.getReleaseType() / 標識升級包 releaseType = RELEANSE_TYPE_UPGRADE_CODE;else / 標識補丁包 releaseType = RELEANSE_TYPE_PATCH_CODE;【舉一反三】在編碼中為了之后的維護,我們要增強代碼的可讀性我們在編碼的過程中應適當加
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 個人借款協(xié)議財產(chǎn)抵押
- 城市亮化與景觀設(shè)計方案
- 智能語音設(shè)備銷售及服務合同
- 影視制作服務中版權(quán)糾紛與免責條款的協(xié)議
- 惠州學院教師進修協(xié)議書
- 意向租賃合同年
- 網(wǎng)絡科技業(yè)物聯(lián)網(wǎng)技術(shù)應用與發(fā)展規(guī)劃方案設(shè)計
- 喪葬禮儀服務契約合同書
- 農(nóng)業(yè)技術(shù)創(chuàng)新與研發(fā)方案
- 生物制藥研發(fā)項目投資合同
- 孔子仁學思想
- 六年級下冊綜合實踐活動教案(II)
- 高中英語常用詞匯表(動詞、名詞、形容詞和副詞)
- 下肢深靜脈血栓形成靜脈置管溶栓術(shù)后-用藥及出血觀察護理-PPT
- 16萬噸_年液化氣綜合利用裝置廢酸環(huán)保綜合利用項目環(huán)境報告書
- T∕CAEPI 43-2022 電絮凝法污水處理技術(shù)規(guī)程
- 農(nóng)村商業(yè)銀行合規(guī)風險管理暫行辦法
- 品牌簡單之道講義
- 更高更妙的物理《摩擦角與自鎖現(xiàn)象》精講
- 水轉(zhuǎn)印檢驗規(guī)范(吉利)
- 魯教版五四制七年級上冊英語單元題
評論
0/150
提交評論