gpt4 book ai didi

c++ - 重复的 switch 语句需要重构

转载 作者:行者123 更新时间:2023-11-28 03:34:42 28 4
gpt4 key购买 nike

许多重复的 switch 语句似乎需要 DRY'd。有什么建议么? (包括什么都不做!)

AnimMapIter _iter;
_iter = _animations->find(name);
if(_iter == _animations->end()) return;

if(_curName != name) {
_curName = name;

switch(dir) {
case DIR_FORWARD_LOOPING: /* Fall through to DIR_FORWARD_NONLOOPING */
case DIR_FORWARD_NONLOOPING:
_iter->second->First();
break;
case DIR_REVERSE_LOOPING: /* Fall through to DIR_REVERSE_NONLOOPING */
case DIR_REVERSE_NONLOOPING:
_iter->second->Last();
break;
}
} else {

switch(dir) {
case DIR_FORWARD_LOOPING: /* Fall through to DIR_FORWARD_NONLOOPING */
case DIR_FORWARD_NONLOOPING:
_iter->second->Next();
break;
case DIR_REVERSE_LOOPING: /* Fall through to DIR_REVERSE_NONLOOPING */
case DIR_REVERSE_NONLOOPING:
_iter->second->Previous();
break;
}

switch(dir) {
case DIR_FORWARD_LOOPING:
if(_iter->second->IsAtEnd())
_iter->second->First();
break;
case DIR_FORWARD_NONLOOPING:
if(_iter->second->IsAtEnd())
_iter->second->Last();
break;
case DIR_REVERSE_LOOPING:
if(_iter->second->IsAtFront())
_iter->second->Last();
break;
case DIR_REVERSE_NONLOOPING:
if(_iter->second->IsAtFront())
_iter->second->First();
break;
}
}

最佳答案

else 下的所有内容都应该合并为一个开关,以拉近相关步骤;例如

case DIR_FORWARD_LOOPING:
_iter->second->Next();
if (_iter->second->IsAtEnd()) {
_iter->second->First();
}
break;

...都在那个案例中。重复几个函数调用并不是什么大问题,因为它可以使整个操作顺序更加清晰。

关于c++ - 重复的 switch 语句需要重构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11335486/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com