替换嵌套的 if 语句 [英] Replacing nested if statements
问题描述
这与漂亮代码的一章有关.在那一章中,我读到了嵌套的 if
s.
This is related to a chapter from beautiful code.
And in that chapter I read about the nested if
s.
作者在谈论深度嵌套的 if
是错误的始作俑者,而且可读性较差.他正在谈论用 case
语句和 if">决策表.
The author was talking about deeply nested if
s as originator of bugs and less readable.
And he was talking about replacing nested if
s with case
statements and decision tables.
有人可以说明如何使用 case
(select case
) 和决策表删除嵌套的 if
吗?
Can anybody illustrate how to remove nested if
s with case
(select case
) and decision tables ?
推荐答案
好吧,不是直接回答您的问题,因为您专门询问 switch/case 语句,但这里有一个类似的问题.
Well, not directly an answer to your question since you specifically ask about switch/case statements, but here is a similar question.
这是关于用保护语句替换嵌套的 if 语句,它会提前返回,而不是在确定返回值之前逐步检查越来越多的事情.
This talks about replacing nested if's with guard-statements, that return early, instead of progressively checking more and more things before settling on a return value.
这篇关于替换嵌套的 if 语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!