哪个是更好的做法? (如果这有所作为,我正在 .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/