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