如何使这段代码组织得更好
这是代码...我的 if , else 语句很长...如何将其分开?有什么建议吗?谢谢。
public function receiveMsg(aMsg){
if($aMsg instanceof LoginMsg){
$this->callingSomeMethod(); //should I separate this code into other class/ object?
$this->callingAnotherMethod(); //should I separate this code into other class/ object?
$aMsg = new RespondLoginMsg(); //should I separate this code into other class/ object?
$this->sendMsg($aMsg); //should I separate this code into other class/ object?
}else if(aMsg instanceof LogoutMsg){
$this->callingSomeMethod(); //should I separate this code into another class/ object?
$aMsg = new RespondLogoutMsg(); //should I separate this code into another class/ object?
$this->sendMsg($aMsg); //should I separate this code into another class/ object?
}else if/*****bababab***/
/*****many else if here (up to 200 msg+ , just upload 2 here.)***/
}
Here is the code... My if , else statement is very long... How to separate it? Any suggestion? Thank you.
public function receiveMsg(aMsg){
if($aMsg instanceof LoginMsg){
$this->callingSomeMethod(); //should I separate this code into other class/ object?
$this->callingAnotherMethod(); //should I separate this code into other class/ object?
$aMsg = new RespondLoginMsg(); //should I separate this code into other class/ object?
$this->sendMsg($aMsg); //should I separate this code into other class/ object?
}else if(aMsg instanceof LogoutMsg){
$this->callingSomeMethod(); //should I separate this code into another class/ object?
$aMsg = new RespondLogoutMsg(); //should I separate this code into another class/ object?
$this->sendMsg($aMsg); //should I separate this code into another class/ object?
}else if/*****bababab***/
/*****many else if here (up to 200 msg+ , just upload 2 here.)***/
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
也许 switch 更容易阅读? sendMsg 可以以任何一种方式移出,只需使用您在 swtich/if 中设置的 $aMsg 对象即可。
Perhaps a switch is easier to read? And the sendMsg could be moved out of it either way and just use the $aMsg object that you set in the swtich/if..
如果需要的话,我不认为长 if/else 语句有什么根本错误。
如果您发现它有碍观瞻,为什么不将每个 if/else 块的内容定义为它自己的函数呢?
您还可以考虑消除冗余。如果它们都以 then 结尾
,则无需在每个块中重复它。
I don't see anything fundamentally wrong about a long if/else statement if that's what's called for.
If you're finding it to be an eyesore, why not just define the contents of each if/else block as it's own function?
You can also consider eliminating redundencies. If they all end with
then there's no need to repeat it in each block.
除了摆脱过多的间距和换行符之外,我不明白这里的问题是什么。您需要检查多少个不同的类来检查某个项目是否是其实例?在我看来,这里的列表非常有限。如果您没有超过 3 或 4 个 if/else 语句,则只需将其保留为 if/else 即可。否则使用开关或循环。
您能更具体地说明您想要实现的目标吗?
这是代码的稍微干净的版本。
Other than getting rid of excessive spacing and line breaks I don't see what the problem here is. How many different classes do you need to check if an item is an instance of? Seems to me that the list would be pretty limited here. If you don't have any more than 3 or 4 if/else statements then just keep it an if/else. Otherwise use a switch or a loop.
Can you be more specific as to what your are trying to accomplish?
Here is a slightly cleaner version of your code.