歡迎您光臨本站 註冊首頁

坑爹代碼 | 循環+條件判斷,你最多能嵌套幾層?

←手機掃碼閱讀     admin @ 2019-06-03 , reply:0

for 循環和 if 條件判斷語句,必不可少吧。但是你見過最多嵌套的循環和條件判斷有幾層呢?或者說,你最多能容忍多少層的嵌套呢?

我們還是先來看看極端的坑爹代碼吧:


// 這個無限循環嵌套,只是總循環的一部分。。。我已經繞暈在黃桷灣立交
if (recordList.size() > start) {
    for (int i = start; i < end; i++) {
        Map<String, Object> map = recordList.get(i);
        Map<String, Object> field11 = (Map<String, Object>) map.get("field"); //name -> code
        Map<String, Object> record11 = (Map<String, Object>) map.get("record"); // code -> value
        String catagory1 = map.get("categoryId").toString();
        //  查詢第一種類型對應的其他類型
        SalaryDataVo ss = JSON.parseObject(JSON.toJSONString(map), SalaryDataVo.class);
        Page page3 = salaryManagerService.getAllRecordsByCondition(ss);
        if (page3.getRecords().size() > 0) {
            List<Map<String, Object>> salaryDataVos = page3.getRecords();
            salaryDataVos = this.reSetMap(salaryDataVos,null,null);
            for (Map<String, Object> map2 : salaryDataVos) {
                Map<String, Object> field2 = (Map<String, Object>) map2.get("field");
                Map<String, Object> record2 = (Map<String, Object>) map2.get("record");
                String catagory2 = map2.get("categoryId").toString();
                List<SalaryGroupVO> groupList2 = salaryGroupService.getSalaryGroupsItems(this.getUserCorpId(), catagory2);
                for (SalaryGroupVO cc : groupList2) {
                    cc.setCode(cc.getParentId() + cc.getCode());
                }
                //計算
                for (Map.Entry<String, Object> entity : field2.entrySet()) {
                    String keyName = entity.getKey();
                    for (SalaryGroupVO s2 : groupList2) {
                        if ("bigDecimal".equals(s2.getItemType()) && s2.getCode().equals(field2.get(keyName).toString()) && ("部門" != keyName) && ("姓名" != keyName) && StringUtils.isNotEmpty(s2.getItemType())) {
                            if (field11.containsKey(keyName)) {
                                if (field11.containsKey(keyName)) {
                                    String code1 = field11.get(keyName).toString();
                                    Double newValue = 0.0;
                                    Double oldValue=0.0;
                                    if (!record11.get(code1).toString().matches("^[0-9]*$")) {
                                        oldValue = Double.parseDouble(record11.get(code1).toString());
                                        if (record2.containsKey(entity.getValue().toString()) && (!record2.get(entity.getValue().toString()).toString().matches("^[0-9]*$"))) {
                                            String value2 = record2.get(entity.getValue().toString()).toString();
                                            newValue = Double.parseDouble(value2);
                                        }
                                        record11.remove(field11.get(keyName).toString());
                                    }

我數了數,一共有 11 層的嵌套!!!

吐槽歸吐槽,這樣的代碼邏輯有什麼重構的好方法嗎?

到下面鏈接發表評論,領取獎品:

https://gitee.com/oschina/bullshit-codes/blob/master/java/InfiniteCycle

碼雲 6 周年,我們正在徵集各種坑爹代碼,很多獎品等你來拿

詳細的參與方法請看  https://gitee.com/oschina/bullshit-codes


[admin ]

來源:OsChina
連結:https://www.oschina.net/news/107151/nesting-bad-code
坑爹代碼 | 循環+條件判斷,你最多能嵌套幾層?已經有258次圍觀

http://coctec.com/news/all/show-post-206370.html