Design patterns 消除中频环路中的冗余
您将如何编写以下代码:Design patterns 消除中频环路中的冗余,design-patterns,Design Patterns,您将如何编写以下代码: if(Conditon 1){ # code block 1... // another if condition if($device!=null){ # code block 2... }else{ # <code block X> } }else{
if(Conditon 1){
# code block 1...
// another if condition
if($device!=null){
# code block 2...
}else{
# <code block X>
}
}else{
# <code block X>
}
if(条件1){
#代码块1。。。
//另一个if条件
如果($device!=null){
#代码块2。。。
}否则{
#<代码块X>
}
}否则{
#<代码块X>
}
因此这里的
#
在两个地方是相同的,如何消除冗余?一般方法:找出每个要执行的块的先决条件:
- 代码块1:
Condition1
- 代码块2:
条件1&&$device=空
- 代码块x:
!条件1 | |!($device!=null)
!(a&b)=!a | | |!b
,因此如果有其他,它们适合进入一个:
if(Conditon1){
# code block 1...
}
if(Conditon1 && $device!=null){
# code block 2...
} else {
# <code block X>
}
if(条件1){
#代码块1。。。
}
如果(条件1&&$device!=null){
#代码块2。。。
}否则{
#<代码块X>
}
一般方法:找出要执行的每个块的先决条件:
- 代码块1:
Condition1
- 代码块2:
条件1&&$device=空
- 代码块x:
!条件1 | |!($device!=null)
块2的前提条件与块x的前提条件相反:!(a&b)=!a | | |!b
,因此如果有其他
,它们适合进入一个:
if(Conditon1){
# code block 1...
}
if(Conditon1 && $device!=null){
# code block 2...
} else {
# <code block X>
}
if(条件1){
#代码块1。。。
}
如果(条件1&&$device!=null){
#代码块2。。。
}否则{
#<代码块X>
}
//存储真/假的变量
var a=真;
如果(条件1){
#代码块1。。。
//另一个if条件
如果($device!=null){
#代码块2。。。
a=假;
}
}
如果(a){
#<代码块X>
}
//存储真/假的变量
var a=真;
如果(条件1){
#代码块1。。。
//另一个if条件
如果($device!=null){
#代码块2。。。
a=假;
}
}
如果(a){
#<代码块X>
}
如果(条件1){
#代码块1。。。
//另一个if条件
如果($device!=null){
#代码块2。。。
}
}
如果(!Condition1 | |$device==null){
#<代码块X>
}
如果(条件1){
#代码块1。。。
//另一个if条件
如果($device!=null){
#代码块2。。。
}
}
如果(!Condition1 | |$device==null){
#<代码块X>
}
将其移动到单独的函数并调用该函数?将其移动到单独的函数并调用该函数?
if(Conditon 1){
# code block 1...
// another if condition
if($device!=null){
# code block 2...
}
}
if (!Condition1 || $device==null){
# <code block X>
}