▄︻┻┳═One Agenda:
▄︻┻┳═1. (1/7) [code tidiness] do you really use enumerations? No!
▄︻┻┳═The method parameters of the (2/7) enumeration error usage
▄︻┻┳═One (3/7) enumeration error method parameter (two)
▄︻┻┳═Method (4/7) enumerated error usage method return value
▄︻┻┳═One (5/7) enumerate the wrong usage of the method inside the body.
▄︻┻┳═The branch judgment of the wrong usage of (6/7) enumeration
▄︻┻┳═(7/7) enumerate the definition of data type by enumeration.
Continue to talk about the use of enumeration. This article gives examples to illustrate the shortcomings of the parameters of the method without enumeration.
【First code.
First look at the following methods:
private boolean updRpmRtulProcc(RpmRtulDO rpmRtulDO, String rfdSts) throws YGException { logger.infoFmt("Modify the status of the refund order for failure., RefundStsEnum.getBySts(rfdSts).getDesc()); rpmRtulDO.setRfdSts(rfdSts); ... ... return true; }
The call to this method in the program:
if (!updRpmRtulProcc(rpmRtul, RefundStsEnum.RFD_CLEARING_PROC.getSts())) { ... ... }
The following is the RefundStsEnum enumeration class:
/** * Refund status enumeration*/ public enum RefundStsEnum { RFD_PROCESS("0","In refund processing "), RFD_SUCC("3","Successful refund "), RFD_FAIL("4","Refund failed "), RFD_CANCEL("7","Cancellation of refund "), RFD_CLEARING_PROC("8","In the settlement of refunds, "), RFD_ACCOUNTING_PROC("9","Refund account processing "), REFUSE("R9", "Approval denied "), RFD_APPROVAL("W3", "Refund approval "); private String sts; private String desc; RefundStsEnum(String sts, String desc) { this.sts = sts; this.desc = desc; } public String getSts() { return sts; } public String getDesc() { return desc; } public static RefundStsEnum getBySts(String sts) { for (RefundStsEnum type : values()) { if (type.getSts().equals(sts)) { return type; } } return null; } }
【Refactoring mode]
The String variable rfdSts in the above method defines the corresponding enumeration type RefundStsEnum in the program.
This example is improper use of typical enumerations. The crux of the problem is that the method parameter rfdSts is defined as String, which leads to two problems:
- Inside the method, such as the first line, when logging, call getBySts () that enumerates RefundStsEnum, convert the sts of RefundStsEnum stored in rfdSts to the ones that enumerate RefundStsEnum, and then get its dESC.
- The parameter passed by calling this method is RefundStsEnum.getSts ().
The correct way to do this is to define the method parameter rfdSts as an enumeration type RefundStsEnum, and naturally there is no such discomfort. This will be very clear.
private boolean updRpmRtulProcc(RpmRtulDO rpmRtulDO, RefundStsEnum rfdSts) throws YGException { logger.infoFmt("Modify the status of the refund order for failure., rfdSts.getDesc()); rpmRtulDO.setRfdSts(rfdSts.getSts()); ... ... return true; }