使用 switch 语句的最佳实践是什么?嵌套?具体的?
只是想知道如果两个代码块产生相同的结果,哪种方法会更好:
string from = ddFrom.SelectedItem.ToString(),
to = ddTo.SelectedItem.ToString();
switch(from)
{
case "celsius":
switch(to)
{
case "celsius":
break;
case "fahrenheit":
break;
case "kelvin":
break;
}
break;
case "fahrenheit":
switch(to)
{
case "celsius":
break;
case "fahrenheit":
break;
case "kelvin":
break;
}
break;
case "kelvin":
switch(to)
{
case "celsius":
break;
case "fahrenheit":
break;
case "kelvin":
break;
}
break;
}
或者这个:
string from = ddFrom.SelectedItem.ToString(),
to = ddTo.SelectedItem.ToString(),
conversion = from + to;
switch(conversion)
{
case "celsiusfahrenheit":
break;
case "celsiuskelvin":
break;
case "fahrenheitcelsius":
break;
case "fahrenheitkelvin":
break;
case "kelvincelsius":
break;
case "kelvinfahrenheit":
break;
}
谢谢。
just wondering which approach would be better if both blocks of code would yield the same result:
string from = ddFrom.SelectedItem.ToString(),
to = ddTo.SelectedItem.ToString();
switch(from)
{
case "celsius":
switch(to)
{
case "celsius":
break;
case "fahrenheit":
break;
case "kelvin":
break;
}
break;
case "fahrenheit":
switch(to)
{
case "celsius":
break;
case "fahrenheit":
break;
case "kelvin":
break;
}
break;
case "kelvin":
switch(to)
{
case "celsius":
break;
case "fahrenheit":
break;
case "kelvin":
break;
}
break;
}
or this one:
string from = ddFrom.SelectedItem.ToString(),
to = ddTo.SelectedItem.ToString(),
conversion = from + to;
switch(conversion)
{
case "celsiusfahrenheit":
break;
case "celsiuskelvin":
break;
case "fahrenheitcelsius":
break;
case "fahrenheitkelvin":
break;
case "kelvincelsius":
break;
case "kelvinfahrenheit":
break;
}
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
第二个选项更可取,因为正如每个人所说,它使代码看起来更好。
但是,您可能需要考虑一个更具架构性的选项:
用法:
当然,这可以进一步改进(首先想到使用枚举值而不是温度类型的字符串,也可以进行一些错误检查),但总体思路有没有。
恕我直言,这是介于旧式 C 风格大型开关和成熟的 OO 方法之间的一个很好的中间方法(这里不需要 OO,因为这个特定的转换问题有一个非常简单的域模型) )。
Second option is preferable because, as everyone has said, it makes the code look and feel better.
However, you might want to consider a more architected option:
Usage:
Of course this can be further improved (using enumeration values instead of strings for the temperature types comes to mind first, also some error checking is in order), but the general idea is there.
IMHO this is a good middle ground approach between the old school C-style mega-
switch
and a full-fledged OO approach (no real need for OO here because this specific conversion problem has a very simple domain model).第二个会更好地获得快速结果和更少的编码,因为它给出相同的结果。
The, second one would be better for fast result and less coding as it is giving the Same result.
最好重构你的代码,这样你就有了温度标度类和一个抽象工厂,它根据输入字符串返回相应的实例:
用法将是:
It is better to restructure your code, so you have temperature scale classes and an abstract factory that returns a corresponding instance basing an input string:
Usage will be:
我认为第二个更好、更直接、更易读。如果将来需要的话,第二种方法将更容易维护、修改和扩展。
I think second one is better, straight forward and more readable. Second approach would be more easy to maintain, modify and expand in future if needed.
选项 #2 看起来更干净并且会产生相同的结果
option #2 seems cleaner and would yield the same result