Analysis and Summary of common illegal codes in Java

Source: Internet
Author: User

1. The eclipse compiler provides source code format input.

Eclipse provides automatic source code format options 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 -- enter and delete unused code in the organization

Instead of manually calling these two functions, you can save files at any time based on the eclipse automatic format and automatic organization options.

In eclipse, choose Window> preferences> JAVA> editor> SAVE actions, save them as selected, and check format source code + Organize imports.

2. Avoid multiple responses (exit point)

Follow your steps 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 ).

1. Private Boolean iseligible (INT age ){

2. If (age> 18 ){

3. Return true;

4.} else {

5. Return false;

6 .}

7 .}

The following code is upgraded, which is a later version.

1. Private Boolean iseligible (INT age ){

2. boolean result;

3. If (age> 18 ){

4. Result = true;

5.} else {

6. Result = false;

7 .}

8. Return result;

9 .}

Iii. simplified if-Else

I wrote a few 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:

1. Private Boolean iseligible (INT age ){

2. boolean result;

3. If (age> 18 ){

4. Result = true;

5.} else {

6. Result = false;

7 .}

8. Return result;

9 .}

The entire method is rewritten with a single return statement:

1. Private Boolean iseligible (INT age ){

2.3.return age> 18;

4.5 .}

4. Do not create a new instance for Boolean, integer, or string.

Avoid creating new instances for Boolean, integer, and 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. Use the braces module statement

Never forget to use the braces module statements such as if, for, and while. The advantage of doing so is that when you modify the module-level statements, you can reduce the chance of fuzzy code and avoid introducing bugs.

Not recommended:

1. If (age> 18)

2. Result = true;

3. Else

4. Result = false;

Suggestion:

1. If (age> 18 ){

2. Result = true;

3.} else {

4. Result = false;

5 .}

6. method parameters marked as final type, applicable at any time

Remember to mark method parameters as final type, which is applicable at any time. 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

Name the public static final field in uppercase (usually called a constant ). This allows you to easily distinguish between constant fields and local variables.

Not recommended:

Public static final string testaccountno = "12345678 ";

Suggestion:

Public static final string test_account_no = "12345678 ";,

8. combine them into a single if statement

In as many cases as possible, multiple if statements are combined into a single if statement, such as the following code:

1. If (age> 18 ){

2. If (voted = false ){

3. // eligible to vote.

4 .}

5 .}

Merge to a single if statement:

1. If (age> 18 &&! Voted ){

2. // eligible to vote

3 .}

9. Switch should have default

Always add default to the switch statement.

10. Use constants to avoid repeatedly defining the same string value

If you must use strings in multiple places, use constants to avoid repeatedly defining strings with the same value.

For example, read the following code:

1. Private void somemethod (){

2. Logger. Log ("My application" + E );

3 ....

4 ....

5. Logger. Log ("My application" + F );

6 .}

String Literal "My applications" can be used as constants and in code.

1. Public static final string my_app = "my application ";

2.

3. Private void somemethod (){

4. Logger. Log (my_app + E );

5 ....

6 ....

7. Logger. Log (my_app + F );

8 .}

Original text from [than the Internet], reproduced Please retain the original link: http://soft.chinabyte.com/database/411/12432911.shtml

 

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.