why doesnot use do while(0) in scene create function and init


#1

In the old version of cocos2d-x,usually use do while(0) in CCScene function scene() and in init()fuction ,but when I see cocos2d-x2.1.4,this is disaper,can some one tell me why use in old version and not use in new version,thanks


#2

i don’t think do{} while(0) is a good coding style.

gao teng wrote:

In the old version of cocos2d-x,usually use do while(0) in CCScene function scene() and in init()fuction ,but when I see cocos2d-x2.1.4,this is disaper,can some one tell me why use in old version and not use in new version,thanks


#3

some articles say use “do{} while(0)” can avoid the redundant code,but why they delete in the new version,i cannot understand
Like Zhang wrote:

i don’t think do{} while(0) is a good coding style.
>
gao teng wrote:
> In the old version of cocos2d-x,usually use do while(0) in CCScene function scene() and in init()fuction ,but when I see cocos2d-x2.1.4,this is disaper,can some one tell me why use in old version and not use in new version,thanks


#4

in most cases, u don’t have to. why do u think we should use that crappy do {} while(0) pattern?

gao teng wrote:

some articles say use “do{} while(0)” can avoid the redundant code,but why they delete in the new version,i cannot understand
Like Zhang wrote:
> i don’t think do{} while(0) is a good coding style.
>
> gao teng wrote:
> > In the old version of cocos2d-x,usually use do while(0) in CCScene function scene() and in init()fuction ,but when I see cocos2d-x2.1.4,this is disaper,can some one tell me why use in old version and not use in new version,thanks


#5

Consider this examples :

void fun1() {
    do {
        //some code
    while(0);
}

void fun2() {
   //some code
}

This basically does the same thing, and a good compiler should strip the first function of the unnecessary ‘do … while’ enclosing.
You may have seen it in macros where it is used as some kind of a trick and a workaround to to let you put a semicolon at the end of the line without causing compiler warnings/errors.

Btw. - could you provide a link to the article you mentioned? I cannot imagine any case where ‘do … while (0);’ would help in avoiding redundancy, as it basically is a redundant enclosing.


#6

this is the article
http://www.cnblogs.com/flying_bat/archive/2008/01/18/1044693.html,but it is written in chinese.
see this example
At the begining,we need new some source,and when we meet error in middle of the funtion,wo need exit the funtion.
bool Execute()
{
// 分配资源
int **p = new int;
bool bOk;
// 执行并进行错误处理
bOk = func1;
if
{
delete p;
p = NULL;
return false;
}
bOk = func2;
if
{
delete p;
p = NULL;
return false;
}
bOk = func3;
if
{
delete p;
p = NULL;
return false;
}
// ……….
// 执行成功,释放资源并返回
delete p;
p = NULL;
return true;
}
**so we use the release operate many times,
but if we write as follow:*

bool Execute()
{

int *p = new int;

bool bOk(true);
do
{

bOk = func1();
if(!bOk) break;

bOk = func2();
if(!bOk) break;

bOk = func3();
if(!bOk) break;

// ……….

}while(0);

delete p;
p = NULL;
return bOk;

}
we will see ,it can avoid the redundant code


#7

This is a good example of “Perfect for hello world”

  1. Few real-world program will have the same cleanup code. Your sample code simply want to delete pointer p for cleaning up. Obviously, we all know this is stupid since you have no reason to create pointer P before you validate all those functions. see below code:

int p = new int;
if || !func2 || !func3)
{
delete p;
p = NULL; // hold on, why do u wanna set p to NULL here? just to demonstrate you can “save an extra line”???
}

Man, you can make your life much easier by just write:
if || !func2 || !func3)
{
return;
}
int
p = new int;

  1. In real world, especially for C coding, people usually use:

if (!func1())
{
goto CLEANUP;
}

if (!func2())
{
goto CLEANUP;
}

CLEANUP:

Yes, it’s not the “school teach” coding style and against most “in book” standard, but it’s widely used in many areas include device driver/network protocols/etc.

gao teng wrote:

this is the article
http://www.cnblogs.com/flying_bat/archive/2008/01/18/1044693.html,but it is written in chinese.
see this example
At the begining,we need new some source,and when we meet error in middle of the funtion,wo need exit the funtion.
bool Execute()
{
// 分配资源
int **p = new int;
bool bOk;
>
// 执行并进行错误处理
bOk = func1;
if
{
delete p;
p = NULL;
return false;
}
>
bOk = func2;
if
{
delete p;
p = NULL;
return false;
}
>
bOk = func3;
if
{
delete p;
p = NULL;
return false;
}
>
// ……….
>
// 执行成功,释放资源并返回
delete p;
p = NULL;
return true;
>
}
>**so we use the release operate many times,
but if we write as follow:*
>
>
bool Execute()
{
>
int *p = new int;
>
bool bOk(true);
do
{
>
bOk = func1();
if(!bOk) break;
>
bOk = func2();
if(!bOk) break;
>
bOk = func3();
if(!bOk) break;
>
// ……….
>
}while(0);
>
>
delete p;
p = NULL;
return bOk;
>
}
we will see ,it can avoid the redundant code