Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the common Java non-standard codes

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces you to the common Java non-standard code which are several, the content is very detailed, interested friends can refer to, hope to be helpful to you.

At work, I recently cleaned up an existing Java project code. When I was done, I found some recurring irregular code. So, I organized them into a list and shared them with my peers, hoping to attract attention and improve the quality and maintainability of the code.

This list is not order-sensitive and all comes from code quality checking tools such as CheckStyle, FindBugs, and PMD.

Format source code and manage import statements in Eclipse

Eclipse provides the ability to automatically format source code and manage import statements (and remove unused statements). You can use the following keyboard shortcuts to use these functions.

Ctrl + Shift + F-format the source code.

Ctrl + Shift + O-manages import statements and removes unused statements.

In addition to performing these two functions manually, you can also have Eclipse format the source code and manage import statements automatically when saving the file. To do this, in Eclipse, go to Window-> Preferences-> Java-> Editor-> Save Actions and enable Perform the selected actions on save, selecting Format source code and organize imports.

Avoid multiple return statements (exit points) in the method:

In your approach, make sure there is only one exit point. Do not use more than one return statement in a method.

For example, the following code is not recommended because it has multiple exit points (return statements).

Private boolean isEligible (int age) {if (age > 18) {return true;} else {return false;}}

The above code can be written like this (of course, the following code can be improved, later).

Private boolean isEligible (int age) {boolean result; if (age > 18) {result = true;} else {result = false;} return result;}

Simplified if-else method:

We write some utility methods that take only one parameter, check some conditions and return a value according to the condition. For example, the isEligible method seen above.

Private boolean isEligible (int age) {boolean result; if (age > 18) {result = true;} else {result = false;} return result;}

You can override this method with only one return statement.

Private boolean isEligible (int age) {return age > 18;}

Do not create a new instance for Boolean,Integer or String:

Avoid creating new instances such as Boolean,Integer,String. Use Boolean.valueOf (true) instead of new Boolean (true). The two writing methods have the same effect, but can improve the performance.

Use braces around the code block:

Never forget to use curly braces around block type statements such as if,for,while. This reduces code ambiguity and avoids generating new bug when you modify a code block.

Not recommended

If (age > 18) return true; else return false

Recommend

If (age > 18) {return true;} else {return false;}

Declare the parameters of the method as of type final:

Always declare the parameters of the method as final in all compatible places. In doing so, when you inadvertently change the value of the parameter, you will be warned at compile time, and the bytecode generated by the compilation will be optimized.

Recommend

Private boolean isEligible (final int age) {...}

Name public static final type member variables in uppercase:

Always use uppercase naming with public static final type variables. This makes it easy to distinguish between constants and local variables.

Not recommended

Public static final String testAccountNo = "12345678"

Recommend

Public static final String TEST_ACCOUNT_NO = "12345678"

Merge multiple if statements into one:

The following code

If (age > 18) {if (voted = = false) {/ / eligible to vote. }}

You can use an if statement to rewrite:

If (age > 18 & &! voted) {/ / eligible to vote}

Don't forget to add the default statement to switch:

Always add a default statement to switch.

To avoid repeating the same string, create a constant:

If you need to use the same string in multiple places, create a string constant to use it.

The following code:

Private void someMethod () {logger.log ("My Application" + e);. .... Logger.log ("My Application" + f);}

You can create a constant instead of the string "My Application":

Public static final String MY_APP = "My Application"; private void someMethod () {logger.log (MY_APP + e); .... Logger.log (MY_APP + f);}

Other resources: Java*** practices, code specification reviews, PMD rule lists

About the common Java non-standard code which is shared here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report