【每日阅读】2020年5月13日-如何解决代码中 if…else 过多的问题

链接

https://mp.weixin.qq.com/s/roquJ8dymBbgh31U0U6yAA

文章截图

简评

稍微总结一下,平时简化if-else的从简单到高阶的方法:

  1. 用具有可读性的变量替换繁琐臃肿的if表达式
  2. 抽取方法,让if-else清晰明朗
  3. 类的多态
  4. 设计模式:职责链/状态机

文中还有一些方法我没有写道这里,因为我觉得我不会去使用他们。例如那个Optional类,真的好?我怎么感觉可读性还不如if-else呢,至少if-else逻辑很明显。

原创文章,作者:geekgao,如若转载,请注明出处:https://www.geekgao.cn/archives/1656

(0)
geekgaogeekgao博主
上一篇 2020年5月12日
下一篇 2020年5月15日

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

GitHub
分享本页
返回顶部

Warning: error_log(/usr/local/lighthouse/softwares/wordpress/wp-content/plugins/spider-analyser/#log/log-2713.txt): failed to open stream: No such file or directory in /usr/local/lighthouse/softwares/wordpress/wp-content/plugins/spider-analyser/spider.class.php on line 2900