Eight common Java nonstandard codes

At work, I recently cleaned up an existing Java project code. After I finished, I found some repeated non-standard 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 in order. It comes from some code quality inspection 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 these functions by using the shortcut keys below.

CTRL + Shift + F C format source code.

CTRL + Shift + O C manages import statements and removes unused statements.

In addition to performing these two functions manually, you can also let eclipse automatically format the source code and manage the import statements when saving files. To do this, in eclipse, go to window - > Preferences - > java - > editor - > Save actions, enable perform the selected actions on save, and select format source code and organize imports.

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

In your method, 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).

Simplified if else method:

We wrote some tool methods with only one parameter, checked some conditions and returned a value according to the conditions. For example, the iseligible method seen above is www.twitter.com net。

Do not create new instances for Boolean, integer, or string:

Avoid creating new instances of Boolean, integer, string, etc. Use Boolean Valueof (true) replaces new Boolean (true). The two writing methods have the same effect, but they can improve performance.

Use curly braces around the code block:

Never forget to use braces around block type statements (such as if, for, while). This can reduce code ambiguity and avoid new bugs when you modify code blocks.

Not recommended

Declare the parameters of the method as final type:

Method parameters are always declared final in all compatible places. In this way, when you inadvertently modify the value of the parameter, you will be warned during compilation, and the bytecode generated by compilation will be optimized.

recommend

Name the public static final type member variable in uppercase:

Always name variables of type public static final in uppercase. This makes it easy to distinguish between constants and local variables.

Not recommended

Combine multiple if statements into one:

The following code

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

Always add a default statement to switch.

To avoid reusing the same string, create a constant:

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

The following code:

The above is the whole content of this article. I hope the content of this article can bring some help to your study or work. At the same time, I also hope to support a lot of programming tips!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>