Code style tips

Source: Internet
Author: User

1. non-null judgment is required for null cases to prevent null exceptions. We recommend that you use apache commons-lang to effectively build NullpointExcetipion and NumberFormrtException.

2. Try not to have multiple entrances and exits in a method. Try to have one portal and one exit, that is, reduce return. 3. Remember to close all streams after the operation, especially during file operations. 4. Try to put objects that cannot be empty before using the equels method. 5. Try your best in judgment! Replace = false to make the code readable. 6. Try not to operate the parameter value in the method to avoid changing the value and affecting the business logic. 7. When using new ArrayList (), try to use the parent class List as much as possible. In this way, it is better to do it during expansion and complies with the dependency reversal principle. 8. Simple judgment: encapsulate public static final Map <Integer, String> printResultMap = new HashMap <Integer, String> (); static {printResultMap. put (1, ResultCodeDefine. DRAW_TICKET_SUCCESS); printResultMap. put (4, ResultCodeDefine. DRAW_TICKET_ERROR); printResultMap. put (0, ResultCodeDefine. DRAW_TICKET_SUCCESS);} 9. Soft encoding. Code like 1 2 3 cannot be defined. We recommend that you unify the configuration into common classes or make them configurable. 10, in the encapsulation of commonly used string date to use aptech help class commons-lang.jar11, clever use of enumeration, to ensure the rigor of the Code, improve readability. Public enum BoMsgStatus {// 0: initialization status // 1: warehouse receiving/2: ticket issuing in progress // 3: ticket issuing successful // 4: ticket issuing failed DEFAULT (short) 0), DB (short) 1), RUN (short) 2), SUCCESS (short) 3), FAILURE (short) 4); private final short value; public short getValue () {return value;} BoMsgStatus (short value) {this. value = value;} public static BoMsgStatus getEnum (short value) {for (BoMsgStatus bms: BoMsgStatus. values () {if (bms. getValue () = value) {return Bms ;}} return null ;}12. The log output format must be adequate and output in a certain format to facilitate later log analysis.

 

This article is from my site blog, please be sure to keep this source http://zhaoyongpan.blog.51cto.com/2714930/676934

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.