刚看到一个提问帖: 《如果程序中出现多层嵌套的 if...else...语句,如何重构可使程序逻辑变得更为清晰易读?》,因回答篇幅比较大,单独开个帖子答一下。

个人喜好代码风格不一样,下面只是我认为好的代码风格,不喜勿喷。如果有其他好的技巧,欢迎分享补充。

技巧一#

删除 else

如:

function test($arg)
{
if($arg == 'foobar'){
return true;
}else{
return false;
}
}

尽量写成这样

function test($arg)
{
if($arg == 'foobar'){
return true;
} return false;
}

优先将代码量少,可使流程中断的代码块(return, throw excetion, continue ...)放到 if 中, 提前中断代码。

技巧二#

拆分为多个函数

如果整个 if else 中的代码比较多,或者 if 与 else 中带代码不会导致后面的判断流程中断,并且还有 if else 之外的代码,将就 if else 中的代码拆分为多个函数。

if($age > 18){
doSomeThingA();
doSomeThingB();
doSomeThingC();
}else{
doSomeThingD();
doSomeThingE();
}

这种方式需要将函数名取的尽量清晰易懂,不要嫌长。

技巧三#

罗列规则式的写代码

多层 if 嵌套的语法,把他写成线性的,就像写规则一样将其一条条罗列出来

如:

function match($age, $salary, $pretty){
if($age > 18){
// do some thing A;
if($salary > 5000){
// do some thing B;
if($pretty == true){
return true;
}
}
} return false;
}

改写成这样是不是清晰多了?

function match($age, $salary, $pretty){
if($age < 18){
return false;
} // do some thing A; if($salary < 5000){
return false;
} // do some thing B; return $pretty == true;
}

总结#

少用 else , 提前中断(return)!!!#

少用 else , 提前中断(return)!!!#

少用 else , 提前中断(return)!!!#

重要的事情说三遍!

、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、

我也来尝试回答这个问题如果程序中出现多层嵌套的 if...else...语句,如何重构可使程序逻辑变得更为清晰易读?

一般原则是if 是越少越好,能不用就不用,层级越少越好。

技巧1:映射表法#

function contry_initial($country){
if ($country==="China" ){
return "CHN";
}else if($country==="America"){
return "USA";
}else if($country==="Japna"){
return "JPN";
}else{
return "OTHER";
}
}

这样的if语句,可以看到值和返回是一一对应的,所以你可以这样写

function contry_initial($country){
$countryList=[
"China"=> "CHN",
"America"=> "USA",
"Japna"=> "JPN",
]; if(in_array($country, array_keys($countryList))) {
return $countryList[$country];
}
return "Other"; }

如果需要更加自由的定义映射表的话,可以这样写

function contry_initial($country, array $countryList){
if(in_array($country, array_keys($countryList))) {
return $countryList[$country];
}
return "Other";
}

完全去掉if语句可以写成

function contry_initial($country, array $countryList){
return in_array($country, array_keys($countryList))?$countryList[$country]:"Other";
}

技巧二:多维映射表#

自己制定一个标准,建立一个多维的映射表,然后尽量少的if语句去囊括所有范例

function match($age,$gender,$pretty){
$list=[
['age'=>[1,12], 'gender'=>"Male",'pretty'=>true,'action'=>function(){//do something},'return'=>'pretty young man'],
['age'=>[1,12], 'gender'=>"Female",'pretty'=>true,'action'=>function(){//do something},'return'=>'pretty young lady'],
['age'=>[1,12], 'gender'=>"Male", 'pretty'=>false,'action'=>function(){//do something},'return'=>'boy'],
['age'=>[1,12], 'gender'=>"Female",'pretty'=>false,'action'=>function(){//do something},'return'=>'girl'],
['age'=>[13,18], 'gender'=>"Male", 'pretty'=>true,'action'=>function(){//do something},'return'=>'pretty man'],
....
]; foreach($list as $item){
if($age>=$item['age'][0]&&$age<=$item['age'][1]&&$gender===$item['gender']&&$pretty===$item['pretty']){
$item['action']();
return $item['return']; }
}
return null; }

当然,这样排列组合可能会有很多案例,而callable的代码也会有重复。所以改进的方案是

function match($age,$gender,$pretty){
$ageList=[
[ "age"=>[1,12],"action"=>function(){},"return"=>"young"],
[ "age"=>[13,18],"action"=>function(){},"return"=>"teenage"],
[ "age"=>[19,100],"action"=>function(){},"return"=>"adult"],
[ "age"=>[100,1000],"action"=>function(){},"return"=>"adult"],
]; $genderList=[
'Male'=>["action"=>function(){},"return"=>"man"],
'Female'=>["action"=>function(){},"return"=>"lady"],
'default'=>["action"=>function(){},"return"=>"person"],
]; $prettyList=[
true=>["action"=>function(){},"return"=>"pretty"],
false=>["action"=>function(){},"return"=>""],
]; foreach($ageList as $item){
if($age>=$item['age'][0]&&$age<=$item['age'][1]){
$item['action']();
$returnValue= $item['return'];
}
} if(in_array($gender,array_keys($genderList))) {
$genderList[$gender]['action']();
$returnValue .=" ".$genderList[$gender]['return'];
} else {
$genderList['default']['action']();
$returnValue .=" ".$genderList['default']['return'];
} $prettyList[$pretty]['action'](); return $prettyList[$pretty]['return']." ".$returnValue;
}

总结#

以上代码并不完美,总之要更具需要去重构,而不是为了优雅而重构。

使用映射表法的好处是提高单元测试的覆盖率,而坏处是增加了加载时间和消耗内存空间

当然,还是要注意一些

  • return能越早越好
  • if else 语句越少越好,可以用condition?a:b 表达的,就不要用if else
  • 有一一对应关系的,使用映射表。

改善过多的if else的更多相关文章

  1. MySQL监控模板说明-Percona MySQL Monitoring Template for Cacti

    http://blog.chinaunix.net/uid-16844903-id-3535535.html https://www.percona.com/doc/percona-monitorin ...

  2. Percona监控MySQL模板详解

    InnoDB Adaptive Hash Index 显示了"自适应哈希索引"的使用情况,哈希索引只能用来搜索等值的查询. # Hash table size 17700827, ...

  3. 编写高质量代码改善C#程序的157个建议——建议78:应避免线程数量过多

    建议78:应避免线程数量过多 在多数情况下,创建过多的线程意味着应用程序的架构设计可能存在着缺陷.经常有人会问,一个应用程序中到底含有多少线程才是合理的.现在我们找一台PC机,打开Windows的任务 ...

  4. [转]响应式WEB设计学习(3)—如何改善移动设备网页的性能

    原文地址:http://www.jb51.net/web/70362.html 前言 移动设备由于受到带宽.处理器运算速度的限制,因而对网页的性能有更高的要求.究竟是网页中的何种元素拉低了网页在移动设 ...

  5. 改善C#公共程序类库质量的10种方法

    最近重构一套代码,运用以下几种方法,供参考. 1  公共方法尽可能的使用缓存 public static List<string> GetRegisteredCompany() { Str ...

  6. 编写高质量代码--改善python程序的建议(六)

    原文发表在我的博客主页,转载请注明出处! 建议二十八:区别对待可变对象和不可变对象 python中一切皆对象,每一个对象都有一个唯一的标识符(id()).类型(type())以及值,对象根据其值能否修 ...

  7. 重构HTML改善web应用设计

    本文从良构,有效性,布局三个角度,结合往日项目开发经历, 整理总结重构HTML改善Web应用设计的几点规则和做法.部分参考自<重构HTML改善Web应用设计>. 重构.什么是重构?为什么要 ...

  8. Microsoft.VisualBasic.dll的妙用and 改善C#公共程序类库质量的10种方法

    Microsoft.VisualBasic.dll的妙用(开发中肯定会用到哦) 前言 做过VB开发的都知道,有一些VB里面的好的函数在.NET里面都没有,而Microsoft.VisualBasic. ...

  9. 改善C#公共程序类库质量的10种方法和工具

    最近重构一套代码,运用以下几种方法,供参考. 1  公共方法尽可能的使用缓存 public static List<string> GetRegisteredCompany() { Str ...

随机推荐

  1. kali 密码攻击

    第八章 密码攻击 作者:Willie L. Pritchett, David De Smet 译者:飞龙 协议:CC BY-NC-SA 4.0 这一章中,我们要探索一些攻击密码来获得用户账户的方式.密 ...

  2. 连接mysql问题 mysqlnd cannot connect to MySQL 4.1+ using old authentication

    第一篇:PHP5.3开始使用MySqlND作为默认的MySql访问驱动,而且从这个版本开始将不再支持使用旧的用户接口链接Mysql了,你可能会看到类似的提示: #2000 - mysqlnd cann ...

  3. Extjs 中column的renderer使用方法

    renderer: function(value, cellmeta, record, rowIndex, columnIndex, store) { if (record.get('productT ...

  4. NGINX: 405 Not Allowed

    近期做一个手机端静态网站,在wcm上网站预览的时候显示正常,网站数据发布到nginx网站服务上后,发现页面有部分不显示: 正常页面: 错误页面: 进入谷歌浏览器的Developer Tools(F12 ...

  5. VMware简介

    VMware(中文名威睿”) 虚拟机软件,是全球桌面到数据中心虚拟化解决方案的领导厂商.VMware vSphere 是VMware 的一个虚拟化产品,它包括vCenter,ESX Server,ES ...

  6. 看StackOverflow如何用25台服务器撑起5.6亿的月PV(微软的架构)

     问答社区网络 StackExchange 由 100 多个网站构成,其中包括了 Alexa 排名第 54 的 StackOverflow.StackExchang 有 400 万用户,每月 5.6 ...

  7. lighttpd与fastcgi+cgilua原理、代码分析与安装

    原理 http://www.cnblogs.com/skynet/p/4173450.html 快速通用网关接口(Fast Common Gateway Interface/FastCGI)是通用网关 ...

  8. Git 遇到了 early EOF index-pack failed 问题

    Git 遇到了 early EOF index-pack failed 问题 今天想 clone 一下 boost 在 github 的 repo,结果在 clone 的过程中遇到了下面的错误.我原本 ...

  9. css布局之左侧固定右侧自适应布局

    参考代码如下: <form id="form1" style="height:100%; overflow:hidden;"> <div st ...

  10. -XX:-PrintClassHistogram 按下Ctrl+Break后,打印类的信息

    -XX:+PrintClassHistogram –按下Ctrl+Break后,打印类的信息: num     #instances         #bytes  class name ------ ...