替换嵌套的if语句 [英] Replacing nested if statements

查看:101
本文介绍了替换嵌套的if语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这与美丽代码. 在这一章中,我读到了有关嵌套的if的信息.

This is related to a chapter from beautiful code. And in that chapter I read about the nested ifs.

作者谈论的是深度嵌套的if作为错误的发起者,而且可读性较差. 他当时在谈论用case语句和决策表替换嵌套的if.

The author was talking about deeply nested ifs as originator of bugs and less readable. And he was talking about replacing nested ifs with case statements and decision tables.

有人能说明如何使用case(select case)和决策表删除嵌套的if吗?

Can anybody illustrate how to remove nested ifs 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"语句以减少嵌套

这是关于用较早返回的保护语句替换嵌套的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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆