Why is high-quality engineering code difficult to write?

Why is high-quality engineering code difficult to write?

Author: hellojava Release time: 2016-11-30 13:53 Read: 12471 times Recommend: 27Original    link     [collection]  

  The reason why I thought of writing this article is because I recently saw various problems when a well-known open source project was used internally. I have to say that many open source ideas are good, but they are far from real engineering. There is a small distance, so there are no open source products adopted by commercial companies. If you want to introduce open source products, you must be careful. There are usually many pits waiting for you to fill in, and most of the more successful open source projects have commercial companies behind them. Improve.

  When I started learning to write asp code in 2000, I thought it was not difficult to write code. It was nothing more than learning grammar rules and libraries, and I could write it out. I remember one time when I was interviewing for an internship, I was asked to write it in an afternoon or so. A complete message board, that is, it is written with a brush, but as I work later, especially after joining Ali, I have become more and more aware of why it is difficult to write high-quality engineering code.

  In the early stage of writing code, the most concern is how to use code to achieve requirements. If it is only to achieve business requirements, even a programmer who has just started, as long as the problem-solving ability is OK, basically he can write code. So I always think that math scores are a very important requirement for programmers. People who are good at math usually have good problem solving and logical thinking skills.

  In the above basic process of writing code, students who write better will be reflected in the deep understanding and abstraction of the business, and the code written will have a certain ability to reuse, which will not be discussed here.

  But does the code end when the business requirements are fulfilled? In fact, it is far from it. This is actually just the beginning of writing the code. Except for the forward logic implementation, how to deal with the abnormal branch logic at any point is the engineering code. The more difficult part is that this problem is relatively easy to deal with in a stand-alone system, but it will become very complicated in a distributed environment. For example, the function of calling other machines has timed out and an error has occurred. How to deal with it? That's why there are so many distributed theories. After adding the processing logic of the exception branch, it is usually found that the proportion of the code of the forward logic in the whole code will be greatly reduced at this time.

  After the exception branch logic is processed, it is usually necessary to add necessary log information to facilitate troubleshooting when there is a problem, instead of when it comes to troubleshooting, the current system status is unclear at all, so important exception information is eaten. Not throwing this behavior is very shameful in writing code.

  After handling the relevant actions of the above exceptions, the robustness of the code should also be handled well, which mainly refers to:

  1. Self-protection ability

  Whether the externally provided interface has sufficient self-protection capabilities, that is, even if the user does not carefully read the API documentation and uses it indiscriminately, the system will not cause problems. There are many such cases. For example, if a batch query interface is provided externally, the user Pass an array with thousands of user IDs in it, and query it to directly consume the memory. In this case, you can't blame the person who uses it, but you should blame the protection of the API end for not doing well enough, according to this Looking at the standard, you will find that the APIs of many open source things are not qualified.

  Another is capacity protection. If the capacity of the processing concurrency is exceeded, what will happen at this time.

  2. Restrictions on the use of resources

  This is also where code novice or some open source products do poorly. It is easy to increase the scale, and the resource usage has been increasing without limit, and then the system will hang up. A very common case is the use of thread pools. For example, like Executors.newCachedThreadPool in Java, this interface is used by many people, but many people who use it have not thought carefully about whether a huge number of threads will be created here under certain circumstances; for example, using Map as cache , did not consider the size limit, the result is that as the amount of data grows, it suddenly hangs one day.

  Robustness is the more complex part of the code, and it is usually the part that shows the ability of the code. It may seem like a few lines of code, but in fact, the gap reflected behind it is huge.

  In addition to the gap in robustness of open source products, a huge gap usually occurs in the scalability of the design of the entire system. If the scalability is not enough, it usually leads to structural refactoring. Another common one is in concurrent processing. Not efficient enough, such as the rational use of locks, the introduction of lock-free algorithms, etc., and these require very strong system design and code skills.

  In addition to the above, high-quality engineering code also needs to consider maintainability (such as monitoring information exposure), security, etc. For me, I always think that the so-called engineering is actually turning some toy code into Code that can run really robustly in commercial systems.

  The above content is written relatively briefly, but it should also be seen that for high-quality engineering code, the implementation of business logic is only a small part of it, and even the time spent is relatively the least part, so I do I very much agree to let students write code during the interview. At this time, it is easy to see the ability of students to write code; sometimes, in order to check the proficiency of students to write code, I will ask the IDE shortcut keys, or make a handwritten paragraph not too complicated. code.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326105505&siteId=291194637