Recently alibaba-P3C released a new version of the Java development manual (you can go to Github Alibaba-P3C for related resources), I will follow this matter to say my understanding of the development specification.

When it comes to development standards, what impresses me most is the requirement of a tutor of my elder brother during my graduate study. At that time, I was developing a B/S 3D rendering engine, in which I was in charge of the Web part. In the process of code review, my big brother criticized me severely (and was very grateful for his strict requirements at that time). The reason was that part of the Web page I developed was text introduction. The punctuation marks are mixed in Chinese and English, and the code is not standard, such as inconsistent indentation, the space is not required and so on. It was then that I became very serious about code specifications. This later to write their graduation thesis and so on also played a good role in promoting, because the tutor is relatively strict with this requirement.

Getting back to the code specification thing, good code is really enjoyable to read, and of course I’m always on a quest to write good code. The code style in the code specification is the simplest of these, and the Java development manual mentioned above has strict instructions for this part, but not many people actually do it. Logically, this part is also the easiest to follow because there is a template. Remember there was a TV show (what’s it called?) It’s about couples breaking up because they can’t decide whether to use the space or TAB key (or Vim and Emacs). However, it is necessary to maintain a uniform code style in a team. After all, you are not the only one to read your code. Spaces and tabs may display differently on different terminals and editors, which will affect the reading experience.

As for code specifications, I also hope to infect people around me through my own efforts. For example, in the hackthon of CreditEase earlier, I organized a team to launch a project called “Code Review Advisor”. The purpose of the project was actually very simple, that is, to let everyone code in accordance with the unified specification in advance. If there is a violation of the specification, the code submitter will be directly informed in the form of Comment that it needs to be modified.

Later, when I came to DJI, I asked the team to strictly abide by the specifications and completely standardized the code submitted by everyone with some technical means. For the code that violated the corresponding rules, it was not allowed to be submitted or packaged directly, which was actually taking advantage of the external open source tool support of Alibaba. I have to say that I admire Alibaba for its contribution to the community.

Now, IN Ali, I find that the reality is that the specification is actually a good vision. Due to various historical reasons, in fact, everyone’s code is not implemented and implemented according to the expected specification. This should also be a common situation. As gujuanda said in one of the exchange meetings, a good vision is definitely needed, and to realize it requires everyone to spend time and energy to promote it.

The above is a signed version of the development manual (forgive me for being uneducated and unable to recognize it) that I got at the conference. Let me use one of these examples to illustrate the benefits of specifications.

public class SimpleDateFormatTest { public static final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); public static void main(String[] args) { Date now = new Date(); String printedDate = simpleDateFormat.format(now); System.out.println(printedDate); for (int i = 0; i < 10; i++) { new Thread(new Runnable() { @Override public void run() { try { Date date = simpleDateFormat.parse(printedDate); System.out.println(simpleDateFormat.format(date)); } catch (ParseException e) { e.printStackTrace(); } } }).start(); }}}Copy the code

The code snippet above is an example of an incorrect use of SimpleDateFormat in the spec. It is actually a thread-safe issue. Run the code snippet above and expect the output to be a correct string representation (e.g. 2020-04-25 15:41:34). Actual process may throw exceptions (such as Java. Lang. A NumberFormatException), also the output may be some strange time (for example, the 0015-03-25 15:41:34), it is also good, because, after all, or abnormal, or “time” abnormal “, The worst part is that it is possible to output a correctly formatted time, just not as expected, which is relatively difficult to troubleshoot.

For example, the problem of yyyy and YYYY occurred in all systems at the beginning of the New Year is also explained in this manual. Therefore, it is better to know more about similar specifications.

If you find this article useful, please share, retweet and “check it out”. Thank you!

Welcome to contact me by scanning the code on wechat

This article is published by OpenWrite, a blogging tool platform