简体   繁体   中英

How to shorten the condition code ? (JAV, Check null)

I use it to check for null.DataSet has a structure of both String Integer and Bigdecimal data types. How to shorten the condition code? Is there any way? To shorten my code. Thank you.

public void ConfrimData(DataSet data) {
            if (StringUtils.isEmpty(data.getA())
                || StringUtils.isEmpty(data.getB())
                || StringUtils.isEmpty(data.getC())
                || StringUtils.isEmpty(data.getD())
                || StringUtils.isEmpty(data.getE())
                || StringUtils.isEmpty(data.getF())
                ){
        if (StringUtils.isEmpty(data.getA())) {
            loggerTransaction.info(Var.VALUE_A);
        }
        if (StringUtils.isEmpty(data.getB())) {
            loggerTransaction.info(Var.VALUE_B);
        }
        if (StringUtils.isEmpty(data.getC())) {
            loggerTransaction.info(Var.VALUE_C);
        }
        if (StringUtils.isEmpty(data.getD())) {
            loggerTransaction.info(Var.VALUE_D);
        }
        if (StringUtils.isEmpty(data.getE())) {
            loggerTransaction.info(Var.VALUE_E);
        }
        if (StringUtils.isEmpty(data.getF())) {
            loggerTransaction.info(Var.VALUE_F);
        }
        return; 
        }

_ DataSet

private String A = null;

private Integer B = null;

private String C= null;

private String D = null;

private BigDecimal E= null;

private String F= null;

In your first if statement you check all the parameters to see if any of them are empty. Then, in the inner if statements, you check them again. The first check is redundant. The return statement is also not necessary since it does not end the method early or returns any data.

Here is a shorter version that should give the same result:

public void confirmData(DataSet data) {
    if (StringUtils.isEmpty(data.getA())) {
        loggerTransaction.info(Var.VALUE_A);
    }
    if (StringUtils.isEmpty(data.getB())) {
        loggerTransaction.info(Var.VALUE_B);
    }
    if (StringUtils.isEmpty(data.getC())) {
        loggerTransaction.info(Var.VALUE_C);
    }
    if (StringUtils.isEmpty(data.getD())) {
        loggerTransaction.info(Var.VALUE_D);
    }
    if (StringUtils.isEmpty(data.getE())) {
        loggerTransaction.info(Var.VALUE_E);
    }
    if (StringUtils.isEmpty(data.getF())) {
        loggerTransaction.info(Var.VALUE_F);
    }
}

EDIT

Here is a slightly prettier solution with less code repetition:

public void confirmData(DataSet data) {
    logIfEmpty(data.getA(), Var.VALUE_A);
    logIfEmpty(data.getB(), Var.VALUE_B);
    logIfEmpty(data.getC(), Var.VALUE_C);
    logIfEmpty(data.getD(), Var.VALUE_D);
    logIfEmpty(data.getE(), Var.VALUE_E);
    logIfEmpty(data.getF(), Var.VALUE_F);
}
private void logIfEmpty(Object check, String log) {
    if (StringUtils.isEmpty(check)) {
        loggerTransaction.info(log);
    }
}

EDIT #2

And if you have other code you want to execute if you did not find any empty values, you can do this:

public void confirmData(DataSet data) {
    boolean foundEmpty;
    foundEmpty = logIfEmpty(data.getA(), Var.VALUE_A);
    foundEmpty = logIfEmpty(data.getB(), Var.VALUE_B) || foundEmpty;
    foundEmpty = logIfEmpty(data.getC(), Var.VALUE_C) || foundEmpty;
    foundEmpty = logIfEmpty(data.getD(), Var.VALUE_D) || foundEmpty;
    foundEmpty = logIfEmpty(data.getE(), Var.VALUE_E) || foundEmpty;
    foundEmpty = logIfEmpty(data.getF(), Var.VALUE_F) || foundEmpty;
    if(foundEmpty) {
        return;
    }
}
private boolean logIfEmpty(String check, String log) {
    if (StringUtils.isEmpty(check)) {
        loggerTransaction.info(log);
        return true;
    }
    return false;
}

Well, you could make it slightly less repeaty using streams, but it won't make it necessarily better, let alone faster:

LinkedHashMap<Supplier, String> map = new LinkedHashMap<>();
map.put(data::getA, Var.VALUE_A);
map.put(data::getB, Var.VALUE_B);
map.put(data::getC, Var.VALUE_C);
map.put(data::getD, Var.VALUE_D);
map.put(data::getE, Var.VALUE_E);
map.put(data::getF, Var.VALUE_F);

List<String> logMessages = map.entrySet().stream()
    .filter(entry -> StringUtils.isEmpty(entry.getKey().get()))
    .map(Map.Entry::getValue)
    .collect(Collectors.toList());
if (!logMessages.isEmpty()) {
    logMessages.forEach(loggerTransaction::info);
}
else {
    // Remaining code
}

Maybe assigning a flag...

public void ConfrimData(DataSet data) {
        boolean flag;
        if (flag = StringUtils.isEmpty(data.getA())) {
            loggerTransaction.info(Var.VALUE_A);
        }
        if (flag = StringUtils.isEmpty(data.getB()) || flag) {
            loggerTransaction.info(Var.VALUE_B);
        }
        if (flag = StringUtils.isEmpty(data.getC()) || flag) {
            loggerTransaction.info(Var.VALUE_C);
        }
        if (flag = StringUtils.isEmpty(data.getD()) || flag) {
            loggerTransaction.info(Var.VALUE_D);
        }
        if (flag = StringUtils.isEmpty(data.getE()) || flag) {
            loggerTransaction.info(Var.VALUE_E);
        }
        if (flag = StringUtils.isEmpty(data.getF()) || flag) {
            loggerTransaction.info(Var.VALUE_F);
        }
        if (flag) return; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM