Introduction to cocos2dx (15) Anti-exception programming mode

Source: Internet
Author: User
~~~~ My life, my little bit by bit !! Anti-exception programming mode. This is what I saw when I browsed other blogs. Here I will give a general description of it. I 'd like to write articles about this aspect on Baidu. In fact, the anti-exception programming mode is not a programming mode, but we should take some defense measures to ensure the robustness of the program when writing code.

~~~~ My life, my little bit by bit !! Anti-exception programming mode. This is what I saw when I browsed other blogs. Here I will give a general description of it. I 'd like to write articles about this aspect on Baidu. In fact, the anti-exception programming mode is not a programming mode, but we should take some defense measures to ensure the robustness of the program when writing code.

~~~~ My life, my little bit by bit !!

Anti-exception programming mode. This is what I saw when I browsed other blogs. Here I will give a general description of it. I 'd like to write articles about this aspect on Baidu. In fact, the anti-exception programming mode is not a programming mode, but we should take some defense measures to ensure the robustness of the program when writing code, we usually write code with certain assumptions. Imagine that if the user inputs an invalid value, our assumptions will be broken, and the program will be prone to bugs, so in order to prevent program errors, we need to take some measures to avoid the bug caused by such uncertain operations. This is called the anti-exception programming mode.

This is often seen in cocos code, such as do... while in combination with CC_BREAK_IF, if a certain object is not initialized successfully, it will break this loop. At this time, the init return value is false, so that we know that the program has an error. However, the init function of version 3.0 is not written in this way. I think it may be too troublesome to write this code. That is to say, this programming mode will cause code bloated. There are also some macros provided by the engine for us. We should try to use these macros during programming to ensure program robustness.

Cocos2dx provides many macro definitions to prevent exceptions as much as possible.

#define CC_SAFE_DELETE(p)            do { if(p) { delete (p); (p) = 0; } } while(0)#define CC_SAFE_DELETE_ARRAY(p)     do { if(p) { delete[] (p); (p) = 0; } } while(0)#define CC_SAFE_FREE(p)                do { if(p) { free(p); (p) = 0; } } while(0)#define CC_SAFE_RELEASE(p)            do { if(p) { (p)->release(); } } while(0)#define CC_SAFE_RELEASE_NULL(p)        do { if(p) { (p)->release(); (p) = 0; } } while(0)#define CC_SAFE_RETAIN(p)            do { if(p) { (p)->retain(); } } while(0)

Therefore, this programming mode is actually a measure taken to consider some bugs that may exist in the program. For example, it checks the parameters passed by the function and uses symbolic constants to define some file names or numbers, it is best to have a return value for a function, so that you can easily find bugs. Well, that's all. I think this programming mode is also a habit. If you want to develop good programming habits, pay attention to these small details.


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.