.net - IF/ELSE 语句顺序的最佳实践

标签 .net vb.net if-statement

哪个是更好的做法? (如果这有所作为,我正在 .Net 中编码)

IF condition = true THEN
   ...true action--even if rare...
ELSE
   ...action
END IF

或者
IF condition = [most common condition] THEN
   ...most common action....
ELSE
   ...least common action
END IF

最佳答案

根据 Code Complete 的作者 Steve McConnell 的说法,你应该

"Put the case you normally expect to process first. This is in line with the general principle of putting code that results from a decision as close as possible to the decision...[putting the normal case after the if] puts the focus on reading the main flow rather than on wading through the exceptional cases, so the code is easier to read overall."



Code Complete ,第 2 版,第 356-357 页。

关于.net - IF/ELSE 语句顺序的最佳实践,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1306158/

相关文章:

c# - C#中的对象和类有什么区别?

vb.net - 高亮显示ListView中的特定行

asp.net - 禁用 IE6 客户端的 GZIP 压缩

C# 创建类和派生类的实例

c# - 是否可以在 .NET Core 平台上运行 ASP .NET MVC 5?

c# - 使用 XSLT 将 XML 转换为固定长度的文本文件

mysql - 多重标准更新

python - 确定python中两个数据集是否相等的最佳方法?

javascript - 交换后如何重新加载gif?

java - 简化 if(执行某些操作)然后返回