Please indicate the original source, thank you!

The reason

Ali JAVA development manual has been published for a long time, which is worthy of careful study and promotion

  • Ali official Java code specification standard, this development manual not only standardized some development details, but also put forward a lot of engineering development philosophy, worth a good read.
  • Almost every aspect of everyday Java development is involved.
  • Every one of them is a pit stepped on by the predecessors, summed up through the lessons of blood.
  • It’s good for all Java developers to get it out there.
  • The development manual lists in detail how to develop more efficient, more fault tolerant, more collaborative, and strive to know what it is, better know what it is not, combined with positive and negative examples, improve code quality. For example, various irregularities in exception log handling; Set conversion of various pits; Create thread pool appear wait queue OOM etc.

Ali JAVA development manual is indeed worth our good reading and thinking, each is the previous step on the pit, through the lessons of blood summed up. So today on one of my own thinking and understanding to share.

Ali JAVA Development Manual

After reading this article, I think it is mainly cyclomatic complexity. Since the code is written by a human and needs to be maintained by a human, if it is complex enough, there is a great possibility of errors in writing, and maintenance and understanding are very difficult. And if you need to extend the already complicated and add a very simple function becomes very difficult (I believe you must have such experience).

Cyclomatic complexity

Cyclomatic complexity is a measure of code complexity. In the concept of software testing, cyclomatic complexity is used to measure the complexity of a module’s decision structure, which is expressed as the number of independent linear paths, that is, the minimum number of paths needed to be tested reasonably to prevent errors. High cyclomatic complexity indicates that program code may be of low quality and difficult to test and maintain.

If you have to use if()… else if()… else… To express logic, avoid subsequent code maintenance difficulties, please don’t go beyond 3 layers. If there are more than 3 levels of if-else logic determination code can be implemented using guard statements, policy mode, state mode, etc.

In my opinion, the use of guard statements, policy patterns, and state patterns is to reduce cyclomatic complexity and make the code simpler so that both the coders and maintainers can easily understand the essence of the code.

Although ali JAVA development manual refers to if()… else if()… else… The way to express logic, extension, about multiple nested loops and other truths are the same, need to consider optimization.

Thought analysis

Judge a tree

As shown in the figure above, it actually looks like a tree structure, relatively speaking, it is actually more complex, the idea of optimization is to turn the tree structure into a sequential structure, so that the order is clear, the overall idea is like this, let’s see how to use the guard statement, strategy mode, state mode to achieve.

Who’s statement

Who statement? For example, if a complex expression is nested with several layers of if-then-else statements, it is converted into multiple if statements to realize its logic. The multiple IF statements are protected statements.

The following is an example of a central defender statement:

public void today(a) {
    if (isBusy()) {
        System.out.println("change time.");
        return;
    }
    if (isFree()) {
        System.out.println("go to travel."); 
        return;
    }
    System.out.println("stay at home to learn Alibaba Java Coding Guidelines.");
    return;
}Copy the code

In fact, this is relatively simple, each if corresponds to a path to the leaf node (each if basically returns).

The strategy pattern

Overview: Use this pattern to encapsulate a set of algorithms into a series of objects. Passing these objects gives you the flexibility to change the functionality of your program.

Strategy mode is more famous is zhuge Liang’s three tricks up his sleeve, as shown in the figure:

Why did Zhuge Liang bother to do three things? He could have just made a costumer and written all three of them on it. Instead of doing that, he used the strategy model correctly and made three tips. The benefits of doing so are very obvious: Zhuge Liang wrote a clever plan for a good idea, his thinking is very clear, not the three strategies of confusion. And Zhao Yun is also very convenient to see the clever plan, when to see which clever plan, the use is very convenient, if three clever plan mixed together, he is not so convenient.

In the JDK, java.util.Comparator#compare() is the policy pattern that we use, for example, when we sort items, there are many conditions, In fact, this is also the tree above, according to the product view, price, update time, [price, time], [view, update time] (ascending and descending operation), what we need to do is to take one leaf node at a time. A lot of times these judgments are written in a common method that makes a lot of judgments and then writes sort. How does the JDK do that? So, taking out the comparative judgment of change, which is actually a strategy of judging every leaf in the tree, imagine what we do in normal times? We write the Comparator#compare(). (There are probably many sorting algorithms that implement the Comparator interface.)

Unlike the wei statement, which assigns each if to a path to a leaf node. When using the policy mode, the leaves implement the Comparator interface. When using the policy mode, the leaves implement the “if” interface.

The state pattern

Overview: Allowing an object to change its behavior when its internal state changes, the object appears to have changed its class. The main solution is that the behavior of an object depends on its state (properties) and can change its related behavior as its state changes.

  1. Encapsulates the transformation rules.
  2. Enumerates possible states. The type of state needs to be determined before enumerating states.
  3. Put all the behavior associated with a state into a single class, and you can easily add new states by simply changing the object’s state to change its behavior.
  4. Allows state transition logic to be integrated with state objects, rather than some giant block of conditional statements.
  5. You can reduce the number of objects in the system by having multiple environment objects share a state object.

Since the state mode encapsulates the transformation rules, the general depth of the tree needs at least 2 layers and up, so the feeling of personal understanding is a process, such as * water below 0 degrees is ice state -> > 0 degrees and then becomes liquid -> 100 degrees and then becomes boiling state

The state pattern is much like the policy pattern in that the policy pattern is externally driven and the state pattern is internally driven. It’s essentially a path that takes only one of the leaves from the tree.

State mode has an obvious shortcoming: state pattern to the support of “the open closed principle” is not too good, for the state pattern can switch state, increase new state class need to modify the source code is responsible for the state transition, otherwise unable to switch to the new condition, and modify the behavior of the state of a class also need to modify the source code for the class.

Programmer jokes

The wife makes a phone call to be programmer’s husband: come off work along the way buy 10 steamed stuffed bun, if see sell watermelon, buy 1. That night husband holding a steamed stuffed bun into the house… Wife nu way: how do you buy a steamed stuffed bun only? ! The husband is very afraid, mumble way: because I really saw sell watermelon.”

If the strategy mode is used, it is easy, 2 messages, 1: buy ten baozi. 2: Buy a watermelon and forget it. It’s a joke, but the sequence is easier than the judgment.

conclusion

  • Cyclomatic complexity concept
  • Who’s statement
  • The strategy pattern
  • The state pattern

Summary: The essence of the deep judgment tree, when used, is to turn the judgment tree structure into a sequential structure, which is to give the path of each leaf without looking like a complex tree structure.

thinking

Ali JAVA Development Manual

This is ali JAVA development manual one of the details, why, the result is what, how unexpected? Looking forward to your comments and analysis!!


Some of the above analysis is personal understanding and thinking, if found wrong hope message pointed out, thank you!! If you find it rewarding, please click “like” and follow it.


Consult historical articles, welcome to pay attention to personal public number!!

Originality zero public account.jpg