Java coding traps

Source: Internet
Author: User

Abstract: Veera Sundar found some common illegal codes during code cleaning. Therefore, Veera Sundar summarized some common illegal codes into a list, this helps Java enthusiasts improve code quality and maintainability. Recently, I made a code cleanup for the Java project. After cleaning, I found that a group of common illegal code (that is, the nonstandard Code does not indicate a code error) repeatedly appears in the code. Therefore, I will summarize the common illegal codes into a list and share them with you to help Java enthusiasts improve code quality and maintainability. This list is not based on any rules or order, all of which are checked through code quality tools including CheckStyle, FindBugs, and PMD. Let's take a look: 1. The Eclipse compiler provides the source code format input Eclipse provides the option of automatic source code format, and organizes the input (delete unused code ). You can use the following shortcut keys for operations. Ctrl + Shift + F -- source code format Ctrl + Shift + O -- organize the input and delete unused code, instead of manually calling these two functions, you only need to set the automatic format and automatic organization options based on Eclipse, files can be saved at any time. In Eclipse, choose Window> Preferences> Java> Editor> Save Actions, Save them as selected, and check Format source code + Organize imports. 2. Avoid Multiple Return points. Follow your method to ensure that there is only one exit point. Do not use return in the same place or multiple places. For example, the following code does not recommended (not recommended) because there are multiple exit points (return statements ). Private boolean isEligible (int age) {if (age> 18) {return true ;}else {return false ;}} the code below has been improved, which is a later version. Private boolean isEligible (int age) {boolean result; if (age> 18) {result = true;} else {result = false;} return result ;} iii. Simplify if-else I have written several practical methods as a reference to check the statement conditions and return values based on the conditions. For example, considering the isEligible method, as you can see before: private boolean isEligible (int age) {boolean result; if (age> 18) {result = true ;} else {result = false;} return result;} the entire method is rewritten with a single return statement: private boolean isEligible (int age) {return age> 18 ;} 4. Do not create instances for Boolean, Integer, or String. Do not create instances for Boolean, Integer, or String. For example, use new Boolean (true), Boolean, valueOf (true ). The modified statement has the same effect as the previous statement, except for Improving the performance. 5. Never forget to use the braces module statements such as if, for, and while. The advantage of this is that when you modify a module-level statement, you can reduce the chance of fuzzy code and avoid introducing bugs. Not recommended: if (age> 18) result = true; else result = false; suggestion: if (age> 18) {result = true;} else {result = false ;} 6. Mark Method parameters with final type. Remember to mark method parameters with final type whenever applicable. The advantage of doing so is that when you accidentally modify the parameter value, the compiler will give you a warning and it can optimize the compiler code byte in a better way. Suggestion: private boolean isEligible (final int age ){...} 7. Name the public static final field in UPPERCASE and name the public static final field (usually called a constant) in UPPERCASE ). This allows you to easily distinguish between constant fields and local variables. Not recommended: public static final String testAccountNo = "12345678"; recommended: public static final String TEST_ACCOUNT_NO = "12345678 ";, 8. Combine multiple if statements into a single if statement as many as possible, for example, the following code: if (age> 18) {if (voted = false) {// eligible to vote .}} merge to a single if statement: if (age> 18 &&! Voted) {// eligible to vote} 9. The Switch should have a default value and always add default to the Switch statement. 10. Use constants to avoid repeated Definition of the same string value. If you must use strings in multiple places, use constants to avoid repeated definition of strings with the same value. For example, read the following code: private void someMethod () {logger. log ("My Application" + e );........ logger. log ("My Application" + f);} www.2cto. comstring literal "My applications" can be used as constants and in code. Public static final String MY_APP = "My Application"; private void someMethod () {logger. log (MY_APP + e );........ logger. log (MY_APP + f );}

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.