When should I use assert?

Source: Internet
Author: User

Http://sunxiunan.com /? P = 1638

 

------------------ Michael to pongba

There are two methods:

Method 1: Section * pimagesection = new section (pimage );

Assert (pimagesection );

Method 2: omitted

PS: Current Project TeamCodeThe first method is used, and no logs are written. Every time the client goes down, it takes a long time to locate the problem, causing a crash. Assert is everywhere.

I personally think that the location where assert is used is a serious error and cannot even makeProgramRun it again.

It would be too cruel to use asset everywhere. Sometimes, you should skip it gently, write the log, and return it. Tell me where the operation failed.

------------------

Below are my answers to this question:

First, we need to point out a very important issue that must be clarified before discussion. Assert should not be used directly, but should be packaged. For example, Microsoft has an Assert macro, it can trace useful information such as line file and cancel assert through Conditional compilation. Every time you locate the program for a long time, you can only say that this programming common sense has not yet been established.

Although the original article is quite simple, it involves several aspects: Version Management and publishing, error processing, and log writing.

 

First, under what circumstances should I use assert and under what circumstances should I handle wrong case?

In a mature code, about 30% to of the code is used to check the error, fault tolerance. For example, if you want to write an API or a function for external use, you need to make sure that the input pointer is not null and the input parameter is legal. At this time, it should not, note that assert should not be used because of the out of your control.

When can I use assert? It should be a rare example, and the code sections that you can control, such as object * P = new object, return a null pointer, in this case, most memory application errors occur. Even if you are fault-tolerant, the subsequent logic will not work normally. In this case, you should stop running and report an error. There is also your internal function (note that it is the internal function of your program), because the code Responsibility of the internal function is actually your own, you can only say that the level is too bad to blame others for passing the error parameter. Some people may say, what if other programmers in the same group call this function? This is certainly the case, but it cannot be avoided, but we can use assert and code review to standardize the situation. The problems in the same program group are much better solved by internal conflicts.

Therefore, the principle can be as follows: internal functions should simplify the fault tolerance mechanism as much as possible, while external interfaces should be as tolerant as possible, and error handling should be gentle.

 

The second question is, How to Write assert?

As I have mentioned, we recommend that you do not directly use assert. Add the # ifdef _ debug condition compilation option to ensure that the assert is not displayed in the release version. This actually involves version management. A commercial product release usually contains two versions (Release and debug checked), even in a Windows operating system. In the release version, disasble assert is required. In this case, if an error occurs, the following crash dump mechanism can be used to capture the trace. In addition, it must be noted that the release version must include PDB files (or similar Linux Files ), make sure that crash dump can be located. The debug version also needs to be sent to users. Users can use it to return more details. Each build of release and debug should be properly backed up. In the future, the customer will submit bugs, corresponding versions, and PDB, so that they can be accurately located without any errors.

 

You can also add the log option. Here, I ran a question. logging is also a big problem. We need to consider multithreading and performance issues. In fact, it's not just a good deal to join the log system, for those who use 2B number one, any good thing may be used badly. Generally, assert runs out, which is equivalent to crash for customers. Therefore, you can refer to Google's crash report open-source project. For example, in Windows, you can use the crash dump mechanism to capture the call stack when a crash occurs, in this way, even fail soon fail fast will not be ugly. These methods are common sense. A normal software development company should accumulate the wealth of these codes.

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.