Skip to content

Instantly share code, notes, and snippets.

Created March 12, 2013 00:25
Show Gist options
  • Save anonymous/5139243 to your computer and use it in GitHub Desktop.
Save anonymous/5139243 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
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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment