博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
改善过多的if else
阅读量:4483 次
发布时间:2019-06-08

本文共 3535 字,大约阅读时间需要 11 分钟。

刚看到一个提问帖: 《》,因回答篇幅比较大,单独开个帖子答一下。

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

file

技巧一

删除 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 是越少越好,能不用就不用,层级越少越好。

技巧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[

转载于:https://www.cnblogs.com/clphp/p/5939905.html

你可能感兴趣的文章
py16 面向对象深入
查看>>
CentOS 7 安装 Gitlab
查看>>
JavaScript-03-常见函数
查看>>
ajax 设置Access-Control-Allow-Origin实现跨域访问
查看>>
去掉ExpandableListView的箭头图标
查看>>
[LeetCode]Binary Tree Level Order Traversal II
查看>>
跨页面传值自动刷新 操作文本与文件夹
查看>>
最完美的毁尸灭迹:皮箱连环弃尸案始末
查看>>
002
查看>>
WCF服务“*”有零个应用程序(非基础结构)终结点。这可能是因为未找到应用程序的配置文件,或者在配置文件中未找到与服务名称匹配的服务元素,或者服务元素中未定义终结点。...
查看>>
cocos2d 读书随笔《cocos2d-x游戏开发技术精讲》
查看>>
Asterisk 代码架构概述
查看>>
中兴电信光纤猫F450获取管理员密码方法
查看>>
申请TexturePacker 或 PhysicsEditor free licenses
查看>>
kafka启动报错&问题解决
查看>>
nginx反向代理下没有获取到正确的clientIP问题发散
查看>>
python周报第一周
查看>>
IBM MQ 创建以及常见问题集锦
查看>>
Office文件的奥秘——.NET平台下不借助Office实现Word、Powerpoint等文件的解析(1)
查看>>
SQL Server 服务器磁盘测试之SQLIO篇(一)
查看>>