78 golden rules for Java programming

Source: Internet
Author: User

Creating and destroying objects

1. Consider replacing the constructor with a static factory method (static method that returns an instance of the class)
2. When encountering multiple constructor parameters, consider using the constructor
3. Hardening the Singleton property with a private constructor or enumeration type
4, the ability to strengthen the non-instantiation through the private constructor
5. Avoid creating unnecessary objects
6. Eliminate outdated object references
7. Avoid using the end-of-life (final) method

methods that are common to all objects

8. Please observe the General agreement when you overwrite equals
9. Overwrite equals when always overwrite Hashcode
10. Always overwrite ToString
11. Carefully overwrite clone
12. Consider implementing comparable interface

Classes and Interfaces

13. Minimize the accessibility of classes and members
14. Use Access methods in public classes instead of public domain
15, to minimize the variability of
16, compound better than inheritance
17. Either design for inheritance, provide documentation, or prohibit inheritance
18, interface is better than abstract class
19, the interface is only used to define the type
20, class level is better than label class
21. Use the function object to represent the policy
22. Prioritize static Member classes

generic type

23. Do not use the original ecological type in the new code
24. Eliminate non-inspected warnings
25, the list takes precedence over the array
26. Preference for generics
27. Preference for generic methods
28, the use of limited access to the Fraiti upgrade API flexibility
29. Preference for type-safe heterogeneous constructors

Enumerations and annotations

30. Use enum to bring up int constants
31. Replace ordinal with instance field
32, replace the bit field with Enumset
33. Replace ordinal index with Enummap
34. Simulating a scalable enumeration with an interface
35. Annotations take precedence over naming patterns
36. Insist on using override annotations
37. Defining the type with the tag interface

Method

38, check the validity of the parameters
39, if necessary, to carry out protective copies
40, careful design method signature
41, cautious use of heavy
42. Use variable parameters with caution
43. Returns an array or set of 0 lengths, not NULL
44. Write documentation comments for all exported API elements

General Programming

45. Minimizing the scope of local variables
46. The For-each cycle takes precedence over the traditional for loop, [why the For loop is hateful] http://justjavac.com/other/2012/05/15/whats-wrong-with-the-for-loop.html
47. Understanding and using class libraries
48. If precise answers are required, avoid using float and double
49, the basic type takes precedence over the boxing basic type
50. If other types are more appropriate, avoid using strings as much as possible
51, beware of String link performance
52. Referencing objects via interfaces
53, the interface takes precedence over the reflection mechanism
54. Use local methods with caution
55. Be careful to optimize
56. Adherence to universally accepted naming conventions

Exception

57. Use exceptions only for abnormal situations
58. Use the exception to the recoverable condition, use run-time exception for programming error
59. Avoid unnecessary use of the exception being inspected
60. Preferential use of standard exceptions
61. Throws the exception corresponding to the abstract
62, each method throws the exception to have the document
63. Contains the information of the capture failure in the detail message
64. Try to make the failure contain atomicity
65. Do not ignore exceptions

Concurrency

66. Simultaneous access to shared mutable data
67. Avoid over-synchronization
68. Executor and tasks take precedence over threads
69. Concurrency tools take precedence over wait and notify
70. Documentation of thread Safety
......

Detailed Description: http://java.662p.com/thread-3615-1-1.html

78 golden rules for Java programming

Related Article

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.