If I have a database table containing a flag that can have multiple states, should I do this
如果我有一个包含可以有多个状态的标志的数据库表,我应该这样做
if ($Object->isStateOne()) {
// do something
}
else if ($Object->isStateTwo()) {
// do something else
}
else if ($Object->isStateThree()) {
// do yet something else
}
or this
switch ($Object->getSomeFlag()) {
case ObjectMapper::STATE_ONE:
// do something
break;
case ObjectMapper::STATE_TWO:
// do something else
break;
case ObjectMapper::STATE_THREE:
// do yet something else
break;
}
?
3 个解决方案
#1
Whichever makes sense, of course.
当然,无论哪个有意义。
The switch looks much cleaner. But, what is this 'state' you are checking? If you're translating a string, use an array, for example.
开关看起来更干净。但是,你正在检查这个'状态'是什么?例如,如果要翻译字符串,请使用数组。
The if has different behavior from switch. The method calls MAY have side effects. The if is better if multiple states may be active on the object at once, though.
if与switch有不同的行为。方法调用MAY可能有副作用。但是,如果多个状态可能同时在对象上处于活动状态,那么if会更好。
#2
From an OO perspective, both are discouraged. If you have different state, you may want to create a virtual methods, and override it in inherited class. Then based on polymorphism, you can avoid the if, and switch statement.
从OO的角度来看,两者都是气馁的。如果您具有不同的状态,则可能需要创建虚方法,并在继承的类中覆盖它。然后基于多态,可以避免使用if和switch语句。
#3
The second option just seems to be the better solution. IMHO, the unsightly duplication of the comparison code that would accompany the methods of the first solution would be the show-stopper for me, for example:
第二种选择似乎是更好的解决方案。恕我直言,第一个解决方案的方法伴随的比较代码的难看的重复将是我的节目,例如:
public function isStateOne() {
if(strcmp(ObjectMapper::STATE_ONE, '1') == 0) {
return true;
}
}
public function isStateTwo() {
if(strcmp(ObjectMapper::STATE_TWO, '2') == 0) {
return true;
}
}
public function isStateThree() {
if(strcmp(ObjectMapper::STATE_THREE, '3') == 0) {
return true;
}
}
Of course, others might disagree. I just don't like having classes cluttered up with 'almost the same' methods.
当然,其他人可能不同意。我只是不喜欢让课程与“几乎相同”的方法混乱。
#1
Whichever makes sense, of course.
当然,无论哪个有意义。
The switch looks much cleaner. But, what is this 'state' you are checking? If you're translating a string, use an array, for example.
开关看起来更干净。但是,你正在检查这个'状态'是什么?例如,如果要翻译字符串,请使用数组。
The if has different behavior from switch. The method calls MAY have side effects. The if is better if multiple states may be active on the object at once, though.
if与switch有不同的行为。方法调用MAY可能有副作用。但是,如果多个状态可能同时在对象上处于活动状态,那么if会更好。
#2
From an OO perspective, both are discouraged. If you have different state, you may want to create a virtual methods, and override it in inherited class. Then based on polymorphism, you can avoid the if, and switch statement.
从OO的角度来看,两者都是气馁的。如果您具有不同的状态,则可能需要创建虚方法,并在继承的类中覆盖它。然后基于多态,可以避免使用if和switch语句。
#3
The second option just seems to be the better solution. IMHO, the unsightly duplication of the comparison code that would accompany the methods of the first solution would be the show-stopper for me, for example:
第二种选择似乎是更好的解决方案。恕我直言,第一个解决方案的方法伴随的比较代码的难看的重复将是我的节目,例如:
public function isStateOne() {
if(strcmp(ObjectMapper::STATE_ONE, '1') == 0) {
return true;
}
}
public function isStateTwo() {
if(strcmp(ObjectMapper::STATE_TWO, '2') == 0) {
return true;
}
}
public function isStateThree() {
if(strcmp(ObjectMapper::STATE_THREE, '3') == 0) {
return true;
}
}
Of course, others might disagree. I just don't like having classes cluttered up with 'almost the same' methods.
当然,其他人可能不同意。我只是不喜欢让课程与“几乎相同”的方法混乱。