Thoughts on when to use assert

Source: Internet
Author: User

Author: Zhu Jincan

Source:Http://www.cnblogs.com/clever101

 

A company colleague asked me: "When should I use it?Assert? For example, if the input parameter of a function is a pointer and you want to calculate it effectively, the pointer must be non-null. In the pop-up warning dialog box, will it be okay to prompt the other party? Why?Assert? I want to ask when to use a dialog box to prompt users and when to useAssert?"

 

This is a question worth thinking about. First, define assert ( here I am talking about C ++ environment ) it takes effect only after debugging. The release is invalid. The fact that debugging takes effect tells us that assert one of the important functions is to help developers debug Programs to locate errors. As a common fact for developers, it is not difficult to solve the bug , locating bugs is often the most difficult. This is why I advocate the use of assert . Let's look at another layer. Let's assume that assert can be used on the released version, so when to use assert and when to use the prompt dialog box, what is the principle? I think there is a user-level problem. Generally, there are two types of users: developers before release and users after release. As a developer, we assume that he understands assertions, but for users, we cannot assume that he understands assertions. If this interface is intended for developers, you should use assertions to restrict input parameters. If this interface is intended for users, apply the prompt dialog box, for example, if you select to select an input file in the pop-up dialog box, but you have not selected the file, click "OK". This should be prompted in the dialog box, it should not be assertion (if the assertion dialog box appears, he does not know what is going on ).

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.