Chinaunix
标题:
if elseif else太多怎么办
[打印本页]
作者:
feiyue0908
时间:
2013-02-18 17:39
标题:
if elseif else太多怎么办
写了一个函数,if elseif else太多,很不漂亮,有没有好的办法优化。
主要是判断$child,$mother,$father三个值,每个参数可能有四个值11,10,01,00
,第一个参数$child必需的,其它两个可选,计算每一种的组合,根据不同的组合,返回一个不同的$allele值。
如$child=00,$mother空值,$father空值,返回 "Unknown"
$child=11, $mother= 10, $father空值, 返回"Both (Homozygous)"
谢谢,新手只能写成这样难看的代码。
<?php
function getAllele($child,$mother = '',$father = '')
{
//only child data
if(empty ($mother) && empty ($father)){
if($child == '11'){
$allele = 'Both (Homozygous)';
}else{
$allele = 'Unknown';
}
return $allele;
//child and mother data
}elseif(!empty ($mother) && empty ($father)){
if($child == "11"){
$allele = 'Both (Homozygous)';
}elseif ($child == '01' or $child == '10'){
if($mother == '11'){
$allele = 'Maternal (inferred)';
}elseif ($mother == '00'){
$allele = 'Paternal (inferred)';
}else{
$allele = 'Unknown';
}
}else{
$allele = 'Unknown';
}
return $allele;
//child and father data
}elseif(empty ($mother) && !empty ($father)){
if($child == "11"){
$allele = 'Both (Homozygous)';
}elseif ($child == '01' or $child == '10'){
if($father == '11'){
$allele = 'Paternal (inferred)';
}elseif ($father == '00'){
$allele = 'Maternal (inferred)';
}else{
$allele = 'Unknown';
}
}else{
$allele = 'Unknown';
}
return $allele;
//child and parents data
}elseif(!empty ($mother) && !empty ($father)){
if($child == "11"){
$allele = 'Both (Homozygous)';
}elseif ($child == '01' or $child == '10'){
if($mother == '11'){
if($father == '01' or $father = '10' or $father == '00'){
$allele = 'Maternal (confirmed)';
}else{
$allele = 'Unknown';
}
}elseif($mother == '01' or $mother == '10'){
if($father== "00"){
$allele = 'Maternal (confirmed)';
}elseif($father=='11'){
$allele = 'Paternal (confirmed)';
}else{
$allele = 'Unknown';
}
}elseif ($mother == '00'){
if($father == '10' or $father == '01' or $father == '11'){
$allele = 'Paternal (confirmed)';
}else{
$allele = 'Unknown';
}
}
}else{
$allele = 'Unknown';
}
return $allele;
}
}
//end
?>
作者:
chlinux
时间:
2013-02-18 17:48
本帖最后由 chlinux 于 2013-02-18 17:55 编辑
回复
1#
feiyue0908
可考虑把数据写到一个数组里。可多维
array(
'child'=>array(11,10,01,00),
'mother'=>array(11,10,01,00),
'father'=>array(11,10,01,00),
)
//然后
foreach ($array as $key => $value) {
//
}
复制代码
作者:
feiyue0908
时间:
2013-02-18 18:38
谢谢,我试试吧,对复杂程序还是比较迷茫的,查查资料看看
回复
2#
chlinux
作者:
bikong0411
时间:
2013-02-19 09:19
switch/case
作者:
pitonas
时间:
2013-02-20 09:46
bikong0411 发表于 2013-02-19 02:19
switch/case
谢谢,我试试查查资料看看
作者:
yyxxzz
时间:
2013-02-20 17:41
能用表格驱动的不要用if else
作者:
feiyue0908
时间:
2013-02-20 20:41
你说的表格驱动是什么样的?谢谢
回复
6#
yyxxzz
作者:
yyxxzz
时间:
2013-02-21 10:35
回复
7#
feiyue0908
<代码大全2> 表格驱动 google 一下
作者:
maochanglu
时间:
2013-02-21 11:37
switch
作者:
helww
时间:
2013-02-22 10:17
逆向思维:
结果的状态集 相比于 if-else条件集小很多,可画一个状态机。
一个输出对应于有哪些条件集,条件集是否有合并的余地?
另:一个输出对应一组条件,针对这个输出写一个函数。如果有N个输出,就写N个函数。会清晰一些。
作者:
helww
时间:
2013-02-22 10:19
Both (Homozygous)
Unknown
Maternal (inferred)
Paternal (inferred)
只需要写四个函数而已。
作者:
qsbaq
时间:
2013-02-24 10:05
SWITCH 比较靠谱
作者:
yudotyang
时间:
2013-02-27 20:49
像这样的要考虑类和设计模式了,把改变和扩展的从不易变化的代码中分离出来就容易维护了。而且系统的弹性也会比较好
作者:
PHP小笨笨
时间:
2013-03-18 09:39
回复
4#
bikong0411
猪脑啊,会不会编程
作者:
PHP小笨笨
时间:
2013-03-18 09:40
谁特么说的switch 能特么行吗?傻啊
作者:
ethantsien
时间:
2013-03-18 10:03
按照OOP的细想,如果IF ELSE太多的话,就要用到设计模式了
作者:
craaazy123
时间:
2013-03-18 11:36
有时候用多维数组(或map)可以减少一些if-else
作者:
bikong0411
时间:
2013-03-19 09:18
回复
14#
PHP小笨笨
老子编程的时候,你还在和泥玩,尼玛!
作者:
yangyf1990
时间:
2013-03-21 21:06
数组比较好~
作者:
cuphper
时间:
2013-04-17 16:00
简单的用数组
作者:
gavindev
时间:
2013-04-20 15:42
这个时候建议去找本面向对象,设计模式的书看看,会很有赶脚的说。
欢迎光临 Chinaunix (http://bbs.chinaunix.net/)
Powered by Discuz! X3.2