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

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

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

问题

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

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

  1. CheckCertRecordDao checkCertHistRecordDao = new CheckCertRecordDaoImpl(connection);
  2. int countCertRecord = 0;
  3. countCertRecord = checkCertHistRecordDao.countCertHistRecordBySerialNo(certIdList);
  4. if (countCertRecord == 0) {
  5. InsertCertRecordDao insertCertHistRecordDao = new InsertCertRecordDaoImpl(connection);
  6. insertedCertHist = insertCertHistRecordDao.insertCertHistRecord(certIdList);
  7. if (insertedCertHist = true) {
  8. countCertRecord = checkCertHistRecordDao.countCertHistRecordBySerialNo(certIdList);
  9. if (countCertRecord == certIdList.size()) {
  10. Als.audit(AlsAction.EXCEPTION, "Number of inserted cert hist record is matched with the certIdList!");
  11. String ModeRecord = null;
  12. CheckModeRecordDao checkModeHistRecordDao = new CheckModeRecordDaoImpl(connection);
  13. ModeRecord = checkModeHistRecordDao.checkModeHistRecordByHcp(modeId);
  14. if (ValidValue.isEmpty(ModeRecord)) {
  15. InsertModeRecordDao insertModeHistRecordDao = new InsertModeRecordDaoImpl(connection);
  16. insertedModeHist = insertModeHistRecordDao.insertModeHistRecord(modeId);
  17. if (insertedModeHist = true) {
  18. ModeRecord = checkModeHistRecordDao.checkModeHistRecordByHcp(modeId);
  19. if (!ValidValue.isEmpty(ModeRecord)) {
  20. Als.audit(AlsAction.EXCEPTION, "Inserted mode hist record is completed!");
  21. int countclientRecord = 0;
  22. CheckConnClientRecordDao checkConnClientHistRecordDao = new CheckConnClientRecordDaoImpl(connection);
  23. countclientRecord = checkConnClientHistRecordDao.checkConnClientHistRecord(clientIdList);
  24. if (countclientRecord == 0) {
  25. InsertConnClientRecordDao insertConnClientHistRecordDao = new InsertConnClientRecordDaoImpl(connection);
  26. insertedConnClientHist = insertConnClientHistRecordDao.insertConnClientHistRecord(clientIdList);
  27. Als.audit(AlsAction.INFO, "insertedConnClientHist = " + insertedConnClientHist);
  28. if (insertedConnClientHist = true) {
  29. countclientRecord = checkConnClientHistRecordDao.checkConnClientHistRecord(clientIdList);
  30. Als.audit(AlsAction.INFO, "countclientRecord = " + countclientRecord);
  31. Als.audit(AlsAction.INFO, "clientIdList = " + clientIdList.size());
  32. if (countclientRecord == clientIdList.size()) {
  33. Als.audit(AlsAction.EXCEPTION, "Number of inserted client hist record is matched with the clientIdList");
  34. CheckConnInfoDao checkConnInfoHistRecordDao = new CheckConnInfoDaoImpl(connection);
  35. int connInfoCount = checkConnInfoHistRecordDao.countConnInfoHistRecord(InfoIdList);
  36. if (connInfoCount == 0) {
  37. InsertConnInfoRecordDao insertConnInfoHistRecordDao = new InsertConnInfoRecordDaoImpl(connection);
  38. insertedConnInfoHist = insertConnInfoHistRecordDao.insertConnInfoHistRecord(InfoIdList);
  39. if (insertedConnInfoHist = true) {
  40. Als.audit(AlsAction.EXCEPTION, "Inserting ConnInfo Hist Record is completed!");
  41. connInfoCount = checkConnInfoHistRecordDao.countConnInfoHistRecord(InfoIdList);
  42. if (connInfoCount == InfoIdList.size()) {
  43. Als.audit(AlsAction.EXCEPTION, "Number of inserted connInfo hist record is matched with the InfoIdList");
  44. } else {
  45. response.setStatusCode(SirResponse.ERROR_UNKNOWN_CD);
  46. response.setResultMsgByCode(SirResponse.ERROR_UNKNOWN_CD);
  47. Als.audit(AlsAction.INFO, "Number of inserted connInfo hist record is [NOT] matched with the InfoIdList");
  48. }
  49. } else {
  50. response.setStatusCode(SirResponse.ERROR_UNKNOWN_CD);
  51. response.setResultMsgByCode(SirResponse.ERROR_UNKNOWN_CD);
  52. Als.audit(AlsAction.EXCEPTION, "Inserting ConnInfo Hist Record with error!");
  53. }
  54. } else {
  55. response.setStatusCode(SirResponse.ERROR_UNKNOWN_CD);
  56. response.setResultMsgByCode(SirResponse.ERROR_UNKNOWN_CD);
  57. Als.audit(AlsAction.EXCEPTION, "connInfoCount =/= 0, existing the connInfo hist record already!");
  58. }
  59. } else {
  60. response.setStatusCode(SirResponse.ERROR_UNKNOWN_CD);
  61. response.setResultMsgByCode(SirResponse.ERROR_UNKNOWN_CD);
  62. Als.audit(AlsAction.EXCEPTION, "Number of inserted client hist record is [NOT] matched with the clientIdList");
  63. }
  64. } else {
  65. response.setStatusCode(SirResponse.ERROR_UNKNOWN_CD);
  66. response.setResultMsgByCode(SirResponse.ERROR_UNKNOWN_CD);
  67. Als.audit(AlsAction.EXCEPTION, "Inserting ConnClient Hist Record with error!");
  68. }
  69. } else {
  70. response.setStatusCode(SirResponse.ERROR_UNKNOWN_CD);
  71. response.setResultMsgByCode(SirResponse.ERROR_UNKNOWN_CD);
  72. Als.audit(AlsAction.EXCEPTION, "countclientRecord =/= 0, existing the connClient hist record already!");
  73. }
  74. } else {
  75. response.setStatusCode(SirResponse.ERROR_UNKNOWN_CD);
  76. response.setResultMsgByCode(SirResponse.ERROR_UNKNOWN_CD);
  77. Als.audit(AlsAction.EXCEPTION, "Inserted mode hist record with error!");
  78. }
  79. } else {
  80. response.setStatusCode(SirResponse.ERROR_UNKNOWN_CD);
  81. response.setResultMsgByCode(SirResponse.ERROR_UNKNOWN_CD);
  82. Als.audit(AlsAction.EXCEPTION, "Inserted mode hist record with error!!");
  83. }
  84. } else {
  85. response.setStatusCode(SirResponse.ERROR_UNKNOWN_CD);
  86. response.setResultMsgByCode(SirResponse.ERROR_UNKNOWN_CD);
  87. Als.audit(AlsAction.EXCEPTION, "ModeRecord is not empty, existing the mode hist record already!");
  88. }
  89. } else {
  90. response.setStatusCode(SirResponse.ERROR_UNKNOWN_CD);
  91. response.setResultMsgByCode(SirResponse.ERROR_UNKNOWN_CD);
  92. Als.audit(AlsAction.EXCEPTION, "Number of inserted cert hist record is [NOT] matched with the certIdList");
  93. }
  94. } else {
  95. response.setStatusCode(SirResponse.ERROR_UNKNOWN_CD);
  96. response.setResultMsgByCode(SirResponse.ERROR_UNKNOWN_CD);
  97. Als.audit(AlsAction.EXCEPTION, "Inserted cert hist record with error!!");
  98. }
  99. } else {
  100. response.setStatusCode(SirResponse.ERROR_UNKNOWN_CD);
  101. response.setResultMsgByCode(SirResponse.ERROR_UNKNOWN_CD);
  102. Als.audit(AlsAction.EXCEPTION, "countCertRecord =/= 0, existing the cert hist record already!");
  103. }
英文:

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

  1. CheckCertRecordDao checkCertHistRecordDao = new CheckCertRecordDaoImpl(connection);
  2. //CheckCertHistRecord
  3. int countCertRecord = 0;
  4. countCertRecord = checkCertHistRecordDao.countCertHistRecordBySerialNo(certIdList);
  5. if (countCertRecord == 0) {
  6. InsertCertRecordDao insertCertHistRecordDao = new InsertCertRecordDaoImpl(connection);
  7. insertedCertHist = insertCertHistRecordDao.insertCertHistRecord(certIdList);
  8. if (insertedCertHist = true) {
  9. countCertRecord = checkCertHistRecordDao.countCertHistRecordBySerialNo(certIdList);
  10. if (countCertRecord == certIdList.size()) {
  11. Als.audit(AlsAction.EXCEPTION, "Number of inserted cert hist record is matched with the certIdList!");
  12. String ModeRecord = null;
  13. CheckModeRecordDao checkModeHistRecordDao = new CheckModeRecordDaoImpl(connection);
  14. ModeRecord = checkModeHistRecordDao.checkModeHistRecordByHcp(modeId);
  15. if (ValidValue.isEmpty(ModeRecord)) {
  16. InsertModeRecordDao insertModeHistRecordDao = new InsertModeRecordDaoImpl(connection);
  17. insertedModeHist = insertModeHistRecordDao.insertModeHistRecord(modeId);
  18. if (insertedModeHist = true) {
  19. ModeRecord = checkModeHistRecordDao.checkModeHistRecordByHcp(modeId);
  20. if (!ValidValue.isEmpty(ModeRecord)) {
  21. Als.audit(AlsAction.EXCEPTION, "Inserted mode hist record is completed!");
  22. int countclientRecord = 0;
  23. CheckConnClientRecordDao checkConnClientHistRecordDao = new CheckConnClientRecordDaoImpl(connection);
  24. countclientRecord = checkConnClientHistRecordDao.checkConnClientHistRecord(clientIdList);
  25. if (countclientRecord == 0 ) {
  26. InsertConnClientRecordDao insertConnClientHistRecordDao = new InsertConnClientRecordDaoImpl(connection);
  27. insertedConnClientHist = insertConnClientHistRecordDao.insertConnClientHistRecord(clientIdList);
  28. Als.audit(AlsAction.INFO, "insertedConnClientHist = " + insertedConnClientHist);
  29. if (insertedConnClientHist = true) {
  30. countclientRecord = checkConnClientHistRecordDao.checkConnClientHistRecord(clientIdList);
  31. Als.audit(AlsAction.INFO, "countclientRecord = " + countclientRecord);
  32. Als.audit(AlsAction.INFO, "clientIdList = " + clientIdList.size());
  33. if (countclientRecord == clientIdList.size()) {
  34. Als.audit(AlsAction.EXCEPTION, "Number of inserted client hist record is matched with the clientIdList");
  35. CheckConnInfoDao checkConnInfoHistRecordDao = new CheckConnInfoDaoImpl(connection);
  36. int connInfoCount = checkConnInfoHistRecordDao.countConnInfoHistRecord(InfoIdList);
  37. if (connInfoCount == 0 ) {
  38. InsertConnInfoRecordDao insertConnInfoHistRecordDao = new InsertConnInfoRecordDaoImpl(connection);
  39. insertedConnInfoHist = insertConnInfoHistRecordDao.insertConnInfoHistRecord(InfoIdList);
  40. if (insertedConnInfoHist = true) {
  41. Als.audit(AlsAction.EXCEPTION, "Inserting ConnInfo Hist Record is completed!");
  42. connInfoCount = checkConnInfoHistRecordDao.countConnInfoHistRecord(InfoIdList);
  43. if (connInfoCount == InfoIdList.size() ){
  44. Als.audit(AlsAction.EXCEPTION, "Number of inserted connInfo hist record is matched with the InfoIdList");
  45. } else {
  46. response.setStatusCode(SirResponse.ERROR_UNKNOWN_CD);
  47. response.setResultMsgByCode(SirResponse.ERROR_UNKNOWN_CD);
  48. Als.audit(AlsAction.INFO, "Number of inserted connInfo hist record is [NOT] matched with the InfoIdList");
  49. }
  50. } else {
  51. response.setStatusCode(SirResponse.ERROR_UNKNOWN_CD);
  52. response.setResultMsgByCode(SirResponse.ERROR_UNKNOWN_CD);
  53. Als.audit(AlsAction.EXCEPTION, "Inserting ConnInfo Hist Record with error!");
  54. }
  55. } else {
  56. response.setStatusCode(SirResponse.ERROR_UNKNOWN_CD);
  57. response.setResultMsgByCode(SirResponse.ERROR_UNKNOWN_CD);
  58. Als.audit(AlsAction.EXCEPTION, "connInfoCount =/= 0, existing the connInfo hist record already!");
  59. }
  60. } else {
  61. response.setStatusCode(SirResponse.ERROR_UNKNOWN_CD);
  62. response.setResultMsgByCode(SirResponse.ERROR_UNKNOWN_CD);
  63. Als.audit(AlsAction.EXCEPTION, "Number of inserted client hist record is [NOT] matched with the clientIdList");
  64. }
  65. } else {
  66. response.setStatusCode(SirResponse.ERROR_UNKNOWN_CD);
  67. response.setResultMsgByCode(SirResponse.ERROR_UNKNOWN_CD);
  68. Als.audit(AlsAction.EXCEPTION, "Inserting ConnClient Hist Record with error!");
  69. }
  70. } else {
  71. response.setStatusCode(SirResponse.ERROR_UNKNOWN_CD);
  72. response.setResultMsgByCode(SirResponse.ERROR_UNKNOWN_CD);
  73. Als.audit(AlsAction.EXCEPTION, "countclientRecord =/= 0, existing the connClient hist record already!");
  74. }
  75. } else {
  76. response.setStatusCode(SirResponse.ERROR_UNKNOWN_CD);
  77. response.setResultMsgByCode(SirResponse.ERROR_UNKNOWN_CD);
  78. Als.audit(AlsAction.EXCEPTION, "Inserted mode hist record with error!");
  79. }
  80. } else {
  81. response.setStatusCode(SirResponse.ERROR_UNKNOWN_CD);
  82. response.setResultMsgByCode(SirResponse.ERROR_UNKNOWN_CD);
  83. Als.audit(AlsAction.EXCEPTION, "Inserted mode hist record with error!!");
  84. }
  85. } else {
  86. response.setStatusCode(SirResponse.ERROR_UNKNOWN_CD);
  87. response.setResultMsgByCode(SirResponse.ERROR_UNKNOWN_CD);
  88. Als.audit(AlsAction.EXCEPTION, "ModeRecord is not empty, existing the mode hist record already!");
  89. }
  90. } else {
  91. response.setStatusCode(SirResponse.ERROR_UNKNOWN_CD);
  92. response.setResultMsgByCode(SirResponse.ERROR_UNKNOWN_CD);
  93. Als.audit(AlsAction.EXCEPTION, "Number of inserted cert hist record is [NOT] matched with the certIdList");
  94. }
  95. } else {
  96. response.setStatusCode(SirResponse.ERROR_UNKNOWN_CD);
  97. response.setResultMsgByCode(SirResponse.ERROR_UNKNOWN_CD);
  98. Als.audit(AlsAction.EXCEPTION, "Inserted cert hist record with error!!");
  99. }
  100. } else {
  101. response.setStatusCode(SirResponse.ERROR_UNKNOWN_CD);
  102. response.setResultMsgByCode(SirResponse.ERROR_UNKNOWN_CD);
  103. Als.audit(AlsAction.EXCEPTION, "countCertRecord =/= 0, existing the cert hist record already!");

答案1

得分: 3

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

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

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

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

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

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:

确定