Effective Java experience, enumerations, annotations, methods, generic design, exceptions

Source: Internet
Author: User

这几章看的比较快,内容就如同标题一样比较容易理解,所以只有部分内容会在[]中解释,其他的就直接理解标题,并不影响阅读质量。不过如果时间充足的话,还是仔细读一读原书的内容,相信还是有所收获的。主要最近自己想进入算法与机器学习部分,尽快结束这本书。另一方面,讨论一些自己感兴趣的内容,我会将搞过的东西总结下。后面可能写一部分关于java字节码阅读以及编写的东西。只所以这么来,是因为字节码编程还是非常用途实际的,这一"java编译语言"使得String与StringBuilder的性能对比又有了新的认知,为了更好的编写程序,这些是值得学习的。之后,我会在5月份投入到机器学习中,╮(╯▽╰)╭。

Reprint Please specify source: http://blog.csdn.net/supera_li/article/details/45155857

Enumerations and annotations
    1. Use an enum instead of an int constant. [Class | Method General constants, enum centralized to facilitate management | call | change]
    2. Replace ordinal with instance field
    3. Replacing bit fields with Enumset
    4. Replace ordinal index with Enummap
    5. Simulating a scalable enumeration with interfaces [enumeration of interface types, recommended]
    6. Annotations take precedence over naming patterns [that's what the note pack is]
    7. Insist on using the override annotation [override if no annotations are displayed, you may throw a method that does not actually overwrite the parent class, making future programs a potential bug]
    8. Defining a type with a markup interface [eg: it is something that is a trade-off between some serialization classes and Targe (ENUM. Constants). If you apply a specialized interface, use the former]
Method
    1. Check the validity of the parameter [method at the beginning of the parameter check, the parameters to be trusted to make a credible judgment]
    2. Protective copy when necessary [output to the client, the data returned by the client is not necessarily trustworthy, if possible, a copy on the server side, which is the most reliable]
    3. Careful design method signature
    4. Use overloading with caution [can be less, can be called by a type conversion call, try to avoid overloading]
    5. Use variable parameters sparingly [method parameters should be as few as possible]
    6. Returns a 0-length array or collection, not null[, or the upper code always needs to do a null test, or a lot of comments when returning null.
    7. Write a document comment for all exported API elements
General programming
    1. Minimizing the scope of local variables
    2. For-each loops take precedence over traditional for loops
    3. Understanding and using class libraries
    4. If precise answers are required, avoid using float and double[only for scientific calculations using floating-point numbers, and sometimes consider using large-number classes BigDecimal instead of related operations]
    5. The base type takes precedence over the boxed base type [the latter belongs to the class, especially in the cycle of automatic unboxing and boxing, the performance degradation is more serious]
    6. If other types are more appropriate, try to avoid using strings [strings have a higher performance cost, a connection + sign represents the new character object, consider the StringBuilder class instead? ]
    7. Beware of the performance of string connections [similar to the connection + number is the object's new operation]
    8. Referencing an object through an interface [after changing an object implementation, you only need to change the new object type after the interface, and the other code will continue to work]
    9. The interface takes precedence over the reflection mechanism [the reflection mechanism is mainly intrusive, and the reflected anomalies are concentrated in the running period, which is not easy for abnormal control, so it is not recommended]
    10. Use local methods with caution [platform migration is over]
    11. Careful optimization [Analysis good optimization, as far as possible in the design of good or bad to optimize, otherwise optimization may lead to unnecessary waste]
    12. Adherence to universally accepted naming conventions [easy to understand and team consensus is the norm]
Abnormal
    1. Exceptions are used only for exceptions [a more normal sentence, but the definition of anomalies requires careful consideration]
    2. Use a run-time exception for a programmatic error with a inspected exception for recoverable cases
    3. Avoid the need to use a inspected exception [incoming parameter trustworthiness]
    4. Preferential use of standard-corresponding exceptions [standardized, easy to understand]
    5. Each method throws an exception with a document [easy to understand]
    6. Contains information that captures the failure in the detail message [should be called a reserved exception field, to be able to know the context content when the exception occurs]
    7. Trying to keep the failure atomic [is not to say that many failures are treated as a failure]
    8. Do not ignore exceptions [catch and do a deal, cannot ignore, or neglect to give a comment]

Effective Java experience, enumerations, annotations, methods, generic design, exceptions

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.