在多个return语句之前重复代码 [英] repeated code before multiple return statements

查看:121
本文介绍了在多个return语句之前重复代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好!

请就以下问题提出建议。我有一个函数在多个分支中返回

1或0,并且它必须在每次返回

之前执行相同的代码,如下所示:

int fun()

{

if(some code)

{

restore(param1,param2, param3,param4,param5);

返回1;

}

否则if(某些代码)

{

恢复(param1,param2,param3,param4,param5);

返回0;

}

恢复(param1,param2,param3,param4,param5);

返回1;

}


有没有其他方法可以从纯C中的

函数返回后执行一些代码?重复的代码使我的功能超过两次

更长时间,它失去了可读性。我想到的唯一的事情是

是goto。


谢谢,Alex。

解决方案

Alexander Stoyakin写道:


>

你好!

请就以下问题提供建议。我有一个函数在多个分支中返回

1或0,并且它必须在每次返回

之前执行相同的代码,如下所示:

int fun()

{

if(some code)

{

restore(param1,param2, param3,param4,param5);

返回1;

}

否则if(某些代码)

{

恢复(param1,param2,param3,param4,param5);

返回0;

}

恢复(param1,param2,param3,param4,param5);

返回1;

}


有没有其他方法可以从纯C中的

函数返回后执行一些代码?重复的代码使我的功能超过两次

更长时间,它失去了可读性。我想到的唯一的事情是

是goto。



这样的事情怎么样?


int fun()

{

int retval;


if(some code)

retval = 1;

else if(some other代码)

retval = 0;

else

retval = 1;


restore(param1 ,param2,param3,param4,param5);


返回retval;

}


-

+ ------------------------- + ----------------- --- + ----------------------- +

| Kenneth J. Brody | www.hvcomputer.com | #include |

| kenbrody / at\spamcop.net | www.fptech.com | < std_disclaimer.h |

+ ------------------------- + --------- ----------- + ----------------------- +

不要 - 邮寄给我:< mailto:Th ************* @ gmail.com>


Alexander Stoyakin< st***@ua.fmwrites:


请就以下问题提出建议。我有一个函数在多个分支中返回

1或0,并且它必须在每次返回

之前执行相同的代码,如下所示:

int fun()

{

if(some code)

{

restore(param1,param2, param3,param4,param5);

返回1;

}

否则if(某些代码)

{

恢复(param1,param2,param3,param4,param5);

返回0;

}

恢复(param1,param2,param3,param4,param5);

返回1;

}



int fun2 ()

{

如果(某些代码)

返回1;

否则如果(某些代码)

返回0;

其他

返回1;

}


int fun()

{

int retval = fun2();

restore(param1,param2,param3,param4,param5);

返回retval;

}

-

不要使用Usenet作为律师,因为t嘿,肯定会说不和是。


7月27日下午2点,Kenneth Brody< kenbr ... @ spamcop.netwrote:
< blockquote class =post_quotes>
这样的事情怎么样?


int fun()

{

int retval;


if(some code)

retval = 1;

else if(其他一些代码)

retval = 0;

else

retval = 1;


restore(param1,param2,param3, param4,param5);


返回retval;

}



怎么样


int retval =(some_code || !some_other_code)? 1:0;


恢复(param1,param2,param3,param4,param5);

返回retval;





(取决于some_code和some_other_code实际上是什么,这个

在实践中可能无法读取。)


Hello!
Please advise on the following issue. I have a function which returns
1 or 0 in multiple branches, and it must perform the same code before
each return, like this:
int fun()
{
if (some code)
{
restore(param1,param2,param3,param4,param5);
return 1;
}
else if (some code)
{
restore(param1,param2,param3,param4,param5);
return 0;
}
restore(param1,param2,param3,param4,param5);
return 1;
}

Is there any alternative way to execute some code upon return from the
function in pure C? Repeated code makes my function more than twice
longer and it losts its readability. The only thing coming to my mind
is goto.

Thanks, Alex.

解决方案

Alexander Stoyakin wrote:

>
Hello!
Please advise on the following issue. I have a function which returns
1 or 0 in multiple branches, and it must perform the same code before
each return, like this:
int fun()
{
if (some code)
{
restore(param1,param2,param3,param4,param5);
return 1;
}
else if (some code)
{
restore(param1,param2,param3,param4,param5);
return 0;
}
restore(param1,param2,param3,param4,param5);
return 1;
}

Is there any alternative way to execute some code upon return from the
function in pure C? Repeated code makes my function more than twice
longer and it losts its readability. The only thing coming to my mind
is goto.

What about something like this?

int fun()
{
int retval;

if ( some code )
retval = 1;
else if ( some other code )
retval = 0;
else
retval = 1;

restore(param1, param2, param3, param4, param5);

return retval;
}

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h|
+-------------------------+--------------------+-----------------------+
Don''t e-mail me at: <mailto:Th*************@gmail.com>


Alexander Stoyakin <st***@ua.fmwrites:

Please advise on the following issue. I have a function which returns
1 or 0 in multiple branches, and it must perform the same code before
each return, like this:
int fun()
{
if (some code)
{
restore(param1,param2,param3,param4,param5);
return 1;
}
else if (some code)
{
restore(param1,param2,param3,param4,param5);
return 0;
}
restore(param1,param2,param3,param4,param5);
return 1;
}

int fun2()
{
if (some code)
return 1;
else if (some code)
return 0;
else
return 1;
}

int fun()
{
int retval = fun2();
restore(param1,param2,param3,param4,param5);
return retval;
}
--
Go not to Usenet for counsel, for they will say both no and yes.


On Jul 27, 2:00 pm, Kenneth Brody <kenbr...@spamcop.netwrote:

What about something like this?

int fun()
{
int retval;

if ( some code )
retval = 1;
else if ( some other code )
retval = 0;
else
retval = 1;

restore(param1, param2, param3, param4, param5);

return retval;
}

How about

int retval = ( some_code || !some_other_code ) ? 1 : 0;

restore(param1, param2, param3, param4, param5);
return retval;

?

(Depending on what some_code and some_other_code actually are, this
may not be readable in practice.)


这篇关于在多个return语句之前重复代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆