如何在Java中修改带有太多 “if else” 的情决语句?

huangapple go评论49阅读模式
英文:

how to modifiy the case in java with too many "if else"

问题

这是我的项目的编码部分。尽管我已经完成了任务,但它太长并且看起来很混乱。有没有办法修改结构并使其更简洁?非常感谢。

代码摘要:它是关于检查数据库中记录是否存在的。如果您需要更多详细信息,请告诉我。

CheckCertRecordDao checkCertHistRecordDao = new CheckCertRecordDaoImpl(connection);
int countCertRecord = 0;
countCertRecord = checkCertHistRecordDao.countCertHistRecordBySerialNo(certIdList);
if (countCertRecord == 0) {
    InsertCertRecordDao insertCertHistRecordDao = new InsertCertRecordDaoImpl(connection);
    insertedCertHist = insertCertHistRecordDao.insertCertHistRecord(certIdList);
    if (insertedCertHist = true) {
        countCertRecord = checkCertHistRecordDao.countCertHistRecordBySerialNo(certIdList);
        if (countCertRecord == certIdList.size()) {
            Als.audit(AlsAction.EXCEPTION, "Number of inserted cert hist record is matched with the certIdList!");
            String ModeRecord = null;
            CheckModeRecordDao checkModeHistRecordDao = new CheckModeRecordDaoImpl(connection);
            ModeRecord = checkModeHistRecordDao.checkModeHistRecordByHcp(modeId);
            if (ValidValue.isEmpty(ModeRecord)) {
                InsertModeRecordDao insertModeHistRecordDao = new InsertModeRecordDaoImpl(connection);
                insertedModeHist = insertModeHistRecordDao.insertModeHistRecord(modeId);
                if (insertedModeHist = true) {
                    ModeRecord = checkModeHistRecordDao.checkModeHistRecordByHcp(modeId);
                    if (!ValidValue.isEmpty(ModeRecord)) {
                        Als.audit(AlsAction.EXCEPTION, "Inserted mode hist record is completed!");
                        int countclientRecord = 0;
                        CheckConnClientRecordDao checkConnClientHistRecordDao = new CheckConnClientRecordDaoImpl(connection);
                        countclientRecord = checkConnClientHistRecordDao.checkConnClientHistRecord(clientIdList);
                        if (countclientRecord == 0) {
                            InsertConnClientRecordDao insertConnClientHistRecordDao = new InsertConnClientRecordDaoImpl(connection);
                            insertedConnClientHist = insertConnClientHistRecordDao.insertConnClientHistRecord(clientIdList);
                            Als.audit(AlsAction.INFO, "insertedConnClientHist = " + insertedConnClientHist);
                            if (insertedConnClientHist = true) {
                                countclientRecord = checkConnClientHistRecordDao.checkConnClientHistRecord(clientIdList);
                                Als.audit(AlsAction.INFO, "countclientRecord = " + countclientRecord);
                                Als.audit(AlsAction.INFO, "clientIdList = " + clientIdList.size());
                                if (countclientRecord == clientIdList.size()) {
                                    Als.audit(AlsAction.EXCEPTION, "Number of inserted client hist record is matched with the clientIdList");
                                    CheckConnInfoDao checkConnInfoHistRecordDao = new CheckConnInfoDaoImpl(connection);
                                    int connInfoCount = checkConnInfoHistRecordDao.countConnInfoHistRecord(InfoIdList);
                                    if (connInfoCount == 0) {
                                        InsertConnInfoRecordDao insertConnInfoHistRecordDao = new InsertConnInfoRecordDaoImpl(connection);
                                        insertedConnInfoHist = insertConnInfoHistRecordDao.insertConnInfoHistRecord(InfoIdList);
                                        if (insertedConnInfoHist = true) {
                                            Als.audit(AlsAction.EXCEPTION, "Inserting ConnInfo Hist Record is completed!");
                                            connInfoCount = checkConnInfoHistRecordDao.countConnInfoHistRecord(InfoIdList);
                                            if (connInfoCount == InfoIdList.size()) {
                                                Als.audit(AlsAction.EXCEPTION, "Number of inserted connInfo hist record is matched with the InfoIdList");
                                            } else {
                                                response.setStatusCode(SirResponse.ERROR_UNKNOWN_CD);
                                                response.setResultMsgByCode(SirResponse.ERROR_UNKNOWN_CD);
                                                Als.audit(AlsAction.INFO, "Number of inserted connInfo hist record is [NOT] matched with the InfoIdList");
                                            }
                                        } else {
                                            response.setStatusCode(SirResponse.ERROR_UNKNOWN_CD);
                                            response.setResultMsgByCode(SirResponse.ERROR_UNKNOWN_CD);
                                            Als.audit(AlsAction.EXCEPTION, "Inserting ConnInfo Hist Record with error!");
                                        }
                                    } else {
                                        response.setStatusCode(SirResponse.ERROR_UNKNOWN_CD);
                                        response.setResultMsgByCode(SirResponse.ERROR_UNKNOWN_CD);
                                        Als.audit(AlsAction.EXCEPTION, "connInfoCount =/= 0, existing the connInfo hist record already!");
                                    }
                                } else {
                                    response.setStatusCode(SirResponse.ERROR_UNKNOWN_CD);
                                    response.setResultMsgByCode(SirResponse.ERROR_UNKNOWN_CD);
                                    Als.audit(AlsAction.EXCEPTION, "Number of inserted client hist record is [NOT] matched with the clientIdList");
                                }
                            } else {
                                response.setStatusCode(SirResponse.ERROR_UNKNOWN_CD);
                                response.setResultMsgByCode(SirResponse.ERROR_UNKNOWN_CD);
                                Als.audit(AlsAction.EXCEPTION, "Inserting ConnClient Hist Record with error!");
                            }
                        } else {
                            response.setStatusCode(SirResponse.ERROR_UNKNOWN_CD);
                            response.setResultMsgByCode(SirResponse.ERROR_UNKNOWN_CD);
                            Als.audit(AlsAction.EXCEPTION, "countclientRecord =/= 0, existing the connClient hist record already!");
                        }
                    } else {
                        response.setStatusCode(SirResponse.ERROR_UNKNOWN_CD);
                        response.setResultMsgByCode(SirResponse.ERROR_UNKNOWN_CD);
                        Als.audit(AlsAction.EXCEPTION, "Inserted mode hist record with error!");
                    }
                } else {
                    response.setStatusCode(SirResponse.ERROR_UNKNOWN_CD);
                    response.setResultMsgByCode(SirResponse.ERROR_UNKNOWN_CD);
                    Als.audit(AlsAction.EXCEPTION, "Inserted mode hist record with error!!");
                }
            } else {
                response.setStatusCode(SirResponse.ERROR_UNKNOWN_CD);
                response.setResultMsgByCode(SirResponse.ERROR_UNKNOWN_CD);
                Als.audit(AlsAction.EXCEPTION, "ModeRecord is not empty, existing the mode hist record already!");
            }
        } else {
            response.setStatusCode(SirResponse.ERROR_UNKNOWN_CD);
            response.setResultMsgByCode(SirResponse.ERROR_UNKNOWN_CD);
            Als.audit(AlsAction.EXCEPTION, "Number of inserted cert hist record is [NOT] matched with the certIdList");
        }
    } else {
        response.setStatusCode(SirResponse.ERROR_UNKNOWN_CD);
        response.setResultMsgByCode(SirResponse.ERROR_UNKNOWN_CD);
        Als.audit(AlsAction.EXCEPTION, "Inserted cert hist record with error!!");
    }
} else {
    response.setStatusCode(SirResponse.ERROR_UNKNOWN_CD);
    response.setResultMsgByCode(SirResponse.ERROR_UNKNOWN_CD);
    Als.audit(AlsAction.EXCEPTION, "countCertRecord =/= 0, existing the cert hist record already!");
}
英文:

Here is my coding part of project. Although I have completed the task, it is too long and looks messy. Is there anyway that can modifiy the structure and make it shorter? Thanks alot.

Summarize of the code: It is about checking the record exists or not in the database. If you need some details, please let me know

        CheckCertRecordDao checkCertHistRecordDao = new CheckCertRecordDaoImpl(connection);
//CheckCertHistRecord
int countCertRecord = 0;
countCertRecord = checkCertHistRecordDao.countCertHistRecordBySerialNo(certIdList);
if (countCertRecord == 0) {
InsertCertRecordDao insertCertHistRecordDao = new InsertCertRecordDaoImpl(connection);
insertedCertHist = insertCertHistRecordDao.insertCertHistRecord(certIdList);
if (insertedCertHist = true) {
countCertRecord = checkCertHistRecordDao.countCertHistRecordBySerialNo(certIdList);
if (countCertRecord == certIdList.size()) {
Als.audit(AlsAction.EXCEPTION, "Number of inserted cert hist record is matched with the certIdList!");
String ModeRecord = null;
CheckModeRecordDao checkModeHistRecordDao = new CheckModeRecordDaoImpl(connection);
ModeRecord = checkModeHistRecordDao.checkModeHistRecordByHcp(modeId);
if (ValidValue.isEmpty(ModeRecord)) {
InsertModeRecordDao insertModeHistRecordDao = new InsertModeRecordDaoImpl(connection);
insertedModeHist = insertModeHistRecordDao.insertModeHistRecord(modeId);
if (insertedModeHist = true) {
ModeRecord = checkModeHistRecordDao.checkModeHistRecordByHcp(modeId); 
if (!ValidValue.isEmpty(ModeRecord)) {
Als.audit(AlsAction.EXCEPTION, "Inserted mode hist record is completed!");
int countclientRecord = 0;
CheckConnClientRecordDao checkConnClientHistRecordDao = new CheckConnClientRecordDaoImpl(connection);
countclientRecord = checkConnClientHistRecordDao.checkConnClientHistRecord(clientIdList);
if (countclientRecord == 0 ) {
InsertConnClientRecordDao insertConnClientHistRecordDao = new InsertConnClientRecordDaoImpl(connection);
insertedConnClientHist = insertConnClientHistRecordDao.insertConnClientHistRecord(clientIdList); 
Als.audit(AlsAction.INFO, "insertedConnClientHist = " + insertedConnClientHist);
if (insertedConnClientHist = true) {
countclientRecord = checkConnClientHistRecordDao.checkConnClientHistRecord(clientIdList);
Als.audit(AlsAction.INFO, "countclientRecord = " + countclientRecord);
Als.audit(AlsAction.INFO, "clientIdList = " + clientIdList.size());
if (countclientRecord == clientIdList.size()) {											
Als.audit(AlsAction.EXCEPTION, "Number of inserted client hist record is matched with the clientIdList");	
CheckConnInfoDao checkConnInfoHistRecordDao = new CheckConnInfoDaoImpl(connection);
int connInfoCount = checkConnInfoHistRecordDao.countConnInfoHistRecord(InfoIdList);
if (connInfoCount == 0 ) {
InsertConnInfoRecordDao insertConnInfoHistRecordDao = new InsertConnInfoRecordDaoImpl(connection);
insertedConnInfoHist = insertConnInfoHistRecordDao.insertConnInfoHistRecord(InfoIdList);
if (insertedConnInfoHist =  true) {
Als.audit(AlsAction.EXCEPTION, "Inserting ConnInfo Hist Record is completed!");	
connInfoCount = checkConnInfoHistRecordDao.countConnInfoHistRecord(InfoIdList);
if (connInfoCount == InfoIdList.size() ){
Als.audit(AlsAction.EXCEPTION, "Number of inserted connInfo hist record is matched with the InfoIdList");
} else {
response.setStatusCode(SirResponse.ERROR_UNKNOWN_CD);
response.setResultMsgByCode(SirResponse.ERROR_UNKNOWN_CD);
Als.audit(AlsAction.INFO, "Number of inserted connInfo hist record is [NOT] matched with the InfoIdList");
}	
} else {
response.setStatusCode(SirResponse.ERROR_UNKNOWN_CD);
response.setResultMsgByCode(SirResponse.ERROR_UNKNOWN_CD);
Als.audit(AlsAction.EXCEPTION, "Inserting ConnInfo Hist Record with error!");	
}														
} else {
response.setStatusCode(SirResponse.ERROR_UNKNOWN_CD);
response.setResultMsgByCode(SirResponse.ERROR_UNKNOWN_CD);
Als.audit(AlsAction.EXCEPTION, "connInfoCount =/= 0, existing the connInfo hist record already!");	
}
} else {
response.setStatusCode(SirResponse.ERROR_UNKNOWN_CD);
response.setResultMsgByCode(SirResponse.ERROR_UNKNOWN_CD);
Als.audit(AlsAction.EXCEPTION, "Number of inserted client hist record is [NOT] matched with the clientIdList");																				
}
} else {										
response.setStatusCode(SirResponse.ERROR_UNKNOWN_CD);
response.setResultMsgByCode(SirResponse.ERROR_UNKNOWN_CD);
Als.audit(AlsAction.EXCEPTION, "Inserting ConnClient Hist Record with error!");	
}	
} else {
response.setStatusCode(SirResponse.ERROR_UNKNOWN_CD);
response.setResultMsgByCode(SirResponse.ERROR_UNKNOWN_CD);
Als.audit(AlsAction.EXCEPTION, "countclientRecord =/= 0, existing the connClient hist record already!");	
}
} else {
response.setStatusCode(SirResponse.ERROR_UNKNOWN_CD);
response.setResultMsgByCode(SirResponse.ERROR_UNKNOWN_CD);
Als.audit(AlsAction.EXCEPTION, "Inserted mode hist record with error!");
}	
} else {
response.setStatusCode(SirResponse.ERROR_UNKNOWN_CD);
response.setResultMsgByCode(SirResponse.ERROR_UNKNOWN_CD);
Als.audit(AlsAction.EXCEPTION, "Inserted mode hist record with error!!");
}
} else {
response.setStatusCode(SirResponse.ERROR_UNKNOWN_CD);
response.setResultMsgByCode(SirResponse.ERROR_UNKNOWN_CD);
Als.audit(AlsAction.EXCEPTION, "ModeRecord is not empty, existing the mode hist record already!");
}	
} else {
response.setStatusCode(SirResponse.ERROR_UNKNOWN_CD);
response.setResultMsgByCode(SirResponse.ERROR_UNKNOWN_CD);
Als.audit(AlsAction.EXCEPTION, "Number of inserted cert hist record is [NOT] matched with the certIdList");
}
} else {
response.setStatusCode(SirResponse.ERROR_UNKNOWN_CD);
response.setResultMsgByCode(SirResponse.ERROR_UNKNOWN_CD);
Als.audit(AlsAction.EXCEPTION, "Inserted cert hist record with error!!");
}
} else {
response.setStatusCode(SirResponse.ERROR_UNKNOWN_CD);
response.setResultMsgByCode(SirResponse.ERROR_UNKNOWN_CD);
Als.audit(AlsAction.EXCEPTION, "countCertRecord =/= 0, existing the cert hist record already!");

答案1

得分: 3

  1. 做的事情可能还有更多,但你可以从这里开始:

停止代码重复:创建一个新函数,而不是重复这些行:

response.setStatusCode(SirResponse.ERROR_UNKNOWN_CD);
response.setResultMsgByCode(SirResponse.ERROR_UNKNOWN_CD);
Als.audit(AlsAction.EXCEPTION, "插入模式历史记录时出错!");
  1. 避免嵌套的if语句
if (!condition) {
return 
}
英文:

There is probably more todo, but you can begin with :

  1. Stop code repetition : create a new function instead of repeating these line :
response.setStatusCode(SirResponse.ERROR_UNKNOWN_CD);
response.setResultMsgByCode(SirResponse.ERROR_UNKNOWN_CD);
Als.audit(AlsAction.EXCEPTION, "Inserted mode hist record with error!!");
  1. Avoid nesting if statements
if (!condition) {
return 
}

huangapple
  • 本文由 发表于 2023年3月7日 16:37:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/75659619.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定