替换嵌套的if语句 [英] Replacing nested if statements
问题描述
这与美丽代码.
在这一章中,我读到了有关嵌套的if
的信息.
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屋!