案例之间共享代码 [英] Sharing code between cases

查看:54
本文介绍了案例之间共享代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道什么是最好的是处理switch语句中两个或更多个案例之间共享的代码块的方法。考虑下面代码的

(假设有效的变量和函数定义,显然是
):


switch(a)

{

案例1:

if(x)返回false;

y = 6;

foo();

休息;


案例2:

如果(x)返回false;
$ b $ = 6;

bar();

休息;


案例3:

foo();

休息;

}


当然,这不是太糟糕,但当前两行变成了b / b $ b打,或者因为有更多的情况共享公共代码,所以改变一个没有反映给所有其他人的可能性。 />
增加。一般来说,常见代码的解决方案是将它拉出来,

但是我在这里看到的方式是:


1)顺序开关

开关(a)

{

案例1:

案例2:

//这里共享代码

}

开关(a)

{

//这里的唯一代码

}

如果(a == 1 || a == 2)

这也可能写成

{

//这里共享代码

}

取决于是否有多个不同的共享块

代码。


这种方法看起来可能令人困惑,也许会导致维护

问题。


2)放普通话从每个适用的

案例中调用的函数中的代码。这在某些情况下会很好用,但我认为,如果

是可能生成的多个可能的早期返回值,那么

可以测试的多个变量或者受影响(现在都需要

作为函数的参数)等等。


异常可能是解决多重返回值的一种方法。

的一部分这个解决方案的问题。

那么,有没有其他方法我错过了人们在这个

情况下使用的方法?这或许表明我正在尝试使用一个

变量来控制两个正交值(重构时间)?


-

Greg Schmidt gr***@trawna.com

Trawna Publications http://www.trawna.com/

I''m wondering what the "best" is way to handle blocks of code that are
shared between two or more cases in a switch statement. Consider the
following code (assuming valid variable and function definitions,
obviously):

switch (a)
{
case 1:
if (x) return false;
y = 6;
foo();
break;

case 2:
if (x) return false;
y = 6;
bar();
break;

case 3:
foo();
break;
}

Of course, this isn''t too bad, but when the first two lines turn into a
dozen, or as there are more cases that share common code, the
possibility of a change to one not being reflected to all others
increases. Generally, the solution to common code is to pull it out,
but the ways that I see of doing that here are:

1) Sequential switches
switch (a)
{
case 1:
case 2:
// shared code here
}
switch (a)
{
// unique code here
}
This might also be written like
if (a == 1 || a == 2)
{
// shared code here
}
depending on whether there were multiple different blocks of shared
code.

This method looks potentially confusing, and perhaps causes maintenance
problems.

2) Put the common code in a function that is called from each applicable
case. This would work well in some cases, but not, I think, if there
are multiple possible early return values that might be generated,
multiple variables that may be tested or affected (which now all need to
be parameters to the function), etc.

Exceptions could be a way to solve the "multiple return value" part of
the problem with this solution.
So, are there other methods I''ve missed that people use in this
situation? Is this perhaps an indication that I''m trying to use one
variable to control two orthogonal values (time to refactor)?

--
Greg Schmidt gr***@trawna.com
Trawna Publications http://www.trawna.com/

推荐答案



" Greg Schmidt" < GR *** @ trawna.com> schrieb im Newsbeitrag

新闻:1o *************** @ trawna.com ...

"Greg Schmidt" <gr***@trawna.com> schrieb im Newsbeitrag
news:1o***************@trawna.com...
我想知道是什么最好的是处理在switch语句中两个或多个case之间共享的代码块的方法。考虑
下面的代码(假设有效的变量和函数定义,显然是
):

switch(a)
{
案例1 :
如果(x)返回false;
y = 6;
foo();
中断;

案例2:
如果(x)返回false;
y = 6;
bar();
休息;

案例3:
foo();
休息;
}
I''m wondering what the "best" is way to handle blocks of code that
are
shared between two or more cases in a switch statement. Consider
the
following code (assuming valid variable and function definitions,
obviously):

switch (a)
{
case 1:
if (x) return false;
y = 6;
foo();
break;

case 2:
if (x) return false;
y = 6;
bar();
break;

case 3:
foo();
break;
}




我这样做 - 而且我知道这被认为是糟糕的风格,每个人

现在会激怒我 - 因为写起来很快:


#define DEF1 {if(x)return false; y = 6;}


开关(a)

{

案例1:DEF1

foo();

休息;

案例2:DEF2

bar();

break;

案例3:

foo();

休息;

}


any,是的,我也为很长的积木做这件事。

-Gernot




I do this - and I know this is considered crappy style and everyone
will flame me now - for it''s fast to write:

#define DEF1 {if (x) return false; y=6;}

switch(a)
{
case 1: DEF1
foo();
break;
case 2: DEF2
bar();
break;
case 3:
foo();
break;
}

any, yes, I do this for very long blocks as well.
-Gernot



In消息< 1o *************** @ trawna.com> ;, Greg Schmidt

< gr *** @ trawna.com>写道


[switch()并发症剪断]
In message <1o***************@trawna.com>, Greg Schmidt
<gr***@trawna.com> writes

[switch() complications snipped]

那么,是否还有其他方法,我已经错过了人们在这里使用的方法/>情况?这或许表明我正在尝试使用一个
变量来控制两个正交值(重构的时间)吗?

So, are there other methods I''ve missed that people use in this
situation? Is this perhaps an indication that I''m trying to use one
variable to control two orthogonal values (time to refactor)?




也许,但不要不要做那么明显,把它分成两个值。当

变量控制复杂*行为*时,它看起来像是一个暗示,你需要通过多态类来表示这种行为。然后

而不是


开关(a)

{

/ *很多情况,很多东西* /

}


你会有


p-> DoStuff() ;


-

Richard Herring



Maybe, but don''t just do the obvious and split it into two values. When
a variable is controlling complex *behaviour*, it looks like a hint that
you need to represent that behaviour by polymorphic classes. Then
instead of

switch(a)
{
/* lots of cases, lots of stuff */
}

you''ll have

p->DoStuff();

--
Richard Herring




Greg Schmidt写道:

Greg Schmidt wrote:
例外可能是一种解决多重返回值的方法。部分
这个解决方案的问题。
Exceptions could be a way to solve the "multiple return value" part of the problem with this solution.




我真的很惊讶没有其他人跳过这个。


异常不是返回值的另一种方法。例外是

特殊情况,不应用于流量控制或

退货。这是对该功能的滥用,并包含各种各样的b / b
bugbears,他们喜欢吃丢失的程序员。



I am really surprised nobody else jumped on this one.

Exceptions are not another way to return values. Exceptions are for
exceptional situations and should not be used for flow control or
returns. That is a misuse of the feature and contains all sorts of
bugbears that love to eat lost programmers.


这篇关于案例之间共享代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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