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

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

问题描述

这与漂亮代码的一章有关.在那一章中,我读到了嵌套的 ifs.

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天全站免登陆