“This is the fifth day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”
- Prepare for spring recruitment or summer internship in 2022, wish you every day a hundred million points of progress! Day7
- This article summarizes good naming practices, which will be updated daily
- For Redis Getting started to master, Concurrent Programming, please refer to my previous blog
- Believe in yourself, the more live more strong, alive should be open, meet water bridge! Life, you give me pressure, I return you miracle!
1, the introduction of
Famous Turing award winner: Nicholas Voss, who came up with the famous equation: program = algorithm + data structure. So we programmers should put a lot of effort into learning algorithms and data structures, but that’s not the best thing to do when writing code; Because 99.99% of the time, we don’t need to design algorithms and implement data structures (even for average algorithm engineers).
So in real development, what’s the best thing we can do to write code?
Xiao Ba thinks that good naming conventions and detailed code annotations are two seemingly simple problems, but it is really too difficult to do well, so that many codes written by older developers will have non-standard naming, incomplete code annotations or even no annotations. So this article starts with a look at how Java programmers should name their programs.
2, the body
2.1 the target
To do a good job of naming Java programs, the goal to achieve is summed up in only four words – literal meaning. Naming to achieve this goal allows the program to be “self-explanatory”, to understand the meaning of a package, class, method, or variable from the name alone, and to reduce the work of subsequent comments. Such code is also easy to read and maintain when circulated to other programmers.
2.2 package
There are two general directions for naming package names:
- Standard library
- The user class library
Standard library
In the Java system, the standard class library naming involves Java, Javax, etc., these names are determined by the specification of the person, so people can play how they want to play, the core class library rt.jar package can be checked.
The user class library
It is recommended that the user library code begin with the company’s Internet domain name and precede the top-level domain name, such as com.google, edu.xxx, org.xxx, and so on. This isn’t mandatory, but if you’re writing code that’s going to be used outside of your company (like open source), it’s a great opportunity to advertise and showcase your company.
For everything other than the prefix of the package, be content to use single words whenever possible, such as time, security, math; Abbreviations can also be used, such as AWT, IO, SQL.
You are not advised to use a package name that contains more than eight letters or plural numbers.
2.3 class
There are many types of classes, such as interfaces, abstract classes, enumerated classes, ordinary classes, and so on. These classes have similarities and differences in naming.
Similarities:
- Class names use the big hump, e.g. HttpClient, HttpResponse, HttpRequest
- Class names can be plural names
- The use of pinyin is prohibited. Even international pinyin names such as Alibaba and Youku are not recommended to appear in class names
Difference:
- The interface name must be concise, such that the implementation class can contain the interface name, such as Map, Set, List
- Enumeration classes must end with Enum
- Abstract classes must start with Abstract, such as AbstractMap and AbstractSet. You can also use Base, such as BaseCalendar and BaseRowSet. Abstract is recommended.
2.4 variable
The name of the variable follows the small hump name, which reflects the specific business and describes the purpose of the variable. Never learn from the source code for single-letter naming, such as this:
In addition, it should be noted that variable names should not start with is as far as possible, such as isRemoved and isDeleted, because such names are often mistakenly considered removed and deleted by the framework direction resolution in serialization scenarios, which is believed to have been stepped on by everyone. The best thing to do is not use it at all.
2.5 constant
Constants are values that remain constant in scope. Constants are usually modified with the final keyword. There are many types of constants: global constants, in-package constants, in-class constants, local constants, and so on.
Global constants :(package constants, class constants are the same)
The access modifier for global constants is public, usually with the public static final modifier. These constants are powerful and can be accessed anywhere, so we must name them well.
Global constants must be named in all uppercase letters, and if multiple words are separated by underscores, as in Java’s BigDecimal.
Constants for single words:
public static final BigDecimal ZERO = zeroThroughTen[0];
Copy the code
Constants for multiple words:
public final static int ROUND_HALF_UP = 4;
Copy the code
Local constants:
Local constants are divided into method constants and parameter constants, which do not need to use uppercase letters.
Parameter constants:
Public void hello(final String name) {// Compile error // name = "Leo eight "; System.out.println(name); }Copy the code
Method constants:
Public void hello() {final String name = "Eight "; // compile error // name = "Liziba"; System.out.println(name); }Copy the code
3, egg
Finally, we recommend an online naming artifact. Go ahead and try it, type in a word and it will search for a lot of words made up of that word. Unbug. Making. IO # / codelf/cli…