Created
March 12, 2013 00:33
-
-
Save LearnCocos2D/5139277 to your computer and use it in GitHub Desktop.
how I would rewrite the pseudocode from http://www.cocos2d-x.org/boards/6/topics/16774
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bool foo(char* p1, int* p2) | |
{ | |
// local variable declaration | |
bool bRet = false; // init all local variables here, assume everything is wrong | |
int* ptr = NULL; | |
PROFILER_START(); | |
// check input params | |
if (p1 && p2) | |
{ | |
// start our affairs | |
if (createSomething() && initSomething()) | |
{ | |
if (cond) | |
{ | |
ptr = (int*)mallc(123123243); | |
} | |
bRet = (ptr && doSomething(ptr)); | |
} | |
} | |
// start clean up phase | |
if (ptr) | |
{ | |
delete ptr; | |
ptr = NULL; | |
} | |
if (!bRet) | |
{ | |
reserveSomething(); | |
} | |
// So we haven't missed anything until here. | |
// Make sure the local variables are cleaned, especially the pointer's are deleted. | |
// A function should have only one return point! | |
// In this way, it's very easy to add profiler into each function, only one line at start and one at the end. | |
PROFILER_STOP(); | |
return bRet; | |
} | |
The second example is moot, just let the if/else run to the end: | |
1bool foo() | |
2{ | |
bool bRet = false; | |
3 PROFILER_START(); | |
4 | |
5 if (condition1 && condition2) | |
6 { | |
8 bRet = true; | |
9 } | |
10 else if (condition3) | |
11 { | |
12 if (condition4) | |
13 { | |
15 bRet = true; | |
16 } | |
17 else | |
18 { | |
20 bRet = false; | |
21 } | |
22 } | |
23 // Are you sure you have return value for each condition cases? Code reviewer will kick your ass. | |
// Huh? Yes, of course! | |
7 PROFILER_STOP(); | |
return bRet; | |
24} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment