do / while false [英] do/while false
问题描述
嗨:
我最近碰到了很多程序员使用以下
类型的do / while(false)结构经常:
做{
如果(!CatchFish(pFish)){
休息;
}
if(!CleanFish(pFish)){
break;
}
if(!CookFish (pFish)){
休息;
}
if(!EatFish(pFish)){
break ;
}
而(假);
删除pFish;
这是有效但在我看来是不好的风格。 do / while循环
实际上并不是一个有误导性的循环。同样可以用布尔标志实现
。
例如,
bool bSuccess = CatchFish(pFish );
if(bSuccess){
bSuccess = CleanFish(pFish);
}
等
没有使用异常,因为我们假设方法
在非特殊情况下可以返回false。
对这种do / while(假)风格的任何评论?
Angus。
Hi:
I''ve recently come across a number of programmers who use the following
type of do/while(false) structure quite often:
do {
if (! CatchFish(pFish)) {
break;
}
if (! CleanFish(pFish)) {
break;
}
if (! CookFish(pFish)) {
break;
}
if (! EatFish(pFish)) {
break;
}
while (false);
delete pFish;
This is effective but seems to me to be bad style. The do/while loop
isn''t really a loop at all which is misleading. The same could be
achieved with a boolean flag.
e.g.,
bool bSuccess = CatchFish(pFish);
if (bSuccess) {
bSuccess = CleanFish(pFish);
}
etc.
Exceptions are not being used because we are assuming that the methods
can return false under non-exceptional circumstances.
Any comments on this sort of do/while (false) style?
Angus.
推荐答案
Angus Graham写道:
Angus Graham wrote:
嗨:
我最近遇到过一些程序员使用以下
类型的do /虽然(假)结构很常见:
if {
if(!CatchFish(pFish)){
break;
}
if(!CleanFish (pFish)){
休息;
}
如果(!CookFish(pFish)){
休息;
}
if(!EatFish(pFish) )){
break;
}
while(false);
删除pFish;
Hi:
I''ve recently come across a number of programmers who use the following
type of do/while(false) structure quite often:
do {
if (! CatchFish(pFish)) {
break;
}
if (! CleanFish(pFish)) {
break;
}
if (! CookFish(pFish)) {
break;
}
if (! EatFish(pFish)) {
break;
}
while (false);
delete pFish;
如果 - 有什么问题 - 否则如果?
if(! CatchFish(pFish)){}
else if(!CleanFish(pFish)){}
else if(!CookFish(pFish)){}
else if(!EatFish(pFish)){}
删除pFish;
What''s wrong with if - else if?
if (! CatchFish(pFish)) {}
else if (! CleanFish(pFish)) {}
else if (! CookFish(pFish)) {}
else if (! EatFish(pFish)) {}
delete pFish;
2004年2月14日星期六12:46: 06-0500 in comp.lang.c ++,Angus Graham
< pl **************** @ notme.com>据称写了:
On Sat, 14 Feb 2004 12:46:06 -0500 in comp.lang.c++, Angus Graham
<pl****************@notme.com> was alleged to have written:
这是有效的,但在我看来是不好的风格。 do / while循环
实际上并不是一个有误导性的循环。同样可能是
This is effective but seems to me to be bad style. The do/while loop
isn''t really a loop at all which is misleading. The same could be
嗯,你可以做Perl风格...
CatchFish(pFish)
&& CleanFish(pFish)
&& CookFish(pFish)
&& EatFish(pFish);
Well, you could do it Perl style...
CatchFish(pFish)
&& CleanFish(pFish)
&& CookFish(pFish)
&& EatFish(pFish);
" Angus Graham" < PL **************** @ notme.com>在消息中写道
新闻:F9 ******************* @ news20.bellglobal.com ..
"Angus Graham" <pl****************@notme.com> wrote in message
news:F9*******************@news20.bellglobal.com.. .
这是有效的,但在我看来是不好的风格。 do / while循环
根本不是一个误导的循环。
This is effective but seems to me to be bad style. The do/while loop
isn''t really a loop at all which is misleading.
do循环的优点是它保证执行至少
一次;但在我看来,他们暗示了不止一次执行的可能性。
这让我想起了另外两个可疑但又流行的语言结构:
for(; ;)循环和if(true)语句
我也避免使用它们。
The advantage of a do loop is that it is guaranteed to execute at least
once; but to my mind they imply the possibility of executing more than once.
It reminds me of two other questionable but popular language structures: the
for(;;) loop and the if(true) statement
I avoid their use as well.
这篇关于do / while false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!