Although programming is a complex activity, it is essential to achieve excellent programmers and us standardized and standardized code writing habits. Let's take a look at some suggestions for writing JAVA code efficiently.
#1. The first letter of the class name should be capitalized. The first letter of fields, methods, and objects (handles) should be lowercase. As with all identifiers, all words contained within it should be close together, with the first letter of the intervening word capitalized. For example: ThisIsAClassName thisIsMethodOrFieldName If a constant initialization character appears in the definition, capitalize all letters in the static final basic type identifier. This will mark them as compile-time constants. Java packages are a special case: they are all lowercase, even the words in the middle. For domain name extension names, such as com, org, net or edu, etc., all should be lowercase (this is also one of the differences between Java 1.1 and Java 1.2).
2. When creating a class for general purposes, please adopt the "classic form" and include the definition of the following elements: equals() hashCode() toString() clone()(implement Cloneable) implement Serializable
3. For each class you create, consider placing a main(), which contains the code for testing that class. To use classes from a project, we don't have to delete the test code. If changes of any kind are made, it is easy to return to testing. The code also serves as an example of how to use classes.
4. Methods should be designed into brief, functional units, and used to describe and implement a discontinuous class interface part. Ideally, the approach should be concise and to the point. If the length is large, consider dividing it into shorter pieces in some way. This also facilitates reuse of code within the class (sometimes, methods must be very large, but they should still only do the same thing).
5. When designing a class, please put yourself in the shoes of the client programmer (the method of using the class should be very clear). Then, put yourself in the shoes of the person managing the code (anticipate what kinds of changes are likely to be made, and think about ways to make them easier).
6. Make the class as short and concise as possible, and only solve a specific problem. Here are some suggestions for class design:
A complex switch statement: Consider using the "polymorphic" mechanism.
A large number of methods involve operations with greatly different types: consider using several classes to implement them separately.
Many member variables have very different characteristics: consider using several classes.
7. Make everything as "private" as possible--private. You can make a certain part of the library "public" (a method, a class, a field, etc.) and you can never take it out. If you force it out, it may destroy other people's existing code, forcing them to rewrite and design it. If you only publish what you must publish, you can feel free to change anything else. In multi-threaded environments, privacy is a particularly important factor - only private fields are protected against unsynchronized use.
8. Be wary of "giant object syndrome". For some novices who are accustomed to sequential programming thinking and are new to the OOP field, they often like to write a sequential execution program first, and then embed it into one or two huge objects. According to programming principles, objects should express the concept of the application, not the application itself.
9. If you have to do some unsightly programming, you should at least put the code inside a class.
10. Whenever you find that classes are very closely integrated, you need to consider whether to use internal classes to improve coding and maintenance work (see "Using Internal Classes" in Chapter 14, Section 14.1.2 Improve the code").
The above is the detailed content of How to type code in java. For more information, please follow other related articles on the PHP Chinese website!