Java Programming Specification-oop specification

Source: Internet
Author: User
Tags shallow copy

1. "Force" avoids accessing static or static methods of this class through the object reference of a class, unnecessarily increasing the compiler parsing
It can be accessed directly using the class name.


2. "Mandatory" all the Overwrite method, must add @override annotation.
Inverse example:getObject () and Get0bject () issues. One is the letter O, one is the number 0, plus @override
You can accurately determine if the overwrite is successful. In addition, if the method signature is modified in an abstract class, its implementation class is immediately
Translation of the error.


3. "Force" the same parameter type, the same business meaning, you can use the variable parameters of Java, avoid using Object.
Description: The mutable parameter must be placed at the end of the parameter list. (Encourage students to try not to program with variable parameters)
Positive example: publicUser getusers (String type, Integer ... ids)


4. "Force" externally exposed interface signature, in principle, does not allow the modification of the method signature, to avoid affecting the interface callers. Pick up
Port obsolescence must be annotated with @deprecated and clearly indicate what the new interface or new service is used for.

5. "Enforce" cannot use obsolete classes or methods.
Description: Method Decode (STRINGENCODESTR) in Java.net.URLDecoder is obsolete and should be
The two-parameter decode (string source, string encode) is used. Since the interface provider is explicitly an outdated interface,
Then there is an obligation to provide a new interface at the same time; as a call to Towners, there is an obligation to verify what the new implementation of outdated methods is.


6. The Equals method of the "Force" object is apt to throw a null pointer exception and should be invoked using a constant or a value-determining object
equals
Positive example: "Test". Equals (object);
Inverse example: object.equals ("test");

Description: Recommended use of java.util.objects#equals (tool class introduced by JDK7)


7. "Force" all comparisons between the values of the same type of wrapper class object, all using the Equals method.
Description: For an integer var=? Assignment between 128 and 127, the integer object is
Integercache.cache, the existing object will be reused, and the integer value within this interval can be directly used = =
Judgment, but all data outside this interval will be generated on the heap and will not be reused for existing objects, which is a big pit,
It is recommended to use the Equals method for judgment.


8. "Mandatory" for basic data types and wrapper data types use the following criteria:
1) All Pojo class properties must use the wrapper data type. The return value and parameters of the
2) RPC method must use the wrapper data type.
3) all local variables "recommended" Use the base data type.
Description: The Pojo class attribute does not have an initial value, it is a reminder that the user must make the assignment explicitly when needed, any
NPE problems, or inbound inspection, are guaranteed by the user.
The query result for the database could be null because the automatic unpacking, with the base data type, receives The NPE risk.
For example, show the total volume of transactions, that is, positive and negative x%,x for the basic data type, call the RPC service, call


9. Do not set any property defaults when "Force" defines a Pojo class such as DO/DTO/VO.
Counter Example:the gmtcreate default value for the Pojo class is new Date (), but this property is not placed in the data extraction
The new field is updated with the other fields, causing the creation time to be modified to the current time.


10. When the "force" serialization class adds a new attribute, do not modify the Serialversionuid field to avoid deserialization failure;
To avoid deserialization confusion, modify the Serialversionuid value if it is completely incompatible with the upgrade.
Description: Note The Serialversionuid inconsistency throws a serialized run-time exception.


11. The "forced" construction method prohibits any business logic from being added, and if there is initialization logic, put it in the Init method.


12. The "force" Pojo class must write the ToString method. Using the tools in the IDE: Source>generate toString
, if you inherit another Pojo class, be careful to add super.tostring to the front.
Description: When a method execution throws an exception, it is possible to directly invoke the ToString () method of Pojo to print its property values for easy
Check the problem.


13. "Recommended" when using an index to access an array obtained using the split method of string, the last delimiter is required to have no
Content of the inspection, otherwise there will be a risk of throwing indexoutofboundsexception.
Description
String str = "a,b,c,,";
string[] ary = Str.split (",");
Expected to be greater than 3, the result is 3
System.out.println (ary.length);


14. "Recommended" When a class has more than one constructor method, or multiple methods with the same name, these methods should be placed together in order,
Easy to read.

15. The "recommended" in-class method definition order is: Public method or Protection method > Private Method > Getter/setter
Method.
Description: The public method is the caller of the class and the maintainer is most concerned about the method, the first screen display best; the protection method is only a subclass
Care, or it may be the core approach under template design patterns, while private methods do not generally require special care, and are a
Because of the low value of the method information, all service and DAO Getter/setter methods are placed in the class body most
After.


16. In the "recommended" setter method, the parameter name is the same as the class member variable name, this. member name = parameter name. In the
Getter/setter method, try not to increase the business logic, to increase the difficulty of troubleshooting problems.
Counter Example:
Public Integer GetData () {
if (true) {
return data +;
} else {
return data-100;
}
}


17. "Recommended" loop in the body, the way strings are joined, using the StringBuilder append method to expand.
Counter Example:
String str = "Start";
for (int i=0; i<100; i++) {
str = str + "Hello";
}
Description: The deserialized bytecode file shows that each loop will be new to a StringBuilder object and then
The Append operation finally returns a string object through the ToString method, resulting in a waste of memory resources.


18. "Recommended" final can improve the efficiency of the program response, declared final situation:
1) variables that do not need to be re-assigned, including class attributes, local variables.
2) The object parameter is final, which means that the point of the reference is not allowed to be modified.
3) The class method determines that it is not allowed to be overridden.


19. "Recommended" Use the Clone method of object to copy objects with caution.
Description: The object's Clone method is a shallow copy by default, and if you want to implement a deep copy, you need to override the Clone method to implement the Property object
The copy.

20. "Recommended" class members with strict method access control:
1) if external direct pass is not allowed New to create the object, the construction method must be private. The
2) tool class does not allow public or default construction methods. The
3) class is not a static member variable and is shared with the subclass, which must be protected. The
4) class is not a static member variable and is used only in this class and must be private. The
5) class static member variable must be private if it is used only in this class.
6) if the static member variable, you must consider whether it is final. The
7) class member method is only intended for internal invocation of the class and must be private. The
8) class member method is only exposed to the inheriting class, and is limited to protected.
description: Any class, method, parameter, variable, strict access to the scope. A wide range of access is not conducive to module decoupling. Think
test: If it is a private method, you want to delete it, but a public service method, or a

The scope of the variable is too large, if unrestricted to run around, then you will worry.

Java Programming Specification-oop specification

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.