Use C ++ instead of C (2) using assertions

Source: Internet
Author: User

Use assertions
12. Select assert Macro when using MFC
13. Do not use verify macro or assertion macro.
14. You must call the assert_valid macro before using the object of the cobject derived class.
15. always call the assert_valid macro before calling the assert_kindof macro
16. Also transplanted assertions when porting code
17. To Use assertions effectively, you need certain policies. Do not randomly distribute assertions into your code, but follow some established models.
18. "assert the word", that is, do not try to select an asserted to discover an error, but assert everything according to your asserted policy.
19. The most powerful assertions are used to verify the fixed relationship between variables. These links are called invariants ). Among them, the class and loop relationships are the most common.
20. design programs without changing relationships, understand them before writing code, and create documents for them.
21. Public member functions require more comprehensive assertions than private and protected member functions
22. Fully implement the assertvalid function. Do not use the default implementation provided by MFC classwizard.
23. We recommend that the assertvalid function adopt the following asserted mode:
Void cmyobject: assertvalid ()
{
// Check the immediate base class first
Cmyobjectbaseclass: assertvalid ();
 
// Check the data members not in the base class
Assert_valid (m_pobject1 );
Assert_valid (m_pobject2 );

// Now check the class invariants not checked by the base class
// Be sure to document the Invariants
Assert (m_value! = Illegalvalue );
Assert (m_object1.getsize () = m_size );
...
}
24. incorrect use of assertions may cause errors. Assertions are used to reveal errors, not to correct runtime errors.
25. If your program is defensive, don't forget to use assertions. If you use assertions, do not forget Defensive Programming. It is best to combine these two technologies.
26. Consider using _ asserte (false) to simplify the combination of Defensive Programming and assertions. To obtain more descriptive assertions, consider using _ asserte ("Problem description." = 0 ).
27. assertions are not a substitute for error handling.
28. This method allows you to view the code that causes the assertions to become invalid at any time after the window appears without disturbing the window drawing.
Void cmyview: ondraw (CDC * PDC ){
Int previusreportmode = _ crtsetreportmode (_ crt_assert,
_ Crtdbg_mode_debug ):
Cmydoc * pdoc = getdocument ();
Assert_valid (pdoc );
... // Draw the window
If (previusreportmode! =-1)
_ Crtsetreportmode (_ crt_assert, previusreportmode );
}
29. Use the getobjecttype function to assert a GDI object with a valid handle (check whether the returned value is zero) or a specific GDI object (check whether the returned value is a special value ). Eg:
_ Assert (getobjecttype (hbrush) = obj_brush );
However, be aware that the getobjecttype function may return some surprising results. For example, the following assertions are invalid:
Hbrush = createsolidbrush (RGB (0, 0, 0 ));
Deleteobject (hbrush );
_ Asserte (getobjecttype (hbrush) = obj_brush );
Because the black brush is an object of a backup device (that is, it cannot be deleted), The deleteobject function call is useless.
30. The msdn documentation claims that the isbadcodeptr, isbadreadptr, isbadstringptr, and isbadwriteptr API functions will cause assertions to fail in the debug version when receiving bad pointers. This statement is incorrect. These functions must be encapsulated in the asserted statement.

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.