【发布时间】:2011-03-21 06:29:18
【问题描述】:
在最近使用 ReSharper 时,建议我通过反转 if 条件并使用 continue 语句来减少某些地方的嵌套。
嵌套条件:
foreach(....)
{
if(SomeCondition)
{
//do some things
if(SomeOtherNestedCondition)
{
//do some further things
}
}
}
继续声明:
foreach(....)
{
if(!SomeCondition) continue;
//do some things
if(!SomeOtherNestedCondition) continue;
//do some further things
}
我了解您为什么要减少嵌套以解决性能和内存问题以及两个 sn-ps 如何相互等同的一些逻辑,但是从我的开发背景来看,之前 em> 示例在阅读代码时更容易理解。
您更喜欢哪种方法,为什么?您是否在日常代码中使用 continue 而不是嵌套的 if?
【问题讨论】:
标签: c# resharper nested-loops continue