78 golden rules for java programming and 78 golden rules for java

Source: Internet
Author: User

78 golden rules for java programming and 78 golden rules for java

 

Create and destroy objects

1. Consider replacing the constructor with the static factory method (the static method of the returned class instance ).
2. When encountering multiple constructor parameters, consider using Constructor
3. Use a private constructor or enumeration type to enhance the Singleton attribute.
4. Enhance the non-instantiation capability through the private constructor
5. Avoid creating unnecessary objects.
6. Remove expired object references
7. Avoid using the final Method

Common methods for all objects

8. comply with general conventions when overwriting equals
9. overwrite the hashCode when overwriting equals.
10. always overwrite toString
11. Carefully overwrite clone
12. Consider implementing the Comparable Interface

Class and Interface

13. Minimize the accessibility of classes and members
14. Use Access methods in public classes instead of public domains
15. Minimize Variability
16. combination is better than inheritance
17. Either Design for inheritance, provide document instructions, or prohibit inheritance
18. interfaces are better than abstract classes.
19. The interface is only used to define the type
20. class hierarchy is better than label class
21. Using function objects to represent policies
22. Give priority to static member classes

Generic

23. Do not use the original ecological type in the new Code
24. Eliminate non-inspected warnings
25. List takes precedence over Array
26. Give priority to generic
27. Give priority to generic methods
28. Use restricted wildcards to improve API flexibility
29. Type-safe isomorphic constructors are prioritized

Enumeration and Annotation

30. Start int constant with enum
31. Replace ordinal number with instance domain
32. Use EnumSet to replace the Bit Field
33. Replacing the ordinal index with EnumMap
34. Use interfaces to simulate scalable Enumeration
35. annotation takes precedence over naming
36. Stick to the Override Annotation
37. Define the type with the Mark Interface

Method

38. Check the parameter Validity
39. Make a protective copy if necessary
40. Design Method signature with caution
41. Use heavy load with caution
42. use variable parameters with caution
43. returns an array or set with zero length instead of null.
44. Compile document comments for all exported API Elements

General Program Design

45. Minimize the scope of local variables
46. for-each loop takes precedence over the traditional for loop. [for Loop Why can hate] http://justjavac.com/other/2012/05/15/whats-wrong-with-the-for-loop.html
47. understand and use the class library
48. If you need a precise answer, avoid using float and double
49. The basic type takes precedence over the basic packing type.
50. Avoid using strings if other types are more suitable.
51. Be careful with the performance of string links
52. Reference objects through interfaces
53. Interfaces take precedence over reflection mechanisms
54. Use the local method with caution
55. Careful Optimization
56. comply with universally accepted naming conventions

Exception

57. Use exceptions only in case of exceptions
58. Check exceptions for recoverable situations and run exceptions for programming errors
59. Avoid unnecessary use of checked exceptions
60. Standard-of-priority exceptions
61. Throwing an exception corresponding to the Abstraction
62. The exception thrown by each method must be documented.
63. Contain capture Failure Information in the detail message
64. Try to make the failure contain atomicity
65. Do not ignore exceptions

Concurrency

66. synchronous access to shared variable data
67. Avoid excessive Synchronization
68. executor and task take precedence over thread
69. Concurrent tools take precedence over wait and paiy
70. Documented thread security
......

Http://java.662p.com/thread-3615-1-1.html

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.