Write Java like go, and discuss a programming style from Go features.

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

In the Reprint blog, the features of Go are mentioned, and as a reference to write Java, you can also give yourself some programming style.

The following are the improvements mentioned

    • The following is a simple study and analysis of the key style hints, understand Ken and pike programming Ideas:

    • Canonical syntax (does not require symbol table to parse) garbage collection (exclusive) Headless file explicit dependency no loop dependent constants can only be numeric int and int32 are two types of alphabetic case setting visibility Visibility) any type (type) has a method (not a type) with no subtype inheritance (not a subclass) for package-level initialization and an explicit initialization order file that is compiled into a package package-level globals presented in any Order does not have a numeric type conversion (constant-assisted) interface implicitly implemented (no "implement" Declaration) embedding (not promoted to superclass) method according to function declaration (no special location required) method that is function interface only method (no data) method by name matching (not type) No constructors and destructors postincrement (such as ++i) are state, Not the expression does not have preincrement (i++) and predecrement assignment is not an expression definite assignment and the calculation order in the function call (there is no "sequence point") No pointer operation memory has been initialized with a value of 0 values in the local variable value legal method does not have " This "fragment stack does not have static and other types of annotations no template no exception built-in string, slice, and map array bounds check


    • Constants can only be numbers

      Can learn. In JDBC programming, we put the SQL statement in the Commission configuration file.

    • No subtype inheritance (not subclass)

      Use less inheritance


    • Package-level initialization and explicit initialization order

      Note the initialization order of static variables and constructors, and the order in Java is related to the order in which the static variables are declared in the class.

    • The files are compiled into a package.

      Reduce DLL nightmares and jar nightmares with less use of others ' jar packs.


  • Interface implicit implementation (no "implement" declaration)

    Using the object type appropriately, Jfinal does so. High efficiency.

  • Embedding (not promoted to Super Class)

    Multi-aggregation, multi-combination, less inheritance

  • Method according to function declaration (no special location required)

    Static method can be static, improve efficiency. There is also the ability to put static methods inside a tool class, without initializing this class, and calling directly. That's what Fastjson did. greatly improve efficiency.

  • Method is a function

    Ditto

  • Interface only method (no data)

    Interface does not declare data inside. The Java community has debated a lot of questions before, and go gives a reference answer.

  • method is matched by name (not type)

    This amount uses the object premise.

  • No constructors and destructors

    Instead of relying on constructors, consider writing init methods and Destory methods, manually fetching, freeing links and spaces, and improving efficiency.

  • Postincrement (such as ++i) is a state, not an expression

    Use + + with caution

  • No Preincrement (i++) and predecrement

    No + +

  • Assignment is not an expression

    Prevent the 2 = = and = confuse

  • Definite assignment and Calculation order in function call (no "sequence point")

    Multi-use parentheses. Improve Calculation Order readability

  • Memory is initialized with a value of 0.

    Note the initialized value. See if the situation is to assign an initial value.

  • There is no "this" in the method

    Ah, good stuff. A very important reminder!! I vaguely thought this was inappropriate when I was programming. Bring a lot of ambiguity and hidden trouble.


  • No templates

    Java templates are too disgusting. Although inefficient, the reflection constructor is still used ...

  • No exception

    Less use of abnormal processing, can predict the failure to write program control as far as possible.

  • Built-in string, slice, and map

    Find a String,map tool to take advantage of the hand. If not, write one yourself, I choose Guava.

  • Array bounds checking

    Notice the cross-border error. The number of values in the array must be judged before all direct array reads, never cross the boundary.



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.