~~~~ 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.