一段代码的clean code

pre

public synchronized List queryAccountViaTag(String tag, Boolean isLock) {
        List acc = new ArrayList<>();
        if (tag.contains("&") && !tag.contains("_sc") && !tag.contains("_dc") && !tag.contains("_conference")) {
            String[] separateTag = tag.split("&");
            for (String getTag : separateTag) {
                acc.addAll(queryAccountFromDB(getTag, 1, "RM", isLock));
            }
        } else if (tag.contains("&") && (tag.contains("_sc") || tag.contains("_dc") || tag.contains("_conference"))) {
            String[] separateTag = tag.split("&");
            List tagList = Arrays.asList(separateTag);
            Set uniqueSet = new HashSet<>(tagList);
            Map map = new HashMap();
            for (String temp : uniqueSet) {
                Integer count = Collections.frequency(tagList, temp);
                map.put(temp, count);
            }

            for (Map.Entry entry : map.entrySet()) {
                logger.info("Key = " + entry.getKey() + ", Value = " + entry.getValue());
                String getTag;
                if(entry.getKey().endsWith("_sc")) {
                    getTag = entry.getKey().replace("_sc", "");
                    acc.addAll(queryAccountFromDB(getTag, entry.getValue(), "SC", isLock));
                } else if (entry.getKey().endsWith("_dc")) {
                    getTag = entry.getKey().replace("_dc", "");
                    acc.addAll(queryAccountFromDB(getTag, entry.getValue(), "DC", isLock));
                } else if (entry.getKey().endsWith("_conference")) {
                    getTag = entry.getKey().replace("_conference","");
                    acc.addAll(queryAccountFromDB(getTag, entry.getValue(),"CF", isLock));
                }
                else {
                    getTag = entry.getKey();
                    acc.addAll(queryAccountFromDB(getTag, 1, "RM",isLock));
                }

            }
        } else {
            if(tag.endsWith("_sc")) {
                tag = tag.replace("_sc", "");
            }
            if(tag.endsWith("_dc")) {
                tag = tag.replace("_dc", "");
            }
            if(tag.endsWith("_conference")) {
                tag = tag.replace("_conference","");
                acc = queryAccountFromDB(tag, 1, "CF", isLock);
            } else {
                acc = queryAccountFromDB(tag, 1, "RM", isLock);
            }
        }
        return acc;
    }

之前的版本,一个方法里非常非常非常的冗长,如果有什么新的需求的话,又要再继续if...else...,这是很可怕的事情
把需求重新捋一遍,重新把它们拆开,虽然看着改版后的代码好像更长了,但是如果有新需求的话,只要在map里加一条记录即可
after

# 不同的类型的账号需求
public static Map tagTailMapping =new HashMap(){{
        put("_dc", "DC");
        put("_sc", "SC");
        put("_conference", "CF");
    }};

# 主体方法
public synchronized List queryAccountViaTag(String tag, Boolean isLock) {
        List acc = new ArrayList<>();
        Map map = new HashMap<>();
# 根据提供的tag串,是否有包含&。再根据优先返回原则,最容易判断的条件优先返回,可以提高代码运行效率
        if (!tag.contains("&")) {
            acc = queryTailTagAccounts(tag, 1, isLock);
            return acc;
        }
        if (tag.contains("&")) {
            String[] separateTag = tag.split("&");
            List tagList = Arrays.asList(separateTag);
            map = convertListToUniqueMap(tagList);
        }

        for (Map.Entry entry : map.entrySet()) {
            logger.info("Key = " + entry.getKey() + ", Value = " + entry.getValue());
            acc.addAll(queryTailTagAccounts(entry.getKey(), entry.getValue(), isLock));
        }
        return acc;
    }

# 需求是根据不同账号提供的tag最后一个_xx来决定需要运行什么方法
private List queryTailTagAccounts(String tag, int num, Boolean isLock) {
        List acc = new ArrayList<>();
        String getTag;
        String tailTag = tag.contains("_") ? tag.substring(tag.lastIndexOf("_")) : ""; #获取最后一个 _xx,再把对应的type传进去
        if (tagTailMapping.containsKey(tailTag)) {
            getTag = tag.replace(tailTag, "");
            acc.addAll(queryAccountFromDB(getTag, num, tagTailMapping.get(tailTag), isLock));
        }
        else {
            for (int i = 0; i < num; i++) {
                acc.addAll(queryAccountFromDB(tag, 1, "RM", isLock));
            }
        }
        return acc;
    }
# 把tag串解析出来的list进行处理,减少运行时间
private Map convertListToUniqueMap(List tagList) {
        Set uniqueSet = new HashSet<>(tagList);
        Map map = new HashMap();
        for (String temp : uniqueSet) {
            Integer count = Collections.frequency(tagList, temp);
            String tailTag = temp.contains("_") ? temp.substring(temp.lastIndexOf("_")) : "";
            if (count == 1 && tagTailMapping.containsKey(tailTag)){
                temp = temp.replace(tailTag, "");
                putMapping(map, temp, count);
            } else {
                putMapping(map, temp, count);
            }
        }
        return map;
    }

private void putMapping(Map map, String temp, Integer count) {
        if (map.containsKey(temp)) {
            map.put(temp, map.get(temp) + count);
        } else {
            map.put(temp, count);
        }
    }

你可能感兴趣的:(一段代码的clean code)